Is there a simple way to create and show a custom tooltip control in C# / WinForms?
My current thinking is either:
create a subclass of Tooltip,
override the OnPaint method, set it
as the parent control's tooltip
create a subclass of form and show
it manually
Any thoughts?
It depends on what you need for your tool tip. If you only need a tool tip with balloon shape, animation and fading effects with custom text color and background, It is easier to use ToolTip control
// Create your control
System.Windows.Forms.Button trialButton = new Button();
trialButton.Text = "Trial Button";
// Tool tip string
string toolTipText = "Hello World";
// ToolTip toolTip = new ToolTip(this.components);
ToolTip toolTip = new ToolTip();
toolTip.ToolTipTitle = "ToolTip Title";
toolTip.UseAnimation = true;
toolTip.UseFading = true;
toolTip.IsBalloon = true;
toolTip.Active = true;
toolTip.SetToolTip(button, toolTipText);
Related
how to show tooltip over disabled button in UWP?
Button b=new button();
b.IsEnabled=false;
b.content="Button";
ToolTip t= new ToolTip();
t.Content="Hello";
ToolTipService.SetToolTip(b, ToolTip);
Disabling a button not only changes the style, but also intercepts the triggering of related events.
For example, if disabled, Button will not trigger Pointer related events. The display condition of the Tooltip is that the Pointer is hovering on the control for a period of time. If the control cannot detect the Pointer event, the Tooltip will never meet the corresponding trigger.
But if you need it to display, we can change it another way:
var grid = new Grid();
Button b = new Button();
b.IsEnabled = false;
b.Content = "Button";
ToolTip t = new ToolTip();
t.Content = "Hello";
grid.Children.Add(b);
ToolTipService.SetToolTip(grid, t);
We can put Tooltip on the Grid that will not be disabled, this can avoid this problem.
Thanks.
I create a user control dynamically in my code
UserControl myobject = new UserControl();
myObject contains a button etc.
when I add this control to my picturebox
picturebox.Controls.Add(myobject);
my picturebox's backgorund image is dissappeared.
Why?
note: the button can be seen however. I want picturebox to be seen also
Set transparent basckground color of your user control. This will make picturebox visible:
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
picturebox.Controls.Add(myobject);
BTW I believe you have different name of user control. And yes, as #samjudson stated, PictureBox should not be used this way: try to use Panel with background image instead (approach will stay same - use transparent color to see parent control):
panel.BackgroundImage = // your image
UserControlDisplay myobject = new UserControlDisplay();
myobject.BackColor = Color.Transparent;
panel.Controls.Add(myobject);
Try this:
UserControl myobject = new UserControl();
Button but = new Button();
but.BackColor = Color.Gray
pic.BackColor = Color.Green;
myobject.Controls.Add(but);
pic.Visible = true;
pic.Controls.Add(myobject);
The PictureBox control is not meant to be used as a container. Try adding a parent Panel or similar and the adding the PictureBox and your custom control to the Panel control.
I have a dynamically filled ContextMenuStrip where each ToolStripMenuItem has a formatted text for the tooltip. And, in order for this text to make sense to the user, I must use a monospaced font, such as "Courier New". The default font is a regular, non Monospaced font.
I couldn't find any getter for the ToolTip object nor a way to override its Draw event nor a way to set its style.
So, is it even possible to change ToolStripMenuItem's tooltip font?
Implementing CustomToolTip that inherits from ToolTip doesn't solve the issue, which is passing the new tooltip to ToolStripMenuItem.
OK, thanks to Tony Abrams and William Andrus, the solution is as follows:
A static instance of ToolTip which initialized.
toolTip = new ToolTip();
toolTip.OwnerDraw = true;
toolTip.Draw += new DrawToolTipEventHandler(tooltip_Draw);
toolTip.Popup += new PopupEventHandler(tooltip_Popup);
toolTip.UseAnimation = true;
toolTip.AutoPopDelay = 500;
toolTip.AutomaticDelay = 500;
ToolTip's Popup event to set its size.
void tooltip_Popup(object sender, PopupEventArgs e)
{
e.ToolTipSize = TextRenderer.MeasureText(toolTipText, new Font("Courier New", 10.0f, FontStyle.Bold));
e.ToolTipSize = new Size(e.ToolTipSize.Width + TOOLTIP_XOFFSET, e.ToolTipSize.Height + TOOLTIP_YOFFSET);
}
ToolTip's Draw event for actual drawing.
void tooltip_Draw(object sender, DrawToolTipEventArgs e)
{
Rectangle bounds = e.Bounds;
bounds.Offset(TOOLTIP_XOFFSET, TOOLTIP_YOFFSET);
DrawToolTipEventArgs newArgs = new DrawToolTipEventArgs(e.Graphics, e.AssociatedWindow, e.AssociatedControl, bounds, e.ToolTipText, toolTip.BackColor, toolTip.ForeColor, new Font("Courier New", 10.0f, FontStyle.Bold));
newArgs.DrawBackground();
newArgs.DrawBorder();
newArgs.DrawText(TextFormatFlags.TextBoxControl);
}
ToolStripMenuItem's MouseEnter event to show the tooltip.
System.Windows.Forms.ToolStripMenuItem item = (sender as System.Windows.Forms.ToolStripMenuItem);
toolTip.SetToolTip(item.Owner, "ToolTipText");
You could create a custom ToolTip class (CustomToolTip) that inherits from ToolTip. Then you would have to handle the OnDraw event. Inside that event you can change the font.
Look here for an example (there is a vb and c# example).
EDIT
You would have to handle the rendering of the custom tooltip on your own (IE: OnMouseOver, OnMouseLeave events of the toolstripmenuitem). You might be able to create a customtoolstripmenuitem that uses a custom tooltip, but I'm not sure that toolstripmenuitem exposes a tooltip propety/object.
I know I'm a little late to the party on this one but you can use reflection to set the instance of ToolTip that is used for rendering the tooltips. After doing that you can just use the Draw method as you normally would.
public void SetToolTipInstance(ToolStrip ts, ToolTip tt)
{
Type type = ts.GetType.BaseType;
int propToolTip = Convert.ToInt32(type.GetField("PropToolTip", BindingFlags.NonPublic | BindingFlags.Static).GetValue(ts));
dynamic ps = type.BaseType.GetProperty("Properties", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(ts);
ps.GetType.GetMethod("SetObject", BindingFlags.Instance | BindingFlags.Public).Invoke(ps, {propToolTip,tt});
}
I want to add a tooltip using ToolTip class on a column of a grid in winforms.
I want this because I need to extend duration of builtin grid tooltip in radgridview. If you can help me in settings the time of builtin tooltip of grid then it would also be sufficient.
EDIT: Can anybody just tell me that is it possible or not?
Thanks.
It's possible to add a ToolTip to an existing control. I've never used radgridview, so I can only give you a general direction to head.
ToolTip tooltip = new ToolTip();
tooltip.SetToolTip(grid, "your caption here");
tooltip.Popup += HandleToolTipPopup;
tooltip.AutoPopDelay = {time to display tooltip};
private void HandleToolTipPopup(object sender, PopupEventArgs e)
{
Point mouseLocation = Control.MousePosition;
Point relativeLocation = grid.PointToClient(mouseLocation);
// Check to see if it is within the area to popup on.
// Set e.Cancel to false if not.
}
I would like to display a ToolTip for when the mouse is hovering over a control.
How does one create a tooltip in code, but also in the designer?
Here is your article for doing it with code
private void Form1_Load(object sender, System.EventArgs e)
{
// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();
// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;
// Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(this.button1, "My button1");
toolTip1.SetToolTip(this.checkBox1, "My checkBox1");
}
Drag a tooltip control from the toolbox onto your form. You don't really need to give it any properties other than a name. Then, in the properties of the control you wish to have a tooltip on, look for a new property with the name of the tooltip control you just added. It will by default give you a tooltip when the cursor hovers the control.
Add a ToolTip component to your form
Select one of the controls that you want a tool tip for
Open the property grid (F4), in the list you will find a property called "ToolTip on toolTip1" (or something similar). Set the desired tooltip text on that property.
Repeat 2-3 for the other controls
Done.
The trick here is that the ToolTip control is an extender control, which means that it will extend the set of properties for other controls on the form. Behind the scenes this is achieved by generating code like in Svetlozar's answer. There are other controls working in the same manner (such as the HelpProvider).
ToolTip in C# is very easy to add to almost all UI controls. You don't need to add any MouseHover event for this.
This is how to do it-
Add a ToolTip object to your form. One object is enough for the entire form.
ToolTip toolTip = new ToolTip();
Add the control to the tooltip with the desired text.
toolTip.SetToolTip(Button1,"Click here");
I did it this way: Just add the event to any control, set the control's tag, and add a conditional to handle the tooltip for the appropriate control/tag.
private void Info_MouseHover(object sender, EventArgs e)
{
Control senderObject = sender as Control;
string hoveredControl = senderObject.Tag.ToString();
// only instantiate a tooltip if the control's tag contains data
if (hoveredControl != "")
{
ToolTip info = new ToolTip
{
AutomaticDelay = 500
};
string tooltipMessage = string.Empty;
// add all conditionals here to modify message based on the tag
// of the hovered control
if (hoveredControl == "save button")
{
tooltipMessage = "This button will save stuff.";
}
info.SetToolTip(senderObject, tooltipMessage);
}
}
Just subscribe to the control's ToolTipTextNeeded event, and return e.TooltipText, much simpler.