By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,649 Members | 1,816 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,649 IT Pros & Developers. It's quick & easy.

Is there any way to return the name of the control I click on in winform App

snehil
P: 19
Is there any way to return the name of the control I click on in a window form application using C#?
Using this pointer only returns name of the form.
Apr 17 '11 #1
Share this Question
Share on Google+
7 Replies


GaryTexmo
Expert 100+
P: 1,501
Are you looking for the name of the control, like the text name the user assigns, such as "button1", or are you looking for the type, which would be a "System.Forms.Button" kind of thing?

If the former, you can access the "Name" property. For the latter, use the GetType method.

If it's not either of those, can you please be more clear in what you're looking for? :)
Apr 18 '11 #2

snehil
P: 19
I have 10 checkboxes in a form... and what i want is when i right click on a checkbox and click edit on the contextmenu then the text value of the checkbox should change..
so what i want is to target the checkbox and that check box out of 10 on whihc i have right clicked..
cant even use sender object as as sender object will return "Edit" as we are clicking on the Edit on the context menu
getting me??
Apr 19 '11 #3

GaryTexmo
Expert 100+
P: 1,501
Ah, yea that does throw a bit of a wrench into things, doesn't it? I can't see any kind of parent control property on the context menu, so we'll have to be a bit more creative.

I checked the Click and MouseClick events on the CheckBox and neither get fired; however, both the MouseDown and MouseUp events are fired when you right-click to open a context menu. Additionally, the MouseEnter and MouseLeave events both fire.

One possible solution might be to attach a listener to the MouseDown event on your CheckBox objects. In it, you can put the following code...

Expand|Select|Wrap|Line Numbers
  1. void checkBox_MouseDown(object sender, MouseEventArgs e)
  2. {
  3.     if (sender is CheckBox)
  4.     {
  5.         ((CheckBox)sender).Focus();
  6.     }
  7. }
This will activate the CheckBox object when the mouse button is down, which will in turn update your form's ActiveControl property. At that point you have the CheckBox. I made a context menu with an Edit selection in it, then put the following code in it's Click event handler.

Expand|Select|Wrap|Line Numbers
  1. private void editToolStripMenuItem_Click(object sender, EventArgs e)
  2. {
  3.     ToolStripMenuItem o = sender as ToolStripMenuItem;
  4.     if (o != null)
  5.     {
  6.         Console.WriteLine(o.Name);
  7.         Console.WriteLine(this.ActiveControl.Name);
  8.     }
  9.     else
  10.     {
  11.         Console.WriteLine("Not a control!");
  12.         Console.WriteLine(sender.GetType());
  13.     }
  14. }
I have three check boxes and get the following when I right-click edit on each of them...

editToolStripMenuItem
checkBox1
editToolStripMenuItem
checkBox2
editToolStripMenuItem
checkBox3
Apr 19 '11 #4

snehil
P: 19
I'm not having VS right now.. so will definately try it asap.. and just to be sure.. can i do something like this...
Expand|Select|Wrap|Line Numbers
  1. this.ActiveControl.Name.Text = "somethingSomething";
  2.  
??
Apr 19 '11 #5

GaryTexmo
Expert 100+
P: 1,501
No, you can't do that... not quite. The ActiveControl property is a reference to a Control object. This is the base class for a button, checkbox, listbox... anything that can go in a Form's Controls list.

To see what's available to you at the Control level, you can check the MSDN documentation.
http://msdn.microsoft.com/en-us/libr...s.control.aspx

If you look up the Name property, it's just a string, so you would get it or set it just by accessing the Name property. No need to use Text.

Expand|Select|Wrap|Line Numbers
  1. this.ActiveControl.Name = "somethingSomething";
An important question here though might be this: Why do you want to change the name of the control? If you want to change the text on it, use the Text property, but you shouldn't need to change the Name of the control once you've created the object.

Finally, it's worth mentioning that you only get access to the Control methods and properties from Form.ActiveControl; however, you're free to cast it to whatever you like if it's valid. This is what I did.
Apr 19 '11 #6

snehil
P: 19
no i dont want to change the name of the control.. i want to change its text value..
so i can use it like this right?
Expand|Select|Wrap|Line Numbers
  1. this.ActiveControl.Name.Text = "somethingSomething";
or I'll have to cast it or something??
Apr 20 '11 #7

GaryTexmo
Expert 100+
P: 1,501
You've gotta look at what you're trying to do. Look at this link, it defines the Control class.

http://msdn.microsoft.com/en-us/libr...s.control.aspx

Find the Name property. Does it have a Text property associated with it? No, it doesn't. However, a control does have a Text property...

Finally, you will have to cast it if you want to access things that are specific to that particular control. The ActiveControl property on a Form is a Control object. It only has what a Control exposes. If you want to see the CheckState of the CheckBox object, you'll have to cast it.

If you managed to get Visual Studio installed, you can right-click any object and select "Go to definition". This will bring up the meta data so you can see what an object inherits from (and thus see the meta data for it) and see what things the class exposes publicly. Alternatively, if you don't have Visual Studio, you can look these things up on MSDN using their search. Another helpful way to find it is to type into a google search bar, "MSDN C# <class name here>" and the MSDN page for that class should be your first hit.

MSDN is a very valuable resource, even if you do have Visual Studio, as it lets you find information and examples for almost everything in the .NET framework.
Apr 20 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.