Solved

C#, I want to learn for the name control used in the run-time

Posted on 2011-03-24
4
388 Views
Last Modified: 2012-05-11
Hi there;

In C#, I want to learn for the name control used in the run-time. I mean when I run my windows form and when I click on a textbox let's say named as txtbx, I want to display that name.

How can I do that?

Kind regards.
0
Comment
Question by:jazzIIIlove
  • 2
4 Comments
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 250 total points
Comment Utility
hook into the click event and in that method cast the sender as textbox

yourTextBox_ClickEvent(object sender, EventArgs e)
{
  TextBox tb = sender as TextBox;
   tb.Text = tb.Name;
}
0
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 250 total points
Comment Utility
yourTextBox.Click += new EventHandler(yourTextBox_ClickEvent); //will be the stub in code to "hook" into the event
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
Comment Utility
More generically, do you need to know which control on the form is currently focused?

If so, use Form.ActiveControl:
http://msdn.microsoft.com/en-us/library/system.windows.forms.containercontrol.activecontrol(VS.80).aspx
0
 
LVL 29

Accepted Solution

by:
anarki_jimbel earned 125 total points
Comment Utility
Basicly this is similar to what guys already posted here. But you attach a handler to each control so you don't need to add p_davis's code into each handler, and you will see which control was clicked...
private void Form1_Load(object sender, EventArgs e)
        {
            foreach (Control cntrl in GetControls(this))
            {
                cntrl.Click += ClickHandler;
            }
        }
        public List<Control> GetControls(Control form)
        {
            List<Control> controlList = new List<Control>();

            foreach (Control childControl in form.Controls)
            {
                // Recurse child controls.
                controlList.AddRange(GetControls(childControl));
                controlList.Add(childControl);
            }
            return controlList;
        }
        private void ClickHandler(object sender, EventArgs e)
        {
            MessageBox.Show(((Control)sender).Name);
        }

Open in new window

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now