Monday 6 February 2012

WebBrowser Example

This an example of the Web Browser Control using Windows Form Application Framework 3.5. One of the primary demands for the UI is the ability to have only a single instance of the application running but to be able to have multiple Web pages open that the user can rapidly switch between without having to navigate back and forth.


The goals of this component are:

  • Implementing the WebBrowser Control
  • Having the following button: back, forward, home, refresh and stop
  • Being able to bookmark the web page (Favorite)
  • Being able to have multiple Web pages open that the user can rapidly switch between without having to navigate back and forth (Use container controls)
In the next section, I will explain the coding details in a short form.

Implementing the WebBrowser Control
In the implementation of the Web Browser, I use a Tab Control, that I explain below. In the TabPage I will load each website. In the project I give an option to add new tab, or close tab.
private void NewWeb()
{    // Add a new tab control, and load the web control in it.
    // Define a maxium of 5 tabs, you can change the number.
    if (currentTabCount == 5) return;
   
    // Create the new tab page
    TabPage newpage = new TabPage("Loading...");  
    tabControl1.TabPages.Add(newpage);               
    // Keep track of how many pages are open.
    currentTabCount++;
    // Create a new WebBrowser control.
    WebBrowser webpage = new WebBrowser();        
    webPages.Add(webpage);
    webpage.Parent = newpage;
    webpage.Dock = DockStyle.Fill;

     // When the site has been displayed, and updated the name of the Tab with the site title.
     webpage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webpage_DocumentCompleted);
     // Select the new site as the foreground
     tabControl1.SelectedTab = newpage;
}


Have the following button: back, forward, home, refresh and stop
I use the toolStripButton control where I place all the button for the browser. It gives facilities to add an image and/or a text. In the code-behing, I implemented these snipets.

private  void toolStripButtonForward_Click(object sender, EventArgs e)
{  
    // Go Forward - if the web control can go back.
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    if (thiswebpage.CanGoForward)
        thiswebpage.GoForward();
}

private void toolStripButtonBack_Click(object sender, EventArgs e)
{    // Go Back - if the web control can go back.
    WebBrowser thiswebpage = GetCurrentWebBrowser();
     if (thiswebpage.CanGoBack)
        thiswebpage.GoBack();
}

private void toolStripButtonGoHome_Click(object sender, EventArgs e)
{    // Go Home - go to the default home page.
    toolStripComboBox1.Text = "about:blank";
    // Update the Tab Control too
    tabControl1.SelectedTab.Text = "Blank Page";
   WebBrowser thiswebpage = (WebBrowser)webPages[tabPages.IndexOf(tabControl1.SelectedTab)];  
    thiswebpage.Navigate("about:blank");}

private void toolStripButtonRefresh_Click(object sender, EventArgs e)
{    // Refresh - cause the browser to reload and redisplay the current page
    WebBrowser thiswebpage = GetCurrentWebBrowser();
     thiswebpage.Refresh();
}

private void toolStripButtonStop_Click(object sender, EventArgs e)
{
    
WebBrowser thiswebpage = GetCurrentWebBrowser();
     thiswebpage.Stop();
}

Being able to bookmark the web page (Favorite)
In this case, I use a TreeView Control to bookmark my favorites pages. By default, the root is named Favorites, but you can create your own groups. I created another form and the user can select the node to save the page.

private void toolStripButtonAddFavorite_Click(object sender, EventArgs e)
{    
      if (GetCurrentWebBrowser().Url != null)
     {
         // Show the AddFavorites dialog
         AddFavorites dlgFavorite = new AddFavorites(GetCurrentWebBrowser().Url.ToString());
         TreeNodeCollection nodes = this.treeViewFavorite.Nodes;
       foreach (TreeNode node in nodes)
           dlgFavorite.comboBoxFFolder.Items.Add(node.Text);
      DialogResult resFavorite = dlgFavorite.ShowDialog();
      bool findNode = false;

       // If the user clicks OK
      if (resFavorite == DialogResult.OK)
     {
           
        if (dlgFavorite.comboBoxFFolder.Text.Equals("Favorites"))
        {
             TreeNode newChild = new TreeNode(dlgFavorite.textBoxFName.Text);
             treeViewFavorite.Nodes[0].Nodes.Add(newChild);
             newChild.ToolTipText = dlgFavorite.textBoxFName.Text;
             treeViewFavorite.ExpandAll();
         }
         else
        {
             // Search if the node exists, add the child
             foreach (TreeNode node in treeViewFavorite.Nodes)
             {
                 if (node.Text == dlgFavorite.comboBoxFFolder.Text)
                {
                    // Node exist, add the new child
                    node.Nodes.Add(new TreeNode(dlgFavorite.textBoxFName.Text));
                    node.ToolTipText = dlgFavorite.textBoxFName.Text;
                    treeViewFavorite.ExpandAll();
                    findNode = true;
                    break;
                }
            }
            if (!findNode)
            {               
                // Node doen't exists, add the parent Node and the child
               TreeNode newNode = new TreeNode(dlgFavorite.comboBoxFFolder.Text);
               TreeNode newChild = new TreeNode(dlgFavorite.textBoxFName.Text);

               newNode.Nodes.Add(newChild);
               newChild.ToolTipText = dlgFavorite.textBoxFName.Text;
               treeViewFavorite.Nodes.Add(newNode);
               treeViewFavorite.ExpandAll();
           }

           dlgFavorite.Close();
       }
     }
   }
}

Being able to have multiple Web pages open
I use in the project a TabControl that allow the user rapidly switch between  sites without having to navigate back and forth. The code to use is below.

private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{    if (GetCurrentWebBrowser().Url != null)
         toolStripComboBox1.Text = GetCurrentWebBrowser().Url.ToString();
}


private WebBrowser GetCurrentWebBrowser()
{
    
// This method returns the currently display WebControl, use
    // an array to find the active one by getting the index of the current tab.
    TabPage current_tab = tabControl1.SelectedTab;
    WebBrowser thiswebpage = (WebBrowser)webPages[tabPages.IndexOf(current_tab)];
    return thiswebpage;
}

The sample application and source
The sample application demonstrate the use of differents controls to show the WebBrowser Control functionality. The code is commented, and hopefully gives enough information for helping you put your own solution together. I am able to shareand email you all the source, please let me know.

This is my first blog. Thanks for reading! If you can add any comment or have suggestions or tips, please post a message below.  I would appreciate that.

 

About the Author:

Jenny Coca
. NET Software/Web Developer
I was born in Havana, Cuba
I live in Toronto, Canada
Microsoft Certified Professional Developer (Windows and Web)
MCITP on Microsoft SQL Server 2005 (Database Developer and Administrator)
I work with C#, VB.Net and PHP
Share is the key!


1 comment:

  1. Unknown 11 January 2013 at 05:33

    I need the codes and step by step procedure please

    Reply Delete
Add comment

Newer Post Home

两个鬼故事怎么免费给宝宝起人名电力行业公司起名褚起名有火字取名起名大全是谁杀了我木鬼衣江姓女宝宝起名2021铬金铠甲拉莫斯易经给类公司起名送给宝宝免费起名软件单姓起名黄起名字女孩好听顺口用逸字怎么起名字安康这个词不能随便用神奇燕尾服教学计划模板起名的禁忌万能声卡驱动健康之类的生活馆起名楚辞取名男孩鄞州区卫生局陶瓷店面起什么名好时空旅行者和他的女儿如何给车起名字shuame工作作风扎实起名用的好字有哪些字金色年华ktv诸葛神算三藏起名起姓名诗词山水画名字怎么起少年生前被连续抽血16次?多部门介入两大学生合买彩票中奖一人不认账让美丽中国“从细节出发”淀粉肠小王子日销售额涨超10倍高中生被打伤下体休学 邯郸通报单亲妈妈陷入热恋 14岁儿子报警何赛飞追着代拍打雅江山火三名扑火人员牺牲系谣言张家界的山上“长”满了韩国人?男孩8年未见母亲被告知被遗忘中国拥有亿元资产的家庭达13.3万户19岁小伙救下5人后溺亡 多方发声315晚会后胖东来又人满为患了张立群任西安交通大学校长“重生之我在北大当嫡校长”男子被猫抓伤后确诊“猫抓病”测试车高速逃费 小米:已补缴周杰伦一审败诉网易网友洛杉矶偶遇贾玲今日春分倪萍分享减重40斤方法七年后宇文玥被薅头发捞上岸许家印被限制高消费萧美琴窜访捷克 外交部回应联合利华开始重组专访95后高颜值猪保姆胖东来员工每周单休无小长假男子被流浪猫绊倒 投喂者赔24万小米汽车超级工厂正式揭幕黑马情侣提车了西双版纳热带植物园回应蜉蝣大爆发当地回应沈阳致3死车祸车主疑毒驾恒大被罚41.75亿到底怎么缴妈妈回应孩子在校撞护栏坠楼外国人感慨凌晨的中国很安全杨倩无缘巴黎奥运校方回应护栏损坏小学生课间坠楼房客欠租失踪 房东直发愁专家建议不必谈骨泥色变王树国卸任西安交大校长 师生送别手机成瘾是影响睡眠质量重要因素国产伟哥去年销售近13亿阿根廷将发行1万与2万面值的纸币兔狲“狲大娘”因病死亡遭遇山火的松茸之乡“开封王婆”爆火:促成四五十对奥巴马现身唐宁街 黑色着装引猜测考生莫言也上北大硕士复试名单了德国打算提及普京时仅用姓名天水麻辣烫把捣辣椒大爷累坏了

两个鬼故事 XML地图 TXT地图 虚拟主机 SEO 网站制作 网站优化