C# form events

A

Arne Garvander

How do I set an event handler in C#?
The code below does not seem to fire.
private void Form1_Load(object sender, System.EventArgs e)
{
int i = 1;
}
 
J

Jon Skeet [C# MVP]

Arne Garvander said:
How do I set an event handler in C#?
The code below does not seem to fire.
private void Form1_Load(object sender, System.EventArgs e)
{
int i = 1;
}

You need to subscribe to the event, e.g.

Load += Form1_Load;

(in the constructor, for example). In the designer, you can click on
the lightning bolt to make it hook up the events for you.
 
J

Jibesh

event subscription code looks like the this.

//Constructor
Form1()
{
this.Load += new System.EventHandler(this.Form1_Load);
}
private void Form1_Load(object sender, System.EventArgs e)
{
int i = 1;
}
 
K

kimiraikkonen

How do I set an event handler in C#?
The code below does not seem to fire.
private void Form1_Load(object sender, System.EventArgs e)
{
    int i = 1;

}

To register event dynamically:
this.Load new System.Eventhandler(this.Form1_Load);

and subscibing the form's load event handler can simply be done by
double-clicking on your form and it'll be generated by VS for you.

However, you'll notice that, it's stored in Form1.Designer.cs file as
you may want to check out at anytime.

HTH,

Onur Güzel
 

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