How can I use a webbrowser control as background of a MDI parent

PhatFrogDev
PhatFrogDev used Ask the Experts™
on
I have created a C# winform MDI web browser based on some very helpful tutorials. I want to extend the functionality of this with the following items, but have no idea where to begin. If someone can help me with some code examples, I would be very appreciative.

Here are the things I'd like to do:

1. Use a webbrowser control in the background of the parent, and have the child windows appear over the top of this.

I want to have different backgrounds, HTML, etc., that can be easily changed without having to recompile. I tried this, but the child windows seem to be opening behind the webbrowser control. Any thoughts?

2. I would prefer each time a URL is entered into the Address field, a new child window is created. How do I force this? Currently they open in a new IE window.

3. Can I force a link that has target="_blank", or any command for a new window, to open in a child window, instead of launching an external IE window?

Thanks for any help you can offer.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Rahul Goel ITILSenior Consultant - Deloitte

Commented:
See, you can do apply one approach that you can create a new webbrowser control on child form of the MDI. and size it to the lowest to lowest.. and you can attach event on that and put that control on the background of some visible control.
And you will keep the focus on the same child form

Author

Commented:
So I understand you correctly, are you saying I can create a maximized child form when the application is initialized, then use that as the background? Can the "background" child window be locked into place so it cannot be minimized, closed or moved?
Most Valuable Expert 2012
Top Expert 2008

Commented:
Non-movable form:

http://vaibhavgaikwad.wordpress.com/2006/06/05/creating-a-immovable-windows-form-in-c/

That article talks about overriding WndProc.  You can use the same override to detect, and block other Windows messages, like WM_CLOSE.
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
Would it be better to extend the MDI Client and give it the features I wish to utilize? Is there an alternative to MDI that would enable me to accomplish the 3 items in my list?

Author

Commented:
As for extending the MDI Client, how would one do that, if it's even possible? Or, would that be creating a custom control based on the MDI Client?
Most Valuable Expert 2012
Top Expert 2008

Commented:
What "3 items" are you talking about?  Are you asking for behavior of an MDI parent, without using a WebBrowser control?

Author

Commented:
Actually, I removed one of them, because if the 2 listed below can be done, then I can figure the rest out. I appreciate your time. The edited original question is below:

I have created a C# winform MDI web browser based on some very helpful tutorials. I want to extend the functionality of this with the following items, but have no idea where to begin. If someone can help me with some code examples, I would be very appreciative.

Here are the things I'd like to do:

1. Use a webbrowser control in the background of the parent, and have the child windows appear over the top of this.

I want to have different backgrounds, HTML, etc., that can be easily changed without having to recompile. I tried this, but the child windows seem to be opening behind the webbrowser control. Any thoughts?

2. Can I force a link that has target="_blank", or any command for a new window, to open in a child window, instead of launching an external IE window?

Thanks for any help you can offer.
Most Valuable Expert 2012
Top Expert 2008

Commented:
May I be so bold as to ask what functionality that you are getting (or looking to get) from the WebBrowser, that you can't get a from a Form or PictureBox control?

Author

Commented:
Sure. I want to be able to display HTML, served locally or from a Web server. Now, if using a "locked" child window is the best way to do this, then so be it. But, it seems to me that would be kind of a hokey way to get the result. If not, then that's what I will do, and I can move on to the other item regarding the links and launching them in child windows.
Most Valuable Expert 2012
Top Expert 2008

Commented:
If you want to render HTML to a page, I use a custom control that I can drag and drop from the ToolBox onto a form.

Author

Commented:
Ok. That would work, but I wasn't 100% clear, and I apologize. I would want the control to be able to render HTML, and Javascript, and CSS and anything that could be generated from server-side code as well. In other words, all the capabilities of a modern browser -- as far as rendering is concerned. This means, unless your custom control can do all that, I would need to use a webbrowser control based on IE, or Firefox, or WebKit.

Frankly, except for a few IE specific features, I really don't care which control I use.

Author

Commented:
Keeping in mind, of course, that all this needs to be happening in the background of a MDI Client, or some custom control that has MDI capabilities.

Author

Commented:
AND...each child window will also contain a webbrowser control...

I hope I'm being clear. Let me know if I'm not.
Most Valuable Expert 2012
Top Expert 2008

Commented:
The source for the control came from Lutz Roeder's web site (if you know anything about Reflector):

http://www.lutzroeder.com/dotnet/

Writer
Writer is a simple editor for HTML files. Writer supports text layout and formatting, hyperlink editing and insertion of dates and pictures.

Writer.zip
Most Valuable Expert 2012
Top Expert 2008

Commented:
C# source code attached through Experts-Exchange file storage site:

https://filedb.experts-exchange.com/incoming/ee-stuff/7699-HtmlControl.zip 

Author

Commented:
This control will not display Web pages from a remote Web server.

Author

Commented:
Also, it is throwing an error in VS 2008.

Error      1      Source file 'C:\Users\Kevin\Desktop\htmlcontrol\Properties\AssemblyInfo.cs' could not be opened ('Unspecified error ')      Common.RichTextEditor.HtmlControl
Most Valuable Expert 2012
Top Expert 2008

Commented:
Hmmm...there should be a file reference in the project, which you can remove, and then add an AssemblyInfo.cs file back in.  I didn't realize that was in a sub-folder.  This control should display HTML text from a string, not a URL.

Author

Commented:
Well, I need the ability to display from a URL. There will be ocassions where the URL will have Javascript animations and effects, Flash, etc., so it has to be able to do these as well.
Most Valuable Expert 2012
Top Expert 2008

Commented:
OK, that is more than just rendering HTML (I missed that requirement before), so you are back to the WebBrowser control.  

I believe that the Navigating event handler has a Cancel argument, that you can set to prevent the WebBrowser control from opening a new window.  That way you could create your own form with another WebBrowser control that you can navigate to that new URL.

Author

Commented:
Ok...I think we're getting somewhere, now. So, I use a "locked" child form with a webbrowser control to create the background. Then, I use the cancel argument to stop the webbrowser control from opening an IE window, and open one of my child forms instead. Do you have, or know of somewhere that may have some example code I can see?
Most Valuable Expert 2012
Top Expert 2008
Commented:
My Googling has turned up this:

WebBrowser.Navigating Event
http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.navigating.aspx
private void Form1_Load(object sender, EventArgs e)
{
    webBrowser1.DocumentText =
        "<html><body>Please enter your name:<br/>" +
        "<input type='text' name='userName'/><br/>" +
        "<a href='http://www.microsoft.com'>continue</a>" +
        "</body></html>";
    webBrowser1.Navigating += 
        new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
}
 
private void webBrowser1_Navigating(object sender, 
    WebBrowserNavigatingEventArgs e)
{
    System.Windows.Forms.HtmlDocument document =
        this.webBrowser1.Document;
 
    if (document != null && document.All["userName"] != null && 
        String.IsNullOrEmpty(
        document.All["userName"].GetAttribute("value")))
    {
        e.Cancel = true;
        System.Windows.Forms.MessageBox.Show(
            "You must enter your name before you can navigate to " +
            e.Url.ToString());
    }
}

Open in new window

Author

Commented:
How could I determine what the target of the link is?
i.e. target="_blank", or whatever.

If there isn't a target, or some function that opens a new window, I would want the link to simply go to the URL in the same child window.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial