Windows Develop Bookmark and Share   
 index > Windows Forms General > In C#.NET How to get Current Control Name
 

In C#.NET How to get Current Control Name

Hi,

I want to create a input check method. So I need to know the Current control's name. That method I can use for every form. The method first get the current control name and by Name the method will check the input value.

Please help me

Thanks

s.j

s.j1506  Saturday, April 12, 2008 11:51 AM

Hi,

You can use the sender parameter.Cast the sender parameter to Control anduse the 'Name' property.

Eg:

I have added two buttons to the form. Both thebutton's click events are handled by the Button_Click event handler. Using the sender, I can then determine using this generic handler the name of the button clicked. Similarly you can do it for the control you are targetting

Code Snippet

private void Button_Click(object sender, EventArgs e)

{

string ctrlName = ((Control)sender).Name;

MessageBox.Show(ctrlName);

}

Note: Post moved from 'Where do I Post..' to Windows Forms General.

HTH,
Suprotim Agarwal
Suprotim Agarwal  Saturday, April 12, 2008 2:28 PM

Hi,

You can use the sender parameter.Cast the sender parameter to Control anduse the 'Name' property.

Eg:

I have added two buttons to the form. Both thebutton's click events are handled by the Button_Click event handler. Using the sender, I can then determine using this generic handler the name of the button clicked. Similarly you can do it for the control you are targetting

Code Snippet

private void Button_Click(object sender, EventArgs e)

{

string ctrlName = ((Control)sender).Name;

MessageBox.Show(ctrlName);

}

Note: Post moved from 'Where do I Post..' to Windows Forms General.

HTH,
Suprotim Agarwal
Suprotim Agarwal  Saturday, April 12, 2008 2:28 PM

Suprotim ,

Thanks a lot. It's working.

s.j

s.j1506  Sunday, April 13, 2008 12:57 AM

You can use google to search for other answers

Custom Search

More Threads

• Is it ok to call ShowDialog in the form's constructor...
• XML documentation newbie
• Please, Help with word count!
• DataGridview Related Stuff
• Printing from c#
• Closing an application
• tootip back color does not change
• ListBox is really slow on adding about 64000 items
• Article on Optimizing Localization
• String List Typeconverter help