I have a first order menu item that is a chevron for toggling the state of a title panel. The menu item has ToolTipText that I want to change while the tool tip window is still open (i.e. hover active).
Is there a way to make this happen ?
Code
topMenuStrip.ShowItemToolTips = true;
chevronMenuItem.ToolTipText = "Hide title";
chevronMenuItem.Click += new System.EventHandler(titleToggle_Click);
private void titleToggle_Click(object sender, EventArgs e)
{
var mi = (ToolStripMenuItem)sender;
if (titlePanel.Visible)
{
titlePanel.Visible = false;
mi.ToolTipText = "Show title bar"; // does not change while hover active
mi.Image = Properties.Resources.chevron_expand;
}
else
{
titlePanel.Visible = true;
mi.ToolTipText = "Hide title bar"; // does not change while hover active
mi.Image = Properties.Resources.chevron_collapse;
}
}
Pictures
Third image is after the tooltip is rerendered after new hover (mouse leave chevron mouse enter chevron)
According to Microsoft documentation, you must set the property AutoToolTip = false and the property ShowItemToolTips = true.
ToolStripItem uses the Text property as the default source for the
ToolTip content. Set AutoToolTip to false to use ToolTipText as the
source for ToolTip content.
To use this property, you must also set ShowItemToolTips to true.
Source: ToolStripItem.AutoToolTip Property
Related
I want to implement simple toggle functionality on a ToolStripButton.
View Mode --> Edit Mode
Edit Mode --> View Mode
This piece of code is working fine except that ToolTip doesn't refresh. I tried AutoToolTip property. I also tried setting ToolTipText to string.Empty. But my toolstrip continues to show old tooltip text. ToolTipText should also change when I toggle.
private void btnTrackingMode_Click(object sender, EventArgs e)
{
if (_currentSheetTrackingMode == SheetTrackingMode.ViewMode)
{
_currentTrackingMode = TrackingMode.EditMode;
btnTrackingMode.AutoToolTip = true;
btnTrackingMode.ToolTipText = string.Empty;
btnTrackingMode.ToolTipText = "You are currently in Edit Mode. Click here to enter into View Mode";
btnTrackingMode.Image = ((System.Drawing.Image) ((Image) new ComponentResourceManager(typeof(MyForm)).GetObject("btnTrackingEditMode.Image")));
btnTrackingMode.ImageTransparentColor = System.Drawing.Color.Magenta;
}
else
{
_currentTrackingMode = TrackingMode.ViewMode;
btnTrackingMode.AutoToolTip = true;
btnTrackingMode.ToolTipText = string.Empty;
btnTrackingMode.ToolTipText = "You are currently in View Mode. Click here to enter into Edit Mode";
btnTrackingMode.Image = ((System.Drawing.Image) ((Image) new ComponentResourceManager(typeof(MyForm)).GetObject("btnTrackingViewMode.Image")));
btnTrackingMode.ImageTransparentColor = System.Drawing.Color.Magenta;
}
}
You can try to refresh the button's parent ToolStrip
btnTrackingMode.ToolTipText = "something";
//put your toolstrip's name here
buttonsToolStrip.Refresh();
You should be able to access the parent tool strip through btnTrackingMode.Owner if it's not directly available for some reason.
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 want to change notification icon text on mouse hover using c#
e.g
NotifyIcon notifyicon;
notifyicon.Icon = (Icon)resManager.GetObject("test");
notifyicon.Visible = true;
if(a==b)
{
notifyicon.Text = "Both are equal";
}
else
{
notifyicon.Text = "Not equal";
}
Is it possible???
See the documentation for the ToolTip class on MSDN. The "Examples" section should contain the information you need.
I have a TreeView control for which each node in it I want to share a ContextMenuStrip which has two ToolStripMenuItems ie:
this.BuildTree = new MyApp.MainForm.TreeView();
this.ItemMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
this.DeleteMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.ShowLogMenuItem = new System.Windows.Forms.ToolStripMenuItem();
...
this.ItemMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.DeleteMenuItem,
this.ShowLogMenuItem});
So I show and hide these to items according to certain criteria on a right click in a MouseUp event. When both are hidden I hide the ContextMenuStrip itself. Problem is when I hide the ContextMenuStrip it seems the next time I want to show one of the menu items I have to click twice on the node. The strange thing is on the first click to reshow one or both of the the items I have the following code:
ItemMenuStrip.Visible = true;
ShowLogMenuItem.Visible = true;
The two lines above don't seem to do anything ie both remain false in the debugger view after stepping over each line.
I don't think I've got any events on these values being set at least I don't have any events attached.
What am I doing wrong?
I suggest you to set:
this.BuildTree.ContextMenuStrip = this.ItemMenuStrip;
to make the menu automatically open on tree right-click.
Then subscribe ItemMenuStrip.Opening event to change the visibility of items and the contextmenu itself:
void ItemMenuStrip_Opening(object sender, CancelEventArgs e)
{
if (something)
{
e.Cancel = true; // don't show the menu
}
else
{
// show/hide the items...
}
}
If you need to know the current position of the clicked point (e.g. to check if a tree node is clicked), you can use Control.MousePosition property. Note that MousePosition is a point in screen coordinates, so you need to call treeView1.PointToClient(position) to get the tree coordinates e.g. :
private void ItemMenuStrip_Opening(object sender, CancelEventArgs e)
{
var pointClicked = this.BuildTree.PointToClient(Control.MousePosition);
var nodeClicked = this.BuildTree.GetNodeAt(pointClicked);
if (nodeClicked == null)
{
// no tree-node is clicked --> don't show the context menu
e.Cancel = true;
}
else
{
// nodeClicked variable is the clicked node;
// show/hide the context menu items accordingly
}
}
So figured out what was going wrong I was setting Visible on this.ItemMenuStrip rather than the this.BuildTree.ContextMenuStrip.
This seems rather strange to me as I would have thought BuildTree.ContextMenuStrip was just a direct reference to the ItemMenuStrip but apparently 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.