Direkt zum Hauptbereich

How to - Web browser Control

 Creating a simple HTML Editor using Web brower control

Create a basic Winform Appln.
Drag a Webbrowser( end in the list ) from Toolbox.
To  create buttons drag a Toolstrip control (near webbrowser ctrl in the list 'Commoncontrols' ) from Toolbox.
You can add buttons, text box, progressbar etc by clicking.
c
 

To make the Web browser control editable you have to write the following code while initializing.

  InitializeComponent();

   webBrowser1.DocumentText = "<HTML><BODY contentEditable='true'></BODY></HTML>";         
            toolStripComboFontSize.SelectedIndex = 2;


Double click on buttons to generate events.
Variable possibilities are there . Some are given below,

1. Change text color

 if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                webBrowser1.Document.ExecCommand("ForeColor", false, colorDialog1.Color);
            }      

2. Covert text to Bold
     webBrowser1.Document.ExecCommand("Bold", false, null);

3. Change text to Italics
    webBrowser1.Document.ExecCommand("Italic", false, null);

4. Justifying Text
   Left  :  webBrowser1.Document.ExecCommand("JustifyLeft", false, null);
   Right  : "JustifyRight"
   Middle : "JustifyCenter"

5. Font size 
  switch (toolStripComboFontSize.Text.Trim())
            {
                    case "1":                   
                    webBrowser1.Document.ExecCommand("FontSize", false, One);

                    break;

                    case "2":
                    webBrowser1.Document.ExecCommand("FontSize", false, Two);
                    break;

                    case "3":
                    webBrowser1.Document.ExecCommand("FontSize", false, Three);
                    break;

                    case "4":
                    webBrowser1.Document.ExecCommand("FontSize", false, Four);
                    break;

                    case "5":
                    webBrowser1.Document.ExecCommand("FontSize", false, Five);
                    break;

                    case "6":
                    webBrowser1.Document.ExecCommand("FontSize", false, Six);
                    break;

                    case "7":
                    webBrowser1.Document.ExecCommand("FontSize", false, Seven);                  
                    break;

                    case "8":
                    webBrowser1.Document.ExecCommand("FontSize", false, Eight);
                    break;

                    default:
                    webBrowser1.Document.ExecCommand("FontSize", false, Three);
                    break;
            }

6. Font Style

                webBrowser1.Document.ExecCommand("FontName", false, ComboFontStyles.SelectedItem);
    
& ofcourse dont forget to put inside a try catch block :)


You should populate the combobox ComboFontStyles at first.
   // Fill the Font Style Combo box
            foreach (FontFamily fam in FontFamily.Families)
            {
                ComboFontStyles.Items.Add(fam.Name);              
            }
            ComboFontStyles.SelectedItem = "Times New Roman"; 

7. Preview Function
 webBrowser1.ShowPrintPreviewDialog(); 





Kommentare

Beliebte Posts aus diesem Blog