tnl700

Heya guys.

I have a few question regarding to building a C# web browser using the web browser tool in VS C# Express. I was hoping you could answer me. I'm really new so I'd appreciate code examples or not too complex explanations.

1. When I enter my web browser application a splash screen I built pops up. I want it to pop up only the first time I enter the application and then, when I want to open more windows of this same app or so, I'll get it straight away with no splash screen (I created the splash screen for visual effect only, I don't really require it).

2. When I navigate to a site I want to get its title. For example "Google search: bla bla". Just like in all decent web browsers. I found out that when you hit properties on a page it gives you the title of it. How can I access this info and how can I throw it into my form name

3. The problem that annoys me the most is when I hit links that need to open in new windows in my own browser, they open up in IE. I have no idea how to fix this one. I would also like my app to open another window when I right-click a link and hit Open In New Window. How can I do this (I have a feeling I'll have a rough time understanding your suggestions so remember I'm a newbie).

If it would help I could send you the solution and you'll get to see the mess I wrote here.

That's about it.. I know it's a lot but I'll be grateful for anyone who will help me with those problems.

Thanks,

tnl.



Re: Visual C# Express Edition web browser stuff..

jrboddie

I will address questions 2 and 3 for you:

To show the page title in the form¡¯s title bar:

Hook up a Document_Completed event handler for the webBrowser and put this code into it:

this.Text = webBrowser1.DocumentTitle;

(To create a Document_Completed event handler, go to the Design view and select the webBrowser control. Then in the properties window, click on the lightning bolt icon which should show a list of events for the browser. Find the one labeled ¡®DocumentCompleted¡¯ and double-click it. This should take you back to code view and the event handler will be added. Put the code listed above into the handler.)

To prevent IE from opening a new window you need to intercept the NewWindow event. I presume you want to open a new window with another instance of your webBrowser form. And you want to be able to do the same by right-clicking a link.

Hook up a NewWindow event handler using the same technique above and add the following code:

Form1 f1 = new Form1(lastUrl);

f1.Show();

e.Cancel = true;

This creates a new Form1 (I am assuming that this is the form that your browser control is on), passes a string called ¡®lastUrl¡¯ (more on this later). Launches the form and cancels the event. So, whenever the browser needs to create a new window, this routine will make a new browser for you and stop the event from launching a window in IE.

Now, we need to make Form1 display the web page that we just passed to it. Make the constructor of Form1 look like this:

public Form1(string url)

{

InitializeComponent();

webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);

webBrowser1.Navigate(url);

}

This makes Form1 receive a string url and uses it to navigate the webBrowser whenever it is created. The webBrowser1.StatusTextChanged ¡­ line hooks up an event handler that is not in the list of handlers that you can choose from in the properties window. So you will have to add this event handler like this:

string lastUrl;

void webBrowser1_StatusTextChanged(object sender, EventArgs e)

{

string s0 = webBrowser1.StatusText;

if (s0.Contains("http"))

{

lastUrl = s0;

}

toolStripStatusLabel1.Text = s0; //optional

}

The status text is what is displayed on the status strip at the bottom of most browsers. When you hover your mouse over a url, you will see this change. This bit of code looks for changes in this status and remembers the last bit of status that shows a url in the string variable ¡®lastUrl¡¯. This is the variable that we use to launch any new window either automatically or by right-clicking and selecting the ¡®open in new window¡± option from the context menu. Optionally, you can add a toolstrip wit a toolStripStatusLabel to the bottom of your form and display the status like most browsers.

Finally, one more bit of housekeeping. Assuming Form1 is the first form your program launches (it may not be since you have a splash screen), you will need to make the call to Form1 in the Program.cs file match your constructor. Change the Application.Run(new Form1()); line to:

Application.Run(new Form1(""));

This will provide the string to Form1 that the constructor is expecting.

I hope this explanation was clear. Here is the complete listing of my Form1 which has a webBrowser, textbox, button, and toolStripStatusLabel:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

namespace WindowsApplication1

{

public partial class Form1 : Form

{

public Form1(string url)

{

InitializeComponent();

webBrowser1.StatusTextChanged += new EventHandler(webBrowser1_StatusTextChanged);

webBrowser1.Navigate(url);

}

string lastUrl;

void webBrowser1_StatusTextChanged(object sender, EventArgs e)

{

string s0 = webBrowser1.StatusText;

if (s0.Contains("http"))

{

lastUrl = s0;

}

toolStripStatusLabel1.Text = s0;

}

private void button1_Click(object sender, EventArgs e)

{

webBrowser1.Navigate(textBox1.Text);

}

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)

{

this.Text = webBrowser1.DocumentTitle;

}

private void webBrowser1_NewWindow(object sender, CancelEventArgs e)

{

Form1 f1 = new Form1(lastUrl);

f1.Show();

e.Cancel = true;

}

}

}





Re: Visual C# Express Edition web browser stuff..

tnl700

jrboddie hello!

As for the page title, thanks! It worked perfectly!

However, I'm having problems with the new window code.
When I right click a link and hit 'open in new window' it works fine, but there are links with the javascript openinnewwindow thing (I can see it in the link properties). It's those links that open even if you hit the normally and I can't seem to hit them. It says that there's a script error, Something like this:
An error has occurred in the script on this page.
Line: 106
Char: 89
Error: 'wref' is null or not an object

It asks me if I want to continue running scripts on that page. Either way it opens a new app window that says Navigation Canceled.

Do you think the problem is with that javascript code in those links Am I doing something wrong

Edit: Do you know how can I make my splash screen not appear when that new instance, f1, opens, or is it the same thing I asked in question 1

Thank you very much,
tnl.




Re: Visual C# Express Edition web browser stuff..

jrboddie

cnn.com has some links in their ads that open in a new window. These work ok for me. Perhaps you can show me a link that demonstrates the problem Also you might try to set the ScriptErrorsSuppressed property to true in the webBrowser.

Regarding the splash screen, I can't say because I don't know how you are doing it.





Re: Visual C# Express Edition web browser stuff..

tnl700

Sheesh..some weird things are happening here..

First of all if I hit an ad on CNN it sometimes tells me Navigation Canceled (without the error message now, probably because I turned ScriptErrorsSupressed on) and sometimes just takes me to a random CNN page :\
When he tells me Navigation Canceled I get nothing in the URL textbox so I tried to go to page properties. It tells me that the page has the following address:
res://ieframe.dll/navcancl.htm#http://waiting%20for%20http//edition.cnn.com/.element/js/2.0/scripts/IEPersistence.html...

Also, sometimes when I hit Open In New Window it works and other times gives me Navigation Canceled. I think I get that when the page is still loading, but when the status bar says 'Done' I can safely use the Open In New Window.
Could it be because we're using the status bar to determine the URL Maybe when the page loads it changes and ruins the whole thing

I'm really lost here..
Could we perhaps continue to discuss about this via mail I don't know if a private talk here would fit well. my address is tnl700@ gmail .com

Meanwhile I'll just try links and see what I get. I'll edit this message.

Edit: I tried opening a link in a new window in deviantart.com. When the page is still loading I get Navigation Canceled and the new window's address property is: res://ieframe.dll/navcancl.htm#http://(3%20items%20remaining)%20downloading%20picture%20http//tn1-2.pv.deviantart.com/fs19/150/i/2007/306/e/f/November_by_chicuta.jpg...
If I try again after the page is fully loaded it works fine.

I really thank you for spending time helping me and I'm sorry for being so helpless.
tnl.




Re: Visual C# Express Edition web browser stuff..

jrboddie

I sent you an email with the suggestion to

Try replacing

if(s0.Contains(¡°http¡±))

with

if(s0.StartsWith(¡°http¡±))