Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
238 views
in Technique[技术] by (71.8m points)

c# - ToolTipText popping up underneath control

I've got a .NET app with a NotifyIcon that sits in the systray. That nic has a ContextMenuStrip and that ctx mnu has several ToolStripMenuItems in it. Most of them have their ToolTipText set at run-time. The problem is that most of the time, the ttp pops up UNDER the mnu item. It will either be mostly or entirely obscured by the mnu item itself. Depending on where I move my mouse, sometimes the ttp pops up over the mnu and you can see it entirely, but most of the time it's not.

How it's even possible for a ttp to pop up under its control (thus rendering it useless) is beyond me, but does anyone know how to stop this behavior?

Here's the code that sets it. Pretty straight-forward:

Dim mnu As ToolStripMenuItem = ctmMain.Items.OfType(Of ToolStripMenuItem).Where(Function(m) m.Tag IsNot Nothing AndAlso m.Tag = "EM_" & Account).First
mnu.ToolTipText = dt.Rows(0)("Display")

I've tried cycling ShowItemToolTips on the ctx mnu (and a few other random things), but nothing changes this behavior. I either need to fix this, or find some simple alternative to the ToolStripMenuItem.ToolTipText.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Well, I figured this out midstream but I figured I'd post it for anyone else who runs into this behavior. ToolTipText wasn't viable no matter what I tried, but I was able to add a ToolTip control to the main form and hijack that. I set the ContextMenu's ShowItemToolTips to False and handled the display of the ToolTipText text manually.

You can't use a ToolTip control with a ToolStripMenuItem (because the latter isn't a control), however you can use it with a ContextMenuStrip, which is the Parent control of the mnu itms. So, I added Handlers for the MouseEnter and MouseLeave events of each ToolStripMenuItem and used those to display/hide the ToolTipText for each mnu item. This got rid of the pop-under issue for the most part. There are still occasional odd behaviors, but it's a viable solution.

Sub LoadMenus(acct As ToolStripMenuItem)
    AddHandler acct.MouseEnter, AddressOf EMToolTipShow
    AddHandler acct.MouseLeave, AddressOf EMToolTipHide
End Sub
Private Sub EMToolTipShow(sender As Object, e As EventArgs)
    ttpEM.Show(sender.ToolTipText, sender.GetCurrentParent())
End Sub
Private Sub EMToolTipHide(sender As Object, e As EventArgs)
    ttpEM.Hide(sender.GetCurrentParent())
End Sub

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...