C WebBroeser control usage example tutorial

  • 2020-10-31 21:58:03
  • OfStack

WebBrowser is a very practical control in C#. This article analyzes the use of WebBrowser in the form of examples for your reference. The specific analysis is as follows:

1. Common WebBrowser attributes:


.Refresh();// The refresh 
.GoBack();// back 
.GoForward();// forward 
.Navigate(new Uri(address)); // Open the link 
.Dock = DockStyle.Fill; // Dock completely in the parent container 
.ScriptErrorsSuppressed = true ; // Turn off error display 
Uri a = new Uri(WebBrowser.Document.ActiveElement.GetAttribute("href"));// Gets the link address in the click 

2. WebBrowser Commonly used event handling:


.Navigated += new WebBrowserNavigatedEventHandler(WebBrowser_Navigated);// Handles events in load 
.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser _DocumentCompleted);// Page load completes processing events 
.NewWindow += new CancelEventHandler(WebBrowser _NewWindow);// Open the page in a new window 

3. Event handler

1. Open a new connection:


// Navigates to the given URL if it is valid.
private void Navigate(WebBrowser web,String address)
{
  if (String.IsNullOrEmpty(address)) return;
  if (address.Equals("about:blank")) return;
  if (!address.StartsWith("http://")) address = "http://" + address;
  try
  {
 WebBrowser.Navigate(new Uri(address));
    
  }
  catch (System.UriFormatException)
  {
 return;
  }
  // Updates the URL in TextBoxAddress upon navigation.
  private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
  {
   toolStripTextBox1.Text = WebBrowser.Url.ToString();
  }
}

2. New window processing:


private void WebBrowser_NewWindow(object sender, CancelEventArgs e)
{
  WebBrowser webb = new WebBrowser();
 
  WebBrowser = WebBrowsers[webtab.SelectedIndex];
  webb.Name = "webb" + webtab.TabCount;
  Uri a = new Uri(WebBrowser.Document.ActiveElement.GetAttribute("href"));    
  webb.Url = a;
  webb.Dock = DockStyle.Fill;
  TabPage p = new TabPage();      
  p.Controls.Add(webb);
  webtab.TabPages.Add(p);
  webtab.SelectedTab = p;
  webb.NewWindow += new CancelEventHandler(WebBrowser_NewWindow);
  
  e.Cancel = true;// Unopen in the default browser     
}

3. Automatic login of users:


#region  User login automatically 
private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
   HtmlDocument log_auto = WebBrowser.Document;
   HtmlElement log_btn = null;
 
    foreach (HtmlElement em in log_auto.All) // Round robin 
    {
       string str = em.Name;
       string id = em.Id;
       if ((str == "username") || (str == "password") || (str == "submit")) // Reduce processing 
       {
          switch (str)
          {
            case "username": em.SetAttribute("value", usernamexml);
              break; // Assign a username 
            case "password": em.SetAttribute("value", userpasswordxml );
              break; // Assign a password 
            case "submit": log_btn = em;
              break; // To obtain submit button 
            default:
              break;
          }
        }
 
      }
      log_btn.InvokeMember("click"); // The trigger submit The event         
    }
#endregion

I believe that this article has a certain reference value for the learning of C# programming.


Related articles: