Event handling question in C#

  • Thread starter Thread starter Marinos Christoforou
  • Start date Start date
M

Marinos Christoforou

Sorry if this has been asked before but as an inexperienced wanna-be C#
programmer I wondering how to code classes to help build a standard Windows
UI.

For example to build a common toolbar. I would call a method from the class
in the load event of my varioius windows forms, to create the toolbar, add
buttons to it and set various properties as required. This is fine but I am
at a loss as to how to expose the toolbar's various events (eg. button click
event) to the client form. Can anybody please help?
 
Marinos,

Are you using VS.NET? If so, place a menu bar on the form and double
click it. This will generate a function which will handle an event.

Now, if you look at the section of code hidden by the designer (the
InitializeComponent method), you will be able to see how that event is
hooked up.

Generally, to hook a method up to an event that an object fires, you
have to assign a delegate. Say you need to handle a click event for a
button. You would have to define a method, like this:

// The event handler for the button click event.
private void ButtonClickEventHandler(object o, EventArgs e)
{
// Do something.
}

The accessibility of the method is up to you, but the signature must
handle the delegate type of the event you want to hook up to. In this case,
the delegate is of type EventHandler, so you must match that signature.
Then, on the button, you must assign the event handler:

// mobjButton is the button, and Click is the event:
mobjButton.Click += new EventHandler(this.ButtonClickEventHandler);

Notice how the EventHandler takes the method name.

For more information on Events and Delegates, check out the section of
the .NET framework titled "Events and Delegates", located at (watch for line
wrap):

http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconeventsdelegates.asp

Hope this helps.
 
Thanks Nicholas,

On the whole I have no problems comprehending and using the information you
provide regarding programmatically handling events form. What I am having
difficulties with is best described with an example of the type of thing I
am trying to do in C#.

Consider a class you can call to draw a text box.

class myTextBox {
private TextBox _txtBox ;
public myTextBox {
// constructor instantiates and sets various textbox properties
}
}

So far so good. Now I want to expose the click event of the text box and I
am not sure how best to do it. without declaring the _txtBox object as
public (if possible).

I thought about a public property :

public EventHandler TextBoxClickEvent {
get {
return _txtBox.Click ;
}

but the compiler complains that:

The event 'System.Windows.Forms.TextBox.Click' can only appear on the left
hand side of += or -=

How can I best expose some of the events of the private member textbox, to a
client using the class?

Nicholas Paldino said:
Marinos,

Are you using VS.NET? If so, place a menu bar on the form and double
click it. This will generate a function which will handle an event.

Now, if you look at the section of code hidden by the designer (the
InitializeComponent method), you will be able to see how that event is
hooked up.

Generally, to hook a method up to an event that an object fires, you
have to assign a delegate. Say you need to handle a click event for a
button. You would have to define a method, like this:

// The event handler for the button click event.
private void ButtonClickEventHandler(object o, EventArgs e)
{
// Do something.
}

The accessibility of the method is up to you, but the signature must
handle the delegate type of the event you want to hook up to. In this case,
the delegate is of type EventHandler, so you must match that signature.
Then, on the button, you must assign the event handler:

// mobjButton is the button, and Click is the event:
mobjButton.Click += new EventHandler(this.ButtonClickEventHandler);

Notice how the EventHandler takes the method name.

For more information on Events and Delegates, check out the section of
the .NET framework titled "Events and Delegates", located at (watch for line
http://msdn.microsoft.com/library/d...y/en-us/cpguide/html/cpconeventsdelegates.asp

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Marinos Christoforou said:
Sorry if this has been asked before but as an inexperienced wanna-be C#
programmer I wondering how to code classes to help build a standard Windows
UI.

For example to build a common toolbar. I would call a method from the class
in the load event of my varioius windows forms, to create the toolbar, add
buttons to it and set various properties as required. This is fine but I am
at a loss as to how to expose the toolbar's various events (eg. button click
event) to the client form. Can anybody please help?
 
Back
Top