Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

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...

Posting Guidelines

 

Avatar by Lebb

Posted

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.

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

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...

Posting Guidelines

 

Avatar by Lebb

  • *Experts*
Posted

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

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted
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)

Joe Mamma

Amendment 4: The right of the people to be secure in their persons, houses, papers, and effects, against unreasonable searches and seizures, shall not be violated, and no warrants shall issue, but upon probable cause, supported by oath or affirmation, and particularly describing the place to be searched, and the persons or things to be seized.

Amendment 9: The enumeration in the Constitution, of certain rights, shall not be construed to deny or disparage others retained by the people.

Posted

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:

Posting Guidelines

 

Avatar by Lebb

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...