Wednesday, February 2, 2011

Access HTML Editor Example

Tonight I added an improved example of how to create a Heading 1 style and add and remove a color style. This is useful for an Access HTML Editor using the Web Browser Control. Note how one can program DHTML from within an Access .NET Addin.

Apply an H1 element

void Heading1_Click()
{
Tasks.HtmlDocument.execCommand("formatblock", false, "<H1>");
}

Apply a color style

void ColorCombo_Change()
{   
//Apply style
try
{
mshtml.IHTMLTxtRange rng = (mshtml.IHTMLTxtRange)Tasks.HtmlDocument.selection.createRange();
             
string text = rng.text;
       var elem = rng.parentElement();

       if (elem.tagName != "SPAN")
       {
rng.pasteHTML("<span style='color:" + Tasks.ColorCombo.Text + ";'>" + text + "</span>");

              rng.moveStart("character", -text.Length);
              rng.select();
      }else if (elem.tagName == "SPAN" && elem.style.color != string.Empty)
      {
      if (Tasks.ColorCombo.Text != string.Empty)
elem.style.color = Tasks.ColorCombo.Text;
      else{
              //Remove SPAN element
              rng.pasteHTML(text);

              rng.moveStart("character", -text.Length);
              rng.select();
      }
   }

Tasks.WebBrowser.SetFocus();
   }
catch (Exception) { }
}

No comments:

Post a Comment