views:

112

answers:

6

Hello, 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.

A: 

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

Robert Harvey
Not really the answer I was looking for, but I suppose that would work. I was thinking something like Control.GetChildAtPoint, but it is always returning null. Worse case I suppose a runtime addition of each control (meh..) and adding a tool tip property would work.I'll mark as answer later, but really wanna see if there is a better way.
Dave
A: 

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?

Aaron
A: 

Edit - I wasn't thinking straight. Apologies.

frou
Yes, design name. It shows up when reflected at run time, so I'm pretty sure it's available.
Dave
You *can* access control names with the control.Name property.
codymanix
Ah - this property assignment must be baked in to the auto-generated .Designer.cs file? Because I don't think you can recover a variable name at runtume in the general case without setting up some anonymous type trickery beforehand.
frou
Sorry, I think my rambling about anonymous type stuff only applies to local variables in a method. You can of course use plain reflection to get the names of class members.
frou
A: 

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

codymanix
This would work just like recursivly adding a ToolTip.
Dave
A: 

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);
    }
Dave
A: 

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);
                }
Dave