I have a windows form with a toolstip in it with several buttons.
When the mouse is over a button of the toolstip then the toolstrip button starts to flash... looks like it gets and loses focus every second.
That results for the click to do nothing if the user click at the time that the button has no focus therefore the user has to click the button again and again util he gets the timing correct.
Does anyone knows anything about this?
I rally need some answers as soon as possible...
Thank you very much
I have found the reason...
The toolstrips in windows forms have by default the tooltips set to Auto and if the tooltip opens on the taskbar then the toolstrip loses focus.
The solution to this is to either disable the tooltips or to set it to manual and show the tooltip at another place.
Here is the code for showing the tool tip above the item manually:
private readonly ToolTip currentToolTip = new ToolTip();
private void ToolStripItem_MouseEnter(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Show(item.ToolTipText, item.Owner, item.Bounds.X, -20);
}
private void ToolStripItem_MouseLeave(object sender, EventArgs e)
{
ToolStripItem item = (ToolStripItem)sender;
this.currentToolTip.Hide(item.Owner);
}
You have to add the event handlers to all your ToolStripItems and set the ToolStrips' ShowItemToolTips to false.
Related
Like in web page using css we can show a div on mouse enter or hover, in the same way i want to show a panel on mouse enter event of a button, but i am unable to do this. I am trying like this.
private void btn2_MouseEnter(object sender, EventArgs e)
{
Button btn = (Button)sender;
btn.BackColor = System.Drawing.Color.MistyRose; //this is executed on mouse enter
Point locationOnForm = btn.FindForm().PointToClient(
btn.Parent.PointToScreen(btn.Location));
Panel pnl = new Panel();
Label lbl = new Label();
lbl.Text = "anything";
pnl.Controls.Add(lbl);
pnl.Location = new Point(locationOnForm.X, locationOnForm.Y);
pnl.Size = new Size(500, 500);
pnl.BackColor = Color.SkyBlue;
pnl.Visible = true;
pnl.Show();
}
I am not getting how to solve this. I want to know that
1) Is this the right approach or there is any other way of doing this?
2) If this is ok then what is the mistake i am doing here ?
Thanks.
Don't create the panel on mouse enter, rather have the panel created already then just show and hide it.
private void button1_MouseEnter(object sender, EventArgs e)
{
panel1.Show();
}
You have to add the panel to the Form controls
Form1.Controls.Add(pnl);
If you plan to have a panel hover over the button like a <div> in Web, you will have to call
BringToFront() to ensure that the panel does not appear behind the button or other controls on the form -
pnl.BringToFront();
Also like the previous answer, it may be better to have a panel placed on the form already and just set visible to true or false as well as the panel location otherwise you may end up adding multiple panels to the Form controls.
If you plan on just showing plain text in the panel, it may be easier to use Tooltip control -
MSDN - Tooltip Control
I want to cancel the Context Menu Opened Event of a TreeViewItem
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
e.Handled = true;
}
i thought this should work but instead i got a tiny context menu that has no menu items but i would like to have nothing appearing.
Thanks
You should use the ContextMenuOpening event of the control that owns the context menu, which is fired right before the context menu opens. There you can change the context menu or suppress it. Your code for the event to suppress works if you use the Opening event, see this article for further infos.
I am using the latest Ribbon Control from MS. When one double click the Tab Header, the whole Ribbon will hide the content and show only the header text. I want to disable this hide/unhide feature. So even if user double click Tab Header, the Ribbon remain as it is.
I think it should be the OnMouseDoubleClick Event in RibbonTab but have no clue how to override it. Am I suppose to give it a x:Name to each and every RibbonTab then write an empty method ribbonTab1_OnMouseDoubleClick to each Ribbon Tab?
Use the PreviewMouseDoubleClick event in the Ribbon:
PreviewMouseDoubleClick="ribbon_PreviewMouseDoubleClick"
and then in the handler, mark the event as handled:
private void ribbon_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
I creating a custonmized box class (inherits from ComboBox). I don't want the text box to react to right mouse clicks. I can get rid of the context menu by setting this to null in ApplyTemplate, but right mouse clicks move the cursor. I tried hooking up PreviewMouseRightButtonDown in ApplyTemplate and setting Handled to True, but the event still gets through which is strange as it seems to work for the left click.
The cursor actually moves when the mouse button is released, so you want mark the MouseRightButtonUp event as handled. You could override OnMouseRightButtonUp:
protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
{
base.OnMouseRightButtonUp(e);
e.Handled = true;
}
Or you could attach a class handler to the MouseRightButtonUp event to mark it as handled:
static MyComboBox()
{
EventManager.RegisterClassHandler(
typeof(MyComboBox),
MouseRightButtonUpEvent,
new MouseButtonEventHandler(MyComboBox_MouseRightButtonUp));
}
private static void MyComboBox_MouseRightButtonUp(
object sender, MouseButtonEventArgs e)
{
e.Handled = true;
}
That will also prevent the context menu from being created without you having to set it to null explicitly.
I have a checkbox with no text specified. Now whenever I tab down to this checkbox, it doesnot get highlighted.I even tried setting focus in checkbox_Enter() event. I checked for focus in this event and focus is there in this checkbox. How to get it highlighted so that user can know that the cursor is there in checkbox.
Try putting just a space into the Checkbox
Or
setting AutoSize to false
Setting the size of the Checkbox
Then there may be somewhere for WinForms to draw the focus ret.
Otherwise you have to to custom draw the Checkbox, or draw the focus rec round the Checkbox yourself.
Whatever you do it will not look that good, as users expect the focus rec to be round the label of the checkbox, and you wish to have a checkbox with no label.
I managed to do it by the below mentioned way
Use a panel.Push the checkbox inside that panel.Set the dimensions of a panel as such that it looks like a rectangle around the checkbox.In checkbox_enter() event set the border
BorderStyle.FixedSingle;
And in checkbox_Leave() event set the border again to
BorderStyle.None;
So this way it will tell user that focus in inside the checkbox.
To make the checkbox appear highlighted I had it change color on got focus and change back on lost focus.
this part is in the Form1.Designer.cs:
private void InitializeComponent()
{...
ckBox1.GotFocus += new System.EventHandler(checkBox_Highlight);
ckBox1.LostFocus += new System.EventHandler(checkBox_EndHighlight);
ckBox1.MouseHover += new System.EventHandler(checkBox_Highlight);
ckBox1.MouseLeave += new System.EventHandler(checkBox_EndHighlight);
}
This part is in the Form1:
private void checkBox_Highlight(object sender, EventArgs e)
{
CheckBox control = (CheckBox)sender;
control.FlatStyle = FlatStyle.Flat;
control.ForeColor = Color.Blue;
}
private void checkBox_EndHighlight(object sender, EventArgs e)
{
CheckBox control = (CheckBox)sender;
if (!control.Focused)
{
control.ForeColor = DefaultForeColor;
}
}
While tabbing, this puts a light blue shadow beneath the CheckBox on Enter and on Leave - at least on my Windows7:
...
checkBox1.Enter += new System.EventHandler(check_Enter);
checkBox1.Leave += new System.EventHandler(check_Leave);
...
private void check_Enter(object sender, EventArgs e)
{
((CheckBox)sender).BackColor = SystemColors.Highlight;
}
private void check_Leave(object sender, EventArgs e)
{
((CheckBox)sender).BackColor = Color.Transparent;
}
Actually the highlights comes on the text of the checkbox not the checkbox itself
So, if I was you, I would put any control in the background of my check box and give it the focus when my checkbox is focused, to have the same look of default controls highlights.
which will be shown to end users as checkbox highlights.