I try to set the tooltip for a custom UserControl I created.
The UserControl contains a single text box and surrounding canvas area. The reason behind doing this is to enable an illusion of a vertically centered TextBox with attached label. The label is being created via TextRenderer.DrawText, so the Tooltip works when hovering over the heading text as I am really hovering over the UserControl canvas in that case.
When being rendered, it looks like this:
As expected, the tooltip (which is assigned on the containing form) only displays when the mouse pointer is over the UserControl and not when over the TextBox in the UserControl.
This is the designer generated code of the containing form:
'
' usrCtrlTxtBox
'
usrCtrlTxtBox.BackColor = SystemColors.Window
usrCtrlTxtBox.BorderStyle = BorderStyle.FixedSingle
resources.ApplyResources(usrCtrlTxtBox, "usrCtrlTxtBox")
usrCtrlTxtBox.Name = "usrCtrlTxtBox"
ToolTip1.SetToolTip(usrCtrlTxtBox, resources.GetString("usrCtrlTxtBox.ToolTip"))
I now want to assign the same tooltip text to the contained TextBox inside the user control. For this I somehow need to access the tooltip set for the UserControl itself. After browsing through the .Net source code I discovered that the Tooltip1.SetTooltip command finally calls a method called SetToolTipToControl (lines 1280 to 1286 in Tooltip.cs) which ultimately calls this method (lines 10941 to 10944 in Control.cs):
internal virtual void SetToolTip(ToolTip toolTip)
{
// Control doesn't have a specific logic after a toolTip is set
}
So I thought it would be a good idea to override this method with a custom Sub in my UserControl:
Protected Overrides Sub SetToolTip(outerTooltip As ToolTip)
ToolTipInner.SetToolTip(txtBox, outerTooltip.GetToolTip(Me))
'txtBox is the inner TextBox of the UserControl
'TooltipInner is a ToolTip-Control inside the UserControl
End Sub
Sadly this doesn't work as planned as the compiler complains about:
BC31417
'Protected Overrides Sub SetToolTip(outerTooltip As ToolTip)' cannot override 'Friend Overridable Overloads Sub SetToolTip(toolTip As ToolTip)' because it is not accessible in this context.
I am not very familiar with C# but I thought I would be able to override internal virtual methods as UserControl ultimately inherits from Control if I am not mistaken. I am out of ideas here and would appreciate any help. Any possibilty to access the UserControls tooltip from inside the Control itself?
Edit / addition:
As the UserControl will be used on many different forms with many different possible headings and tooltip texts, these strings aren't static but are being set by the containing form of the UserControl. Due to the need to translate these texts, I use RESX files for each of the forms, where the text being set to each instance of the UserControl is being translated during the InitializeComponent phase of the form.
Summary:
I try overriding the SetToolTip method from inside my UserControl to access the outer tooltip of the user control. I want to avoid constructs like Reflection and Shenanigans like Me.FindForm().Controls.Find("ToolTip1").



Solution 1:
Create the following
Subinside yourUserControl:Create a custom component, inheriting the built-in
ToolTipcomponent:Use this custom
ToolTipon your forms instead of the regularToolTipcomponent.The designer will create the usual code for you when assigning tooltips to your controls:
This will call the overridden
Subwhich then will call theSubon yourUserControl.