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.
Related
I wanted to show my search and clear(cross mark) icon into textbox that i had designed to search my treeview. When user enter into textbox and writes some text into it i want my cross icon to be visible and when nothing is written inside of it i wanted to show search icon.
What i have done so far is something like this.
but i wanted it to be like this when nothing entered.
and when text is entered i wanted to be like this ->
also when user clicks cross mark i wanted textbox to get empty.
Thankyou!
If you can live with the icon sitting above the text you can do this:
Add a Panel IconPanel and nest it in the TextBox textBox1:
Panel IconPanel = new Panel();
IconPanel.Parent = textBox1;
IconPanel.Size = new Size(32, textBox1.ClientSize.Height); // use your numbers!
IconPanel.Location = new Point(textBox1.Width - IconPanel.Width, 0);
IconPanel.BackgroundImageLayout = ImageLayout.Stretch;
Add your icons to the project resources and code the TextChanged event like this:
private void textBox1_TextChanged(object sender, EventArgs e)
{
IconPanel.BackgroundImage = textBox1.Text.Length == 0 ?
global::yourProjectName.Properties.Resources.searchIcon :
global::yourProjectName.Properties.Resources.clearIcon;
}
Initialize the IconPanel.BackgroundImage depending on your intial Textbox content!
I need to show a tooltip with an icon, a header and a text when the mouse hovers a ListViewSubItem. But the tooltip shall only pop up when the underlying text of the cell is trimmed with ellipses.
Up to now I have the following code:
private void ListView_ItemMouseHover(object sender, ListViewItemMouseHoverEventArgs e)
{
if (e.Item != null)
{
// get last subItem
ListViewItem.ListViewSubItem mySubItem = e.Item.SubItems[item.SubItems.Count - 1];
// TODO -> how to check if text is trimmmed?
// e.g. "This is the displayed text in the subitem whi..."
//if (mySubItem.IsTrimmed???)
{
// mToolTip is an instance of ToolTip class
mToolTip.ToolTipIcon = // any icon...
mToolTip.ToolTipTitle = "some title text";
mToolTip.SetToolTip(ListView, "some body text");
}
}
else
{
mToolTip.Hide(ListView);
}
}
Any ideas?
First measure the size of the drawn string:
float realWidth;
using (Graphics g = listView.CreateGraphics()) {
realWidth= g.MeasureString(mySubItem.Text, mySubItem.Font).Width;
}
And find out if its bigger than the current size:
if (mySubItem.Bounds.Width > realWidth) {
// Show tool tip...
}
For making a custom tooltip box, I think you will have to abandon ToolTip. Instead create a Panel with the layout you want, then change the Location of the Panel when you need to display it. You can also add some kind of animation to 'swipe' the Panel into view or something. Loading the information you want in the Panel to display then displaying.
As for detecting the text that is in the ListViewItem, there is an ItemMouseHover event that may do the trick.
I have a form done in WPF which has a custom control already on it called RateView. This custom control has 4 textboxes (which are all working as they should be). It also contains a button.
I have a second custom control called Extended Margin Info, which also has a XAML Form which will just show output data only.
How can I by clicking the button on the custom control called Rateview bring up the XAML canvas onto my Main window of the extendedmargin info XAML, in the same position everytime? Rateview control exists 5 times on the main window therfore there will be 5 buttons that when clicked, will need to output the popup of ExtendedMargin Info to the main screen in the same position each time with the content of extendedmargin info.
Your button, when clicked, should call a Command which updates a Property of some ViewModel that exposes the ViewModel of the current ExtendedMarginInfo you want to display. Then you can bind this property to the Content Property of a ContentControl in the target view. You can select the View you want the Control to display by using the ContentControl.ContentTemplateSelector property.
I guess you want show one popup and change it's content placing in it different controls.
At 1st create your custom control:
balloon = new LogEntryInfoBalloon();
balloon.SetMainWindow(this);
balloon.DataContext = vm.NotificationViewModel;
Then create Popup control (System.Windows.Controls.Primitives):
localPop = new Popup();
localPop.AllowsTransparency = true;
localPop.Placement = PlacementMode.AbsolutePoint;
localPop.StaysOpen = true;
localPop.PlacementTarget = this;
localPop.Child = balloon;
Placement target points to MainWindow.
Define timer that will close(hide) balloon:
localPopTimer = new Timer(new TimerCallback(CloseLocalPopup));
Close func:
private void CloseLocalPopup(object args)
{
var act = new Action(() =>
{
localPop.IsOpen = false;
});
Dispatcher.BeginInvoke(act, null);
}
Show balloon code looks like this:
private void ShowNotifyBaloon(NotifyBaloonViewModel vm)
{
var act = new Action(() =>
{
localPop.IsOpen = true;
localPopTimer.Change(4000, Timeout.Infinite);
});
Dispatcher.BeginInvoke(act, null);
}
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);
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.
}