473,386 Members | 1,621 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Event

Hi there

I build a class "ButtonClass" with two private
Buttons, "OK" And "Cancel".
Now, I would like to use this class in a Windowsform for
example three times.

ButtonClass c1 = new ButtonClass();
ButtonClass c2 = new ButtonClass();
ButtonClass c3 = new ButtonClass();

I assume the EventListener and its function is in the
ButtonClass. How do I know know which Button was pressed,
because I have just one Listener for each of the Buttons?

Thanks for helping

Thomas
Jul 19 '05 #1
2 1564
Hi Mikael

Thank you for helping. I'll try it this evening.

-----Original Message-----
Hi Thomas!

In your case your buttonclass would have to throw events that is picked upby your main class.

First of you need to create delegates that tells everyone what the eventshould look like. Place this outside your class.

public delegate void OkClickEventHandler(object sender, EventArgs e);public delegate void CancelClickEventHandler(object sender, EventArgs e);
public class ButtonClass
{
//Now inside your buttonclass you add the events so that they can befired.
public event EventHandler OkClick;
public event EventHandler CancelClick;

//This is not really nessessary to have, but I usually have it anywaysince it
//keeps things simple and that you might want to change the event a bitif
//you inherit from the ButtonClass

protected virtual void OnOkClick(EventArgs e)
{
if (OkClick != null) //If someone is ready to handle the event {
OkClick(this, e); //Fire the event
}
}

protected virtual void OnCancelClick(EventArgs e)
{
if (CancelClick != null) //If someone is ready to handle the event {
CancelClick(this, e); //Fire the event
}
}

private void okButton_Click(object sender, System.EventArgs e) //Thebasic event fired by clicking the button
{
OnOkClick(EventArgs.Emtpy); //Try to fire the event if someone islistening for it
}

private void cancelButton_Click(object sender, System.EventArgs e) {
OnCancelClick(EventArgs.Empty);
}
}

In your main class you need to listen for the events that the buttonclassfires. Unless you do this you will never know when a button i pressed. So inyour main class you need to do this

ButtonClass bc01 = new ButtonClass();
bc01.OkClick += new EventHandler(bc01_OkClick); //Start listening for okbutton click in buttonclass bc01
bc01.CancelClick += new EventHandler(bc01_CancelClick);

ButtonClass bc02 = new ButtonClass();
bc02.OkClick += new EventHandler(bc02_OkClick);
bc02.CancelClick += new EventHandler(bc02_CancelClick);
And so on.

Finally you need a method that handles the events that has been fired
private void bc01_OkClick(object sender, EventArgs e)
{
//Do what you want to do if they press OK in bc01
}

This way you know exactly which ButtonControl is throwing the event andwhich button in the control also.

I hope this helps you out. Personally I got tired of doing all these stepsso I made a smal event generator that does this stuff for me. Just copy andpaste, which is quite nice and timesaving :)

//Mikael

"Thomas" <th***********@freesurf.ch> wrote in message
news:0a****************************@phx.gbl...
Hi there

I build a class "ButtonClass" with two private
Buttons, "OK" And "Cancel".
Now, I would like to use this class in a Windowsform for example three times.

ButtonClass c1 = new ButtonClass();
ButtonClass c2 = new ButtonClass();
ButtonClass c3 = new ButtonClass();

I assume the EventListener and its function is in the
ButtonClass. How do I know know which Button was pressed, because I have just one Listener for each of the Buttons?
Thanks for helping

Thomas

.

Jul 19 '05 #2
Hi,

I maybe wrong here but from what I gather you have a class called
ButtonClass and this class implements the click event. Now, you have created
three ButtonClass objects and want to know which button was pressed in the
click event handler.. I assume you can use the this keyword in the
ButtonClass which will refer to the current object that raised the click
event...

Again, I may have made a mistake in understanding your query..

-Prateek

"Thomas" <th***********@freesurf.ch> wrote in message
news:0a****************************@phx.gbl...
Hi there

I build a class "ButtonClass" with two private
Buttons, "OK" And "Cancel".
Now, I would like to use this class in a Windowsform for
example three times.

ButtonClass c1 = new ButtonClass();
ButtonClass c2 = new ButtonClass();
ButtonClass c3 = new ButtonClass();

I assume the EventListener and its function is in the
ButtonClass. How do I know know which Button was pressed,
because I have just one Listener for each of the Buttons?

Thanks for helping

Thomas
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Andy Read | last post by:
Hello all, I have the requirement to produce source code that produces an object hierarchy. Example: Root | Folder 1
18
by: Christopher W. Douglas | last post by:
I am writing a VB.NET application in Visual Studio 2003. I have written a method that handles several events, such as closing a form and changing the visible status of a form. I have some code...
8
by: Mark | last post by:
Hi, I'm looking for some ideas on how to build a very simple Event processing framework in my C++ app. Here is a quick background ... I'm building a multithreaded app in C++ (on Linux) that...
9
by: VK | last post by:
My original idea of two trains, however pictural it was, appeared to be wrong. The truth seems to be even more chaotic. IE implements its standard down-up model: any mouse event goes from the...
13
by: Charles Law | last post by:
Mr "yEaH rIgHt" posted the following link about a week ago in answer to my question about removing event handlers. > http://www.vbinfozine.com/t_bindevt.shtml Following on from that post, the...
12
by: Jack Russell | last post by:
My unstanding of all VB up to and including vb6 is that an event could not "interrupt" itself. For instance if you had a timer event containing a msgbox then you would only get one message. ...
41
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based...
9
by: jeff | last post by:
New VB user...developer... Situation...simplified... - I want to wrap a pre and post event around a system generated where the pre-event will always execute before the system event and the...
19
by: Daniela Roman | last post by:
Hello, I try to fire an event under a button click event and maybe anybody can give a clue please. I have let's say a WEB grid with PageIndexChanged event: private void...
5
by: jaysonnward | last post by:
Hello All: I've recently been recreating some 'dropdown menus' for a website I manage. I'm writing all my event handlers into my .js file. I've got the coding to work in Firefox, but the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.