[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

656 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