Click here to Skip to main content
15,890,123 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

Is it possible to pass and get values to a web-browser embedded in winform?
if yes, can you point me to any reference or code sample

thanks
Posted

1 solution

There are ways using COM objects which I find complicated because on the client side you must use some <object> tags and it is using com behind the scene.

I prefer calling a scripts to speak to the page from C#
C#
private void RunScript(string cmd, params object[] args)
{
   var doc = WebBrowser1.Document;
   if (doc != null) doc.InvokeScript(cmd, args);
}

This unfortunately won't return anything.
To get data back you can either make a call to a local TCP port. If your program already must provide pictures or pages it is ideal.
If you prefer you can use javascript to change the title. (this is also using jQuery in this sample):
JavaScript
$(".button").click(function () {
   document.title = "#click:" + $(this).attr('id');
});

Then it is easy in your program
C#
void webBrowser1_DocumentTitleChanged(object sender, EventArgs e)
{
   if (webBrowser1.DocumentTitle.StartsWith("#click:"))
   {
      ...
   }
}
</object>
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900