473,799 Members | 3,178 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Event

Hi there

I build a class "ButtonClas s" 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 1589
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 OkClickEventHan dler(object sender, EventArgs e);public delegate void CancelClickEven tHandler(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(Event Args e)
{
if (OkClick != null) //If someone is ready to handle the event {
OkClick(this, e); //Fire the event
}
}

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

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

private void cancelButton_Cl ick(object sender, System.EventArg s e) {
OnCancelClick(E ventArgs.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(bc 01_OkClick); //Start listening for okbutton click in buttonclass bc01
bc01.CancelCli ck += new EventHandler(bc 01_CancelClick) ;

ButtonClass bc02 = new ButtonClass();
bc02.OkClick += new EventHandler(bc 02_OkClick);
bc02.CancelCli ck += new EventHandler(bc 02_CancelClick) ;
And so on.

Finally you need a method that handles the events that has been fired
private void bc01_OkClick(ob ject 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 "ButtonClas s" 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 "ButtonClas s" 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
7049
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
2890
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 that applies to all these events, but I need to have specific code execute when the form closes. The properties for this method are sender (the originator) and e (event arguments). I know how to get typeof (sender) to determine what form or...
8
6088
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 uses message queues to pass pointers to Events between threads. In my app there are simple events that can be defined using an enum (for example an event called NETWORK_TIMEOUT) and more complex events that contain data (for example an event called...
9
5736
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 deepest visible element to the top. By carefully studying fromElement and toElement properties, one can handle events on any point of their way up. NN/FF implements a "Russian hills" style: mouse events go first up->down (window->deepest...
13
3519
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 following issues still exist. The article shows how to find methods on a receiver that match the pattern OnXXXX given the sender. It loops through the sender events and tries to get methods from the receiver that match the pattern. For each one...
12
4144
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. However in vb.net you get continual messages (even setting the system modal property). Firstly, are these two assumptions right and if so what is the approved
41
4331
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 on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. I'm not using the VB toolbar because of limitations in changing things like backcolor (I can't get...
9
2472
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 post event will always execuate after the system is completed... - I want to wrap this functionality in a framework, so I could possibly have 3 or 4 levels of inherited objects that need to have these pre / post events executed before and after the...
19
4759
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 DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
5
4546
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 onmouseleave / onmouseout event I've attached to my hidden drop down (in this case an <ul>), is not firing correctly. It seems that when the mouse enters the ul it fires the mouseleave event. The problem is in the hideMenu2(e) function. I'm copying...
0
9540
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10475
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10250
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10026
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3757
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.