C # implement a simple Web Browser

Source: Internet
Author: User

(1) create a Windows Application Form, name it a web browser, and add corresponding controls to the form, as shown below:
 
(2) Add the corresponding event response code for the corresponding control. The complete code is as follows:
Using system; using system. Collections. Generic;
Using system. componentmodel; using system. Data;
Using system. Drawing; using system. text;
Using system. Windows. forms; using system. IO;
Namespace Web Browser
{
Public partial class form1: Form
{
Public form1 ()
{
Initializecomponent ();
}
Private void navigate (string address)
{
If (string. isnullorempty (Address ))
Return;
If (address. Equals ("about: blank") return;
If (! Address. startswith ("http: //") Address = "http: //" + address;
Try {
Cursor. Current = cursors. waitcursor; webbrowser1.navigate (New uri (Address ));
}
Catch (system. uriformatexception)
{Return ;}
Finally {cursor. Current = cursors. Default ;}
}
Private void form1_load (Object sender, eventargs E)
{
Toolstrip1.imagelist = imagelist1;
Tbback. imageindex = 0;
Tbforward. imageindex = 1;
Tbrefrash. imageindex = 2;
Tbhome. imageindex = 3;
}
Private void combobox1_keydown (Object sender, keyeventargs E)
{If (E. keycode = keys. Enter ){
Navigate (combobox1.text );
Combobox1.items. Add (combobox1.text );
}
}
Private void tbback_click (Object sender, eventargs E)
{
Webbrowser1.goback ();
}
Private void tbforward_click (Object sender, eventargs E)
{
Webbrowser1.goforward ();
}
Private void tbrefrash_click (Object sender, eventargs E)
{
I f (! Webbrowser1.url. Equals ("about: blank "))
{
Webbrowser1.refresh ();
}
}
Private void tbhome_click (Object sender, eventargs E)
{
Webbrowser1.gohome ();
}
Private void combobox#selectedindexchanged (Object sender, eventargs E)
{
Navigate (combobox1.text );
}
Private void open toolstripmenuitem_click (Object sender, eventargs E)
{
Openfiledialog1.showdialog ();
Webbrowser1.documenttext = file. readalltext (openfiledialog1.filename, encoding. getencodin ("gb2312 "));
}
Private void disable toolstripmenuitem_click (Object sender, eventargs E)
{
This. Close ();
}
Private void form1_resize (Object sender, eventargs E)
{
Webbrowser1.width = This. Width-25;
Webbrowser1.height = This. Height-130;
Combobox1.width = This. Width-55;
}
Private void toolstripbutton#click (Object sender, eventargs E)
{
Webbrowser1.gosearch ();
}
}
}
(3) program description: This program only implements a web browser with simple functions. A powerful web browser is being created. It aims to give beginners an interesting role.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.