Dave
Dave

Reputation: 43

Generic method to get a control name when moused over?

I am writing an application that I am working on a skinning feature to it. What I want to do is allow a user to create an XML document that my code will parse through and set properties of the controls on a form. This is the easy part. Where I am stuck is giving the user a way to find out the control names/types with minimal coding/documentation effort.

My idea was to have a tool tip that when they moused over a control, it got the name of the control and type to show. Anyone know of a way to do this? I was thinking something like how Spy++ can find control, but I want to get the .NET properties also.

If someone has another idea I'm open ears.

Thanks Much.

Upvotes: 0

Views: 877

Answers (5)

Dave
Dave

Reputation: 43

Figured it out. The issue was because the mouse location wasn't relative to the client location. Thus the code below will resolve this issue. I put it in a polling thread that I already had going, but it should work with a timer or other event. Didn't work in MouseMove for some reason though. Thanks everyone for the help.

                Point p = this.PointToClient(MousePosition);
                Control x = this.GetChildAtPoint(p);
                if (x != null)
                {
                    MessageBox.Show(x.GetType().ToString() + " - " + x.Name);
                }

Upvotes: 1

Dave
Dave

Reputation: 43

Why wouldn't this work? I'm putting it in the base form, don't really see what it wouldn't work next to GetChildAtPoint being buggy.

    protected override void OnMouseMove(MouseEventArgs e)
    {
        Control c = this.GetChildAtPoint(e.Location);
        if (c != null)
        {
            MessageBox.Show(String.Format("Your control name is: {0} and type is {1}.", c.Name, c.GetType().ToString()));
        }
        base.OnMouseMove(e);
    }

Upvotes: 0

codymanix
codymanix

Reputation: 29468

You could recursively install a OnMouseOver eventhandler. Then if an OnMouseOver event occures the control is in the sender argument of the event handler.

Upvotes: 0

user187802
user187802

Reputation:

Are you willing to register for each controls mouse over event? If so, the sender would be the type, etc...maybe I'm missing something here? Furthermore you can get the control out of the visual tree in WPF/SL, why not port over to WPF?

Upvotes: 0

Robert Harvey
Robert Harvey

Reputation: 180787

You'll need to set up some ToolTip objects (one for each field), looping through all of the controls in your form to obtain the text for each tooltip.

http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx

Upvotes: 0

Related Questions