What's wrong with that code? Webbrowser control doesn't launch

K

kimiraikkonen

Hello experts,
That's very very simple question but i wonder if you can help:

I have form1 and a button which brings user to form2 when button is
clicked, and i have a web-browser (.net 2.0 component) control placed
in form2 which is coded to run when form2 is loaded:

Form1 has only a button brings user to second form, form1 has that
code:

private void button1_Click(object sender, EventArgs e)
{
Form form2 = new Form();

form2.Show();
}


Form2 has that code:

private void Form2_Load(object sender, EventArgs e)
{
this.mybrowser.Navigate("http://www.google.com");
}


But then i run the program, when i go to form2 by clicking "button" in
form1, form 2 is shown empty as if there's no web-browser control
such. I'm beginner for C#, and there must be something missing.

Thanks...
 
G

Guest

Did you attach the load handler routine to the event in the form2
constructor? You should have a line like this:

this.Load += new EventHandler(Form2_Load);

That's the only thing that I can think of at the moment.
 
K

kimiraikkonen

Did you attach the load handler routine to the event in the form2
constructor? You should have a line like this:

this.Load += new EventHandler(Form2_Load);

That's the only thing that I can think of at the moment.


put just before this.mybrowser.navigate("www.google.com")

Do you mean?

private void Form2_Load(object sender, EventArgs e)
{
this.Load += new EventHandler(Form2_Load);
this.mybrowser.Navigate("http://www.google.com");
}

If so, that didn't help.
 
K

Kalpesh

Put the following line, after InitializeComponent()
this.Load += new EventHandler(Form2_Load);

You are trying to attach an event handler in the event, which will not
get raised.

HTH
Kalpesh
 
P

Peter Duniho

Put the following line, after InitializeComponent()
this.Load += new EventHandler(Form2_Load);

You are trying to attach an event handler in the event, which will not
get raised.

And if it did get raised, each time the event got raised, the handler
would get added again, causing a geometrically increasing number of
handler executions for each raising of the event. :)

As far as subscribing the handler goes, IMHO in the case where the
handler is subscribed as part of initialization and never unsubscribed,
you might as well set the handler in the VS Designer rather than
explicitly writing the code to do so.

Pete
 
G

Guest

Peter,

I'm not following where you are suggesting the line goes. I have always put
it in my constructor after the call to InitializeComponent as Kalpesh stated.
The constructor is only called once.

Or... I just realized this. Are you addressing where Kimiraikkonen placed
here add handler code?
 
P

Peter Duniho

Peter,

I'm not following where you are suggesting the line goes. I have always put
it in my constructor after the call to InitializeComponent as Kalpesh stated.
The constructor is only called once.

Or... I just realized this. Are you addressing where Kimiraikkonen placed
here add handler code?

My post addresses two different issues:

1) The consequence of the improperly placed statement subscribing the
handler, as seen in kimiraikkonen's post.

2) The question of whether one should write code to subscribe a
handler at all. For a situation such as this one, the VS Designer
provides a very easy way to auto-generate the needed code, so why write
it oneself at all?

Nothing in my post was suggesting a specific location for the line
being suggested, other than to agree with Kalpesh's post saying that
the OP has it in the wrong place. My preference is to avoid the
question of where the line goes altogether, by having the Designer
write the line of code instead.

Pete
 
G

Guest

Thanks, now I understand!

Peter Duniho said:
My post addresses two different issues:

1) The consequence of the improperly placed statement subscribing the
handler, as seen in kimiraikkonen's post.

2) The question of whether one should write code to subscribe a
handler at all. For a situation such as this one, the VS Designer
provides a very easy way to auto-generate the needed code, so why write
it oneself at all?

Nothing in my post was suggesting a specific location for the line
being suggested, other than to agree with Kalpesh's post saying that
the OP has it in the wrong place. My preference is to avoid the
question of where the line goes altogether, by having the Designer
write the line of code instead.

Pete
 
K

kimiraikkonen

Hi,
Read all the posts but that didn't work neither:

public Form2()
{
InitializeComponent();
this.Load += new EventHandler(Form2_Load);
}

private void Form2_Load(object sender, EventArgs e)
{

this.mybrowser.Navigate("http://www.google.com");
}


It's not required to use that code if there's no form1 / even didn't
work if form1 exists also.
this.Load += new EventHandler(Form2_Load);

if i form2 is unique / only and starter form of the project, i mean,
if i delete form1, form2's webbrowser function will work but
couldn't understand why the problem occurs when you jump from form1 to
form2.
 
P

Peter Duniho

[...]
if i form2 is unique / only and starter form of the project, i mean,
if i delete form1, form2's webbrowser function will work but
couldn't understand why the problem occurs when you jump from form1 to
form2.

You know, the bottom line here is that you have not posted nearly a
good enough problem description for anyone to really answer your
question.

You should post a concise-but-complete sample of code that reliably
demonstrates your problem. You should also be very specific about what
user steps to take when running the code, what the code actually does,
and what you expect it to do instead.

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top