Generic method to get a control name when moused over? - c#

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.

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

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

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?

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

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

Related

Find if TextBox.Text got changed by the user or by the code (Windows Phone 8)

I have a Windows Phone 8 project that converts values (i.e.: Celsius to Fahrenheit). There are two TextBox UI elements, one of which is read-only. The user can change the first TextBox to input the value to be converted. He can also press a button to "swap" the two TextBoxes so that he can do the reverse conversion. When the user presses the button, the value from the second TextBox goes into the first TextBox (and vice versa). But it's not the user who changed the value, it's the code who did.
I asked around (on IRC) and researched the subject, but I am a beginner and couldn't understand most of what I have found.
I heard that a simple solution would be to use Data Bindings. I researched the subject, and from what I read, Data Bindings can't solve my problem (correct me if I'm wrong).
I also tried to create a subclass of TextBox, hoping that I could hook in some custom event to it and go further in that direction. But I did not understand how to link the custom TextBox to the UI (in XAML). The way I created the subclass is to just create a new class and add TextBox as the parent. I know there is a template in VS to create a new User Control, and I tried it, but I couldn't understand what I was doing (or what I was supposed to do).
So I have two questions: Am I looking at the problem from the right angle? If yes, how do I create a custom TextBox and link it to the UI? If not, how could I solve my problem?
If your question is how to distinguish if the text got changed by the user or by the code then its simple.
Assuming that when the user types something you'd like to perform method A but when the code changes the text you'd like to perform method B:
In both cases you will need to override the TextBox.TextChanged() event handler.
You will also need a flag variable to tell you if the swap button was pressed or not.
The event handler should be something like this:
{
if (swap_pushed)
{
Method_B();
swap_pushed = false;
}
else
{
Method_A();
}
}
And finally your event handler for swap Button.Click() should be like this:
{
swap_pushed = true;
}

How to use tooltip on toolstripbutton

I'm trying to apply a tooltip to a toolstripbutton but it keeps giving me this error:
Operator '==' cannot be applied to operands of type 'System.Windows.Forms.Control' and 'System.Windows.Forms.ToolStripButton'
Any clue on how to solve this?
UPDATE:
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (e.AssociatedControl == tBtn1)
{
using (Font f = new Font("Tahoma", 9))
{
e.ToolTipSize = TextRenderer.MeasureText(
toolTip1.GetToolTip(e.AssociatedControl), f);
}
}
}
ToolStripButton derives from ToolStripItem which has a ToolTipText property.
As already explained, the ToolStripItem does not derive from the Control class so provides its own implementation to render tool tips. This post may help you with customising the tooltip.
UPDATE: After trying this out in a Winform project and not having success, I searched other threads on SO, this is one that may be helpful to you:
Showing a tooltip on a non-focused ToolStripItem
The problem with the first is you can't set it to the button directly,
it doesn't inherit from Control, and the tooltip won't show up unless
you're over the strip but not over a button.
elsewhere
I was trying to do the same thing and determined it was going to be
pretty challenging and not worth it. The reason is that internally,
the .NET code is specifically designed to only show the tooltip if the
window is active - they are checking this at a Win32 level so its
going to be hard to fake the code out.
The user never accepted any of the answers as true, and it does seem at a glance that this may be a lot of work for little reward. Is this a project from scratch? If so, maybe you can do it using WPF, which is much more flexible than winforms.
Strangely enough, toolstripbutton class doesnt inherit from Control class unlike other System.Windows.Forms gui components. Perhaps in your code e.AssociatedControl is meant to be used with System.Windows.Forms controls. In short, I think MS hasnt decided to provide a tooltip for strip controls. I do not know your exact requirement, but for some alternative that might click, see this link.

How to find the id of dragged control

I am implementing drag and drop functionality among lables. I want to find the ID of a dragged control like label, button, etc so that I can assign a text to it.
I am not sure how to get that data through the events. Any suggestion will help.
you can use Drag_Enter and DragOVer Events, to achieve the goal DragEnter Doucmentation
and DragOver Documentation
alternativily you can check following tutorials
Code project : Drag and Drop in Windows Forms
Code Project : Drag and Drop UI in WinForms
check out System.Windows.Forms.DragEventArgs e
void MyControl_DragDrop(object sender, DragEventArgs e)
{
var controlBeingDrag = (Label)sender; // cast from object
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop, false);
}
the control sending drag event is object sender
Assuming you've done your research to figure out how to do drag and drop of controls on your form (and your question is really only limited to your question title: How to find the id of dragged control) most standard WinForm events provide a parameter (object sender) which represents the control used to invoke the event. You should be able to get its ID as you would normally.
Apparently it is less obvious how to consistently get the ID from a WinForm control. Luckily, Brian McMaster has a (fairly old meaning only possibly relevant) MSDN blog post for doing just that. In .NET 3.5 I'd probably use this old post as the start to an extension method for control objects.
If your question is broader than that then you may benefit from #Ravi's links, but on SO we generally expect that you do your own research. Please be sure to do so before asking questions.

How do I click a usercontrols child in designer?

I'm having a bit of trouble with creating one of my custom controls.
What I've got is a listbox within a usercontrol, and I need to be able to click on the lists items while still in the designer. This would make it act much like the tabcontrol.
I haven't dealt much with usercontrols but I've tried catching some overide events without success.
protected override void OnClick(EventArgs e)
{
if (DesignMode)
{
InvokeOnClick(listBox1, e);
}
base.OnClick(e);
}
I haven't been able to find anything on the web.. Any ideas on how I can do this?
Thanks in advance =)
#Bradley: thanks for pointing me in the right direction
You will need to write a ControlDesigner class, then use it in a [Designer( ... )] attribute on your UserControl.
See the example here:
http://msdn.microsoft.com/en-us/library/sycctd1z(v=VS.90).aspx
For the actual click:
http://msdn.microsoft.com/en-us/library/system.windows.forms.design.controldesigner.gethittest(v=VS.90).aspx
The ControlDesigner has a protected bool GetHitTest(Point point) method - you can implement this in your ControlDesigner and return true when you want your control to handle a click, based on the click's location on the screen.
I found this link that says you need to implement a custom designer to get the desired behavior, and explains how to make it happen.
http://social.msdn.microsoft.com/Forums/pl-PL/winforms/thread/0b6ed0cb-907c-4733-b245-ae5d0b0e6606
You may be able to get away with catching the MouseDown event in the custom control and forwarding it on to the inner control. I'm not sure how MouseDown behaves in design mode though.

Can't find this.Controls - what am I missing?

Sorry for a potentially dumb question, I am still new at this. I really appreciate your help.
Referring to Get a Windows Forms control by name in C#
But I don't have a "this.Controls" available. Is there something I am missing here?
In other words, when I type "this." and visual studio populates a list of option, there is no "Controls" option.
In WPF, you should try this.FindName(string name)
Button b = (Button)this.FindName("button1");
If you are looking to iterate through the controls for whatever reason, in your Window class, you can iterate through the LayoutRoot's children (e.g.)
foreach (object o in this.LayoutRoot.Children)
{
MessageBox.Show(o.GetType().Name);
}
Keep in mind that the children can also contain children, so you'll need to delve into each of those as needed.
The link you gave was for Winforms, you are looking for a WPF way to do it which is different.
I have to add whats LayoutRoot: because I am new in WPF I did not know whats that
After general research I found them: it's your Grid
if you write grid visual studio did not find them what you should do?
go to your window
next go to the Grid Start tag : <grid>
next add : x:Name="your desired name"
like this : <Grid x:Name="FRM">
now go back to code yourwindow.xaml.cs
not you see it works foreach (object o in this.FRM.Children)
I hope it was useful for a novice like me
Yes, this is very important issue when we want to make a change in similar controls in a particular Grid. So after looking up for a while I found out one solution which looks better.
I am performing a Null or Empty check on all the textboxes in my Grid when someone randomly presses the SUBMIT Button
Here is the C# Codeblock
foreach (TextBox tx in Grid1.Children.OfType<TextBox>())
{
if (string.IsNullOrEmpty(tx.Text))
{
MessageBox.Show("No empty boxes please!");
return;
}
}

Categories