Solved

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

Posted on 2011-03-24
4
407 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
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 85

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 29

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Connection String 16 54
Exit the loop 4 42
Set value of dynamically created checkboxlist 2 31
Printing 1 50
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

816 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

11 Experts available now in Live!

Get 1:1 Help Now