Priyanka
Priyanka

Reputation: 2832

how to open windows form control in a web page?

I'm developing a web site as well as windows application and i'm going to use same web page for windows form application as well as for web site.

so i want to call the windows form control after clicking on the link which is placed on the web page. And i want to show the windows form control as a pop up form.

How to do this? Or provide some document related to this issue.

Upvotes: 1

Views: 7376

Answers (3)

vibs2006
vibs2006

Reputation: 6548

You can display Windows Form in Web Browser (Internet Explorer Preferred) by using WPF Browser Application which have a facility to embed Windows Forms (or similar like .NET Components).

This is a valid Question because there are lots of Legacy .NET Applications which company don't have resources to be developed for Web HTML 5 format (using AJAX etc). Such legacy applications can be easily displayed in a web browser with a minor initial setup required.

  1. Open your Windows Form Application in your existing Visual Studio IDE
  2. Go to Project Properties and in Application Settings Set output Type to 'Class Library' enter image description here
  3. Add a new project of Type 'WPF Browser Application' as shown below enter image description here
  4. Reference your Windows Form (Now as DLL) along with two important assemblies of WindowsFormIntegration and System.Windows.Forms
  5. Write the code to call Windows Form in XAML and XAML.CS page as shown in this tutorial link.
  6. Rebuild the Solution and Click on Publish and Save select File System as shown below. enter image description here
  7. Go to Publish Folder and double click file with extension '.xbap. It should get automatically opened in Internet Explorer with permissions asking for the first time.
  8. You should be able to display the output in Web Browser. enter image description here

Let me know if you want me to create a video tutorial to achieve the same.

Upvotes: 0

Jeremy Thompson
Jeremy Thompson

Reputation: 65712

I think your getting confused - or we are.

"I have web page which i designed for web application and i'm going to open the same web page in the web browser control which will be placed on the one of form of windows form application. And if i open the form which is having my web site page and i clicked on the link from the web page then that time it should open the another form from the windows application"

So you know the link that will be clicked? Then in the WebBrowser Navigating Method check if its the link you were expecting and open the WinForm, eg:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
if (e.Url.Tostring() == "http://localhost/LinkThatOpensFormTwo.htm")
{
Form2 f = new Form2();
f.Show();

Upvotes: 0

Rob
Rob

Reputation: 45789

Assuming you mean that within your WinForms application you're hosting an instance of the WebBrowser class, you can provide an object to its ObjectForScripting property. that provides a method to call to trigger your WinForms code. For example:

public partial class MyWindowsFormsForm()
{
    public MyWindowsFormsForm()
    {
        this.WebBrowserControl.ObjectForScripting = this;
    }

    public void DoSomething()
    {
        MyOtherForm f = new MyOtherForm();
        f.Show();
    }
}

Then, in your page:

<script language="javascript" type="text/javascript">
function loadOtherForm()
{
    if (RunningInWinFormsApplication())
    {
        window.external.DoSomething();
    }
    else
    {
        // Code to do something when NOT running inside the WinForms app could go here
    }
}

function RunningInWinFormsApplication()
{
   return (window.external.DoSomething != undefined);
}
</script>

<button onclick="loadOtherForm();">Call into WinForms app</button>

There's the obvious caveat that you'll need to have code in your web page that checks to ensure that window.external.DoSomething is actually there, so likely your onclick (in this example) would call a helper method that either calls into WinForms, or does whatever needs doing in the event that the page isn't being hosted inside your application.

Upvotes: 1

Related Questions