473,395 Members | 1,641 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,395 software developers and data experts.

Find control name though object sender?

VMI
In my Windows Form, is it possible to get the control name through object
sender in an event handler? For example, in private void
dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e), how can I
know that the sender is "dataGridView_zip" ?

Thanks.
VS2005 2.0
May 9 '07 #1
2 37091
VMI,

If by control name, you mean the value of the Name property on the
control, then yes, it is, you just have to cast the sender parameter to a
type of Control, and then access the Name property.

However, this does not have to correspond to the name of the field or
variable that references the control. For dynamically created controls, the
name will not correspond to any field/variable you have. If you used the
designer to lay out the control, then it should have the same name as the
field it is assigned to in your containing class.

In general, you can not get the name of a variable/field that is
referencing a control (since multiple fields/variables can reference the
same control, and you wouldn't know what to do with the names of the
variables at that point, since the variables/fields could concievably have
the same name).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"VMI" <vo******@yahoo.comwrote in message
news:uD**************@TK2MSFTNGP06.phx.gbl...
In my Windows Form, is it possible to get the control name through object
sender in an event handler? For example, in private void
dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e), how can I
know that the sender is "dataGridView_zip" ?

Thanks.
VS2005 2.0


May 9 '07 #2
To illustrate in code what Nick pointed out:

private void dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e)
{
string ctrlName = ((DataGridView)sender).Name;
// or for a generic "Control":
string ctrlName2 = ((Control)sender).Name;
}

-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"VMI" wrote:
In my Windows Form, is it possible to get the control name through object
sender in an event handler? For example, in private void
dataGridView_zip_KeyPress(object sender, KeyPressEventArgs e), how can I
know that the sender is "dataGridView_zip" ?

Thanks.
VS2005 2.0
May 9 '07 #3

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

Similar topics

3
by: Ian Ornstein | last post by:
I am coding a windows form. The form has several labels and I hoped to use one click event because the processing to be done is very similar. I just tried this: void lblDayNoClick(object sender,...
1
by: juracan | last post by:
I created a user control with a couple of radio buttons on it. When the user clicks on one of the radio buttons an event gets passed up along with the value of the radio button clicked. I then...
2
by: Josh | last post by:
Hi Guys, I have been stuck on this problem for several days now, i have a set of nested datagrids. Inside the second datagrid i have a dropdown list, a textbox and a label. I want the textbox...
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
2
by: Jeff User | last post by:
Hi C# .NET1.1 I have read Visual Studio help and several articles on the web. Obviously this is a common topic. However, I see this code over and over yet I am stuck with the FindControl method...
9
by: redivider | last post by:
I followed discussion "access form control propertys via control name as string " but am getting NullReferenceException when trying to access any controls via their name using "this.Controls". ...
4
by: =?Utf-8?B?Unlhbg==?= | last post by:
How do I pass a control name to a method? I have a Textbox named txt1; in the form load event I want to pass txt1 to a method as: private void MyForm_Load(object sender, EventArgs e) {...
2
by: =?Utf-8?B?Unlhbg==?= | last post by:
I have the following code in vs2005; line number is added for convenience: 1 private void Form1_Load(object sender, EventArgs e) 2 { 3 txtMyField.Enabled = true; 4 Form2 f = new...
2
by: ricardosms | last post by:
Hello: I have a custom control with a Combobox that at form1_Load gets filled with the names of the controls with visual interface. From this ComboBox the user selects a control name and that...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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...
0
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...

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.