473,407 Members | 2,598 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,407 software developers and data experts.

Reusable event handler - identify sender

Me
My form has a dynamically populated context menu, and each has the same
event handler for the click event.

The event need to write the text of the clicked menu item into a
database, so I need to either pass that to the event handler, which I
cannot figure out how to do, or access the properties of the sender,
which I also cannot figure out how to do.

The context menu items are a list extracted from a database, using the
code below in a while loop:

ToolStripItem itm = cmenTicker.Items.Add(reader.GetString(0));
itm.Click += new System.EventHandler(this.itm_Click);

This is the itm_click:

private void itm_Click(object sender, EventArgs e)
{
//Balloon(/*This is where I need to know the text because
Balloon wants a string*/);
}

Sure this can't be so difficult, but I can't work this out.
Nov 11 '06 #1
4 2490
You can cast the sender variable to the type you know the sender has.
Try this:

private void itm_Click(object sender, EventArgs e)
{
ToolStripItem itm = sender as ToolStripItem;
if (itm != null)
{
Balloon(itm.Text);
}
}

"Me" <no***@nowhere.comwrote in message
news:45**********************@ptn-nntp-reader02.plus.net...
My form has a dynamically populated context menu, and each has the same
event handler for the click event.

The event need to write the text of the clicked menu item into a database,
so I need to either pass that to the event handler, which I cannot figure
out how to do, or access the properties of the sender, which I also cannot
figure out how to do.

The context menu items are a list extracted from a database, using the
code below in a while loop:

ToolStripItem itm = cmenTicker.Items.Add(reader.GetString(0));
itm.Click += new System.EventHandler(this.itm_Click);

This is the itm_click:

private void itm_Click(object sender, EventArgs e)
{
//Balloon(/*This is where I need to know the text because
Balloon wants a string*/);
}

Sure this can't be so difficult, but I can't work this out.

Nov 11 '06 #2
"Me" wrote:
My form has a dynamically populated context menu, and each has the same
event handler for the click event.
.....
This is the itm_click:

private void itm_Click(object sender, EventArgs e)
{
//Balloon(/*This is where I need to know the text because
Balloon wants a string*/);
}

Sure this can't be so difficult, but I can't work this out.
Use the TAG property of all objects that trigger the event to store an
object reference of your choice you can identify. In your event function do
this:

private void itm_Click(object sender, EventArgs e)
{
MyIdentificationObject tagobj =
(MyIdentificationObject)(((Control)(sender)).Tag);
// do what ever you want to do with tagobj.
}

best
doc

Nov 11 '06 #3
Me
Fantastic. Thanks very much!

Lebesgue wrote:
You can cast the sender variable to the type you know the sender has.
Try this:

private void itm_Click(object sender, EventArgs e)
{
ToolStripItem itm = sender as ToolStripItem;
if (itm != null)
{
Balloon(itm.Text);
}
}
Nov 11 '06 #4


"Lebesgue" wrote:
You can cast the sender variable to the type you know the sender has.
Try this:

private void itm_Click(object sender, EventArgs e)
{
ToolStripItem itm = sender as ToolStripItem;
if (itm != null)
{
Balloon(itm.Text);
}
}
Yes, if you know the classtype of the senders this is easier than my solution.

doc

Nov 11 '06 #5

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

Similar topics

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...
3
by: gcl | last post by:
Hi, How to associate the dynamic controls array with its event? For example, creating a number of button, like 100, etc.. Button arrButton ..... In VB6, when u copy and paste a button
3
by: Jakub Otahal | last post by:
Hi all, I have problem........ I have array of buttons and I want to call one common event service. But I need to know the index of clicked button -- how can I do that? thanks jakub
2
by: David A. Beck | last post by:
I would to set up a event routine for several command buttons. I need to be able to identify by the caption or something whick button caused the event to trigger. I think it's in the "sender" or...
7
by: Grahmmer | last post by:
I have a few timers that are added to a form at runtime. I can handle the event fine, but I cannot identify which timer fired. Is there a way to do this? Timer Creation: -------------...
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...
3
by: akki | last post by:
I create link buttons dynamically in the code and give them IDs through which I can distinguish them.I want to know how to create a single event handler for all the link buttons that I create The...
5
by: ally | last post by:
I am working with a panel of preconfigured textbox controls arranged in rows and columns. They are programmatically updated based on changing data from an external data collector device. I...
5
by: james | last post by:
Hello, I am having a little trouble creating an event handler for a context menu toolstripmenuitem. I've seen various tutorials and so on, but I keep getting a bit stuck! So far I have a second...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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,...

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.