Solved

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

Posted on 2011-03-24
4
424 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 250 total points
ID: 35211551
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
ID: 35211561
yourTextBox.Click += new EventHandler(yourTextBox_ClickEvent); //will be the stub in code to "hook" into the event
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 125 total points
ID: 35211747
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 30

Accepted Solution

by:
anarki_jimbel earned 125 total points
ID: 35212532
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…

688 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