?
Solved

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

Posted on 2011-03-24
4
Medium Priority
?
426 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 1000 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 1000 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 500 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 500 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

765 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