Search code examples
c#htmlwindowsweb-controls

Open win form on click of url within a webbrowser control


I have a Windows app where i use webbrowser control to show html text. the problem is that i have some links in that html that i need them to open another win forms when user clicks them.

What I am suppose to write in the link

<a href:????> click to show another win form</a>

or there is another method.please help.

Thanks for your time.


Solution

  • You can execute methods in your windows app by doing the following: First add permission, so add this above public class Form1 : Form

    [PermissionSet(SecurityAction.Demand, Name="FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    

    Second you need to register your webbrowser control to have access to call your form functions, so add this to the Form_Load event

    this.WebBrowser1.ObjectForScripting = this;
    

    Then you can create a Sub in your Main form. The sub MUST be Public.

    public void doOpenForm()
    {
        //your code to open the form here
    }
    

    And in your HTML, do this:

    <a href='' onclick='window.external.doOpenForm()'> click to show another win form</a>