SoFunction
Updated on 2025-03-07

Common usage examples of C# using webbrowser

This article describes the common usage of C# using webbrowser. Share it for your reference. The details are as follows:

Determine whether the network is normal

private bool IsConnectedToInternet()
{
 int Desc;
 return (out Desc, 0);
} 

Set the default page:

Copy the codeThe code is as follows:
(new Uri("/"));

Login code:
private void btnlogin_Click(object sender, EventArgs e)
{
 ("id").InnerText =().Trim();//fill name
 ("passwd").InnerText =().Trim();//fill pwd
 HtmlElement formLogin = ["frmLogin"];
 ("submit");
 (5000);
 herfclick("/");//This is a successful login operation, jump to // //Note that it is not just skipped, simulate clicking on the link //SESSION will not be lost herfclick("/"); 
}

Simulation click link:

private void herfclick(string url)
{
 for (int i = 0; i < ; i++)
 {
  if ([i].TagName == "A" && [i].GetAttribute("href").ToString().Trim() ==url)
  {
   [i].InvokeMember("click");//Raises the "CLICK" event   break;
  }
 }
 
}

Run JS functions in web pages

private void callJsMethod(string jsfun,string jsParameter)
//Function name, function parameters{
 HtmlDocument doc = ;
 (jsfun,new object[]{jsParameter});
}

Fill in data:

private void filldate(string txt)
{
 HtmlElement frmWord = ["frmWord"];//Finish from HtmlElementCollection txtarea = ("TEXTAREA");
 HtmlElementCollection radio = ("INPUT");
 radio[2].InvokeMember("click");//Select radio control txtarea[0].InnerText = ();
}

I hope this article will be helpful to everyone's C# programming.