C# form events

  • Thread starter Thread starter Arne Garvander
  • Start date Start date
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;
}
 
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.
 
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;
}
 
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
 
Back
Top