Tooltip not displaying, flashes when control is clicked - c#

I have a check box that is disabled that should be showing a tooltip when hovered over, but instead nothing happens. Once the checkbox is clicked on the tooltip shows momentarily then flashes on and off very fast. What could be causing this?
The tooltip should also be showing for every control involved, but shows for some and not others eventhough the tooltip is explicitly set for all controls. What could be causing this behavior?
Here is the event handler:
this.MouseHover += new EventHandler(OrderSummaryDetails_MouseHover);
void EventHandler_MouseHover(object sender, EventArgs e)
{
if (someCondition)
{
this.mFormTips.Show("Please open order form to manually modify this order", this);
}
}

I can't be positive, but if using WinForms, and you have your checkbox disabled (as in not enabled), then the checkbox will not receive events. This will cause tooltips not to show up properly.
I had the exact same problem before with a image button and what I ended up having to do was to create a gray scale of the image and swap it out when I wanted the button to be "disabled". I had to add the tooltip to the button and the image (two separate UI elements) and swap out the UI elements.

I added a MouseMove event and applied it to all the controls.
void OrderSummaryDetails_MouseMove(object sender, MouseEventArgs e)
{
Control control = GetChildAtPoint(e.Location);
if (control != null)
{
string toolTipString = mFormTips.GetToolTip(control);
this.mFormTips.ShowAlways = true;
// trigger the tooltip with no delay and some basic positioning just to give you an idea
mFormTips.Show(toolTipString, control, control.Width / 2, control.Height / 2);
}
}

Related

Changing visiblity screwing up events in C#?

I have setup a Panel that has a number of Buttons on it. These Buttons Visible Property is set to false. When I move the mouse over the Panel they became Visible and when I move the mouse out of the Panel they once again became invisible. This all works fine.
The code to do this is:
private void _Display_MouseEnter(object sender, EventArgs e)
{
foreach (Control C in _Display.Controls)
{
C.Visible = true;
}
}
private void _Display_MouseLeave(object sender, EventArgs e)
{
foreach (Control C in _Display.Controls)
{
C.Visible = false;
}
}
The problem is the Events I have set for the Buttons seem to get removed by doing this. If I don't change the Buttons visiblity the events fire as normal. I have set the events in the Designer.
Am I missing something or is this how its supposed to work and I have to resubscribe my events each time I change the Buttons visiblity?
Thanks
Danny
I did some research on that and apparently when the cursor enters one of the buttons the Panel's MouseEnter and MouseLeave events fire until the cursor leaves that button so what it does is not allowing your buttons to catch the Click event.
I think the best solution would be to get the cursor position and check if it is within your panel and then set your buttons visibility.

Event getting triggered multiple times on scroll event handler

While I was adding code to scroll event of a panel in c#, I found a strange behavior.
I have added a panel and inside the panel (auto-Scroll = true) there is a groupbox.
As shown below clicking the scroll moves scroll bar to a small distance.
At the same time, when I add a message box in the event to display a notification that a scroll has taken place, multiple message box are popping out.
Why is that?
I have already planned to add some logic when scrolling, but if it occurs multiple times then how it could be possible?
Here is the event handler:
private void panel1_Scroll_1(object sender, ScrollEventArgs e)
{
MessageBox.Show("ScrollBar is clicked");
}
This is just how scroll event works, it fires many times while the panel is scrolling.
Try ScrollEventArgs.Type EndScroll which should be the last scroll event.
private void panel1_Scroll_1(object sender, ScrollEventArgs e)
{
if (e.Type == ScrollEventType.EndScroll)
MessageBox.Show("ScrollBar is clicked");
}
If above doesn't help for your case you will need to handle those multiple events by using one of approaches explains in this thread.

How to know what control the mouse has clicked in a canvas?

I am creating a C# WPF application and looking for a way to do the following:
I have a canvas with different user controls in it and a button.
When I click on the button the cursor change to a hand (Canvas.Cursor = Cursors.Hand)
Then if I click on one of the controls I get a message box showing the name of the control clicked (the name is a public property of the control).
If I click somewhere else i the cursor resets and I should click on the button again before I can get the name again.
I tried playing with events and handlers but couldn't achieve what I wanted.
Thank you very much for you help
You can use Canvas.MouseDown and use VisualTreeHelper.HitTest() with GetPosition() of the mouse down event args to get the element that was clicked.
<Canvas Name="myCanvas" MouseDown="MouseDownHandler" />
public void MouseDownHandler(object sender, MouseButtonEventArgs e)
{
HitTestResult target = VisualTreeHelper.HitTest(myCanvas, e.GetPosition(myCanvas));
while(!(target is Control) && (target != null))
{
target = VisualTreeHelper.GetParent(target);
}
// now if target is not null, it's the control that was clicked...
}
Then you can use VisualTreeHelper.GetParent() (in a while loop) to get the control that was clicked.

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

Scrolling problem with a WebBrowser control contained in a Panel control

I have a .Net Panel control that contains a single child control that is a WebBrowser. I won't go into the reasons for me doing that, but it is related to printing out the control. The panel control has its AutoScroll property set to "true" and I am sizing the WebBrowser to fit its own content (by using the .Document.Body.ScrollRectangle.Size property of the WebBrowser when the NavigateComplete2 event fires). In this way, the scrollbar on the panel appears and you can scroll the panel up and down in order to be able to see the content of the WebBrowser.
The problem is that when you scroll down to see what's at the bottom of the WebBrowser and then click on it (perhaps you click on a link in the html), the panel jumps back to the top and the link doesn't get actioned.
Please can anyone help me to understand what's going on and how to get around this problem?
I had the same problem, also with a WebBrowser inside a Panel. Here's the solution I'm using (which I found somewhere else on stackoverflow):
class AutoScrollPanel : Panel
{
public AutoScrollPanel()
{
Enter += PanelNoScrollOnFocus_Enter;
Leave += PanelNoScrollOnFocus_Leave;
}
private System.Drawing.Point scrollLocation;
void PanelNoScrollOnFocus_Enter(object sender, System.EventArgs e)
{
// Set the scroll location back when the control regains focus.
HorizontalScroll.Value = scrollLocation.X;
VerticalScroll.Value = scrollLocation.Y;
}
void PanelNoScrollOnFocus_Leave(object sender, System.EventArgs e)
{
// Remember the scroll location when the control loses focus.
scrollLocation.X = HorizontalScroll.Value;
scrollLocation.Y = VerticalScroll.Value;
}
protected override System.Drawing.Point ScrollToControl(Control activeControl)
{
// When the user clicks on the webbrowser, .NET tries to scroll to
// the control. Since it's the only control in the panel it will
// scroll up. This little hack prevents that.
return DisplayRectangle.Location;
}
}
try setting TabStop to false on both the containing panel as well as the WebBrowser control. That did the trick for me. The reason this works is that if it's set as a wanting to be a tabstop, it will take the first click event to mean that it's receiving focus. This then resets the scroll bar positions... not sure why it does that...
However, on navigating to a new page you'll need to manually reset the scroll bar positions...
Here is what I used. Yeah it's a hack.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
webBrowser1.TabStop = true;
webBrowser1.Focus();
webBrowser1.TabStop = false;
}

Categories

Resources