Where are the Form Events in C#.Net?

Mike_R

Junior Contributor
Joined
Oct 20, 2003
Messages
316
Location
NYC
Hi, sorry for the ridiculously Newbie question... But I'm first getting my feet wet with C#...

I can't seem to find how to create the Form Events in C#. In VB.Net I would choose 'Form1' from the 'Class Name' Drop Down box and then find the Event I wanted within the 'Method Name' Drop Down.

But it does not seem to work the same in C#.Net. I couldn't find any of the Form's events and could only create the Form1_Load() Event by double-clicking the Form within the Designer View.

I tried typing in manually:
Code:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
    MessageBox.Show("Form1_Closing() was called.");
}
But it did not actually fire... This must be easy but something eludes me. :(

Much thanks in advance...
 
events are on a tab in the properties window in the IDE. . . Select the control for which you want to create an event handler. Click the lightning bolt and double click in the event you want to handle.

BTW, this is the way it is done in virtually ever other OO IDE I have seen. VB is the odd man out.
 
Huh, interesting... So I have to choose the Events within the Design View. Feels a bit odd, but, since, as you say, it is really VB that's "odd", I'll just have to get used to it! :)

I thank you very much...
 
You can add events in code if you want. If you want them recognized in the Designer, add them in the InitializeComponent method. If you type something like:
this.textBox1.Click +=

You can then press Tab twice to add the rest of the line and the default function itself. It's not quite as "easy" as VB6, but I still like it. I mostly use the designer to add events as there aren't generally that many to add (maybe 2 dozen on one form and that's only a one time thing).

As with VB, you can double click a control in the designer to add a default event (button's get a click event, textbox gets TextChanged, etc.).

-ner
 
Nerseus said:
You can add events in code if you want. If you want them recognized in the Designer, add them in the InitializeComponent method. If you type something like:
this.textBox1.Click +=
-ner
actually the syntax is:

SomeClass.SomeEvent += new SomeEventHandler(SomeDelegate);

in the case of Textbox.Click, it world be:

aTextBox.Click += new EventHandler(MyDelegate);

where my delegate is protoyped:

void MyDelegate(void sender EventArgs e)

There are more event handler definitions other than the simple EventHandler.

Get Lowy's Programming .NET Components (O'Rielly)
 
Thank you both for that...

Testing it out, typing in

this.textBox1.Click +=

and then two <Tab> hits works perfectly, as IntelliSense/AutoComplete fills in the rest for you. (Is this called "IntelliSense" still or is "AutoComplete" the new terminology now?)

Thanks a lot guys... very neat. :cool:
 
Back
Top