Click here to Skip to main content

Creating Instance of window from from browser contol

Sign Up to vote bad
good
See more: C#.NET
Is it possible to create an instance of window form when using browser control. i.e when i click on link inside web contents i.e
<a href="#">Open Form</a>
.
Posted 19 Feb '12
Edited 23 Feb '12

Comments
BillWoodruff - 20 Feb '12
Please tag your question, WinForms, WPF, ASP.NET, etc. Are you describing: a Windows Forms project where you have a WebBrowser Control on a Windows Form, and you have a link on a web-page open in that WB Control: and when that link is clicked you want to create an instance of Windows Form ? Please confirm that's exactly what you mean here: if it is: 1. Yes, it is possible 2. I believe I have a solution for you (I'm testing it now), and will post it, if I am satisfied it works properly.
Nasir M@hmood - 20 Feb '12
Thanks for your reply i have found solution by setting property webBrowser1.ObjectForScripting = this; of web browser by which we can communicate between browser and form or any other class. if you have better solution please tell me.
BillWoodruff - 20 Feb '12
Hi Nasir, I think I'll go ahead and post my solution (tomorrow, after some sleep), which is kind of a hack anyway, just in case someone can get some value out of it, but it would also interest me very much to know how you are using the ObjectForScripting to solve your problem (I am assuming the "this" you set it to is the WinForm that contains the WebBrowser Control). Maybe you might consider publishing a Tip/Trick here on CP to demonstrate your technique, so everyone can learn from it. thanks (shukran), Bill

Dear Friend,
 
These links will be helpful:-
 
http://www.codeproject.com/Articles/50544/Using-the-WebBrowser-Control-in-ASP-NET[^]
 
http://msdn.microsoft.com/en-us/library/360kwx3z%28v=vs.90%29.aspx[^]
 
Extended .NET 2.0 WebBrowser Control[^]
 
Please don't forget to mark this as your answer if it helps you out.
 
Thanks
  Permalink  
Comments
BillWoodruff - 20 Feb '12
I voted this solution a #3 because: 1. link 1: it's pretty clear this not an ASP.NET question ... although it is certainly worth checking to make sure it is not (see my comment to the OP). 2. link2: Aimed at new users of the Express Edition of Visual Studio, A general article on creating a Windows Form that happens to have some content on adding a WebBrowser of the most general, simple type. A very old article complete with bugs that have been reported in the article, but never fixed. 3. A very old article, and the last answer to a question by the author was over two years ago. The last question asked about this article raises the issue of whether the solution will even work in .NET 4.
Varun Sareen - 20 Feb '12
:(
Here's the "hack" I came up with.
 
1. created a simple text file named "MakeNewWindow.txt" ... it's content doesn't matter here. I put it on the Desktop of my machine.
 
2. created a simple HTML test file named "MakeNewWindow.html" :
<html xml:lang="en" lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>
    <body>
        <a href="C:\Users\Ur\Desktop\MakeNewWindow.txt">Click here</a>
    </body>
</html>
Put that on the Desktop of my machine.
 
Created a simple WinForm test bed project:
 
1. main form, 'form1 :
  a. put a WebBrowser control on it 'webBrowser1
  b. put a TextBox control on it 'textBox1
  c. created a Form_Load EventHandler for 'form1
  d. created a webBrowser1_Navigating EventHandler for 'webBrowser1
 
2. second form 'form2 : just a blank form
 
Here's the code for 'form1:
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
 
    private string HTMLPath=@"C:\Users\Ur\Desktop\MakeNewWindow.html";
 
    private string URLPath=@"C:\Users\Ur\Desktop\MakeNewWindow.txt";
 
    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        // is the navigation target a file ?
        if (e.Url.IsFile)
        {
            // it is a file
            // does the LocalPath property of the file
            // contain the path of the .txt file we have specified ?
            if (e.Url.LocalPath.Contains(URLPath))
            {
                // criteria met: make new instance of Form2, bring to front
                Form2 newForm2 = new Form2();
                newForm2.Show();
                newForm2.BringToFront();
                // cancel any further reaction to the link being clicked
                e.Cancel = true;
            }
        }
    }
 
    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.Navigate(HTMLPath);
    }
}
 
So when the application is launched:
 
1. The Form Load EventHandler of 'form 1 executes
 
  a. 'webBrowser1 navigates to the html file specified, and displays it
 
2. When the user of the running application clicks on the one link in the displayed HTML file:
 
  a. the webBrowser1_Navigating EventHandler is called: see the comments in the code for an explanation of exactly what happens there: which determines whether a new instance of Form2 is created, or not.
 
Feels like kind of a "hack," and I sincerely hope someone will come along with a better answer !
  Permalink  
Comments
Nasir M@hmood - 23 Feb '12
Sorry i was busy little bit that's why can't reply.Dear Sir you are communicating between two controls with certain conditions. I think that's my fault i can't explain my question, actually i want to create instance of form when user click link of web contents for example Open Form .

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

add
  Print Answers RSS
Your Filters
Interested
Ignored
     
  1. OriginalGriff (1,245)
  2. Sandeep Mewara (1,037)
  3. VJ Reddy (545)
  4. SAKryukov (473)
  1. Sandeep Mewara (13,934)
  2. OriginalGriff (12,986)
  3. SAKryukov (8,004)
  4. VJ Reddy (5,145)
  5. losmac (5,065)


Advertise | Privacy | Mobile
Web01 | 2.5.120515.1 | Last Updated 23 Feb 2012
Copyright © CodeProject, 1999-2012
All Rights Reserved. Terms of Use
Layout: fixed | fluid