[Closed] ToolTip for .NET control
i’m absolutely sure that many of you know how to set a tooltip on a .net control. but there is a question for everyone who likes a brain-twisting:
- how to get the tooltip object that the control associated with?
condition: don’t use the control’s tag property because in general case it might be used for anything else.
have a good weekend!
You mean search the controls children to find the tooltip object?
why do you think that the tooltip is a child of the control? it’s not. just try to make it a child…
Are you looking for a ‘default tooltip’ (if there is such a thing in .NET Controls), then you might do it with reflection if it’s a private property. But I assume this is not what you’re looking for.
Finding any tooltip, that’s a tricky one. I guess you’d have to search the heap for tooltip objects which reference the control.
i spent almost two days looking for the solution. it was a great challenge for me, and it was really the fun.
is there a benefit to getting the actual tooltip object or is this just for the challenge?
it’s a benefit… the reason was for me to dynamically change the tooltip based on the state of the control.
the another benefit is… let think… who is responsible for the disposing of a tooltip object if the object is not a child of anything?
the complete solution is… but let’s start from something simpler.
here is a snippet to make a controls with tooltips by most common way:
try(form.close()) catch()
(
global form = dotnetobject "Form"
form.Text = "ToolTips"
form.BackColor = form.BackColor.DarkGray
form.ShowIcon = form.ShowInTaskbar = form.MaximizeBox = form.MinimizeBox = off
form.StartPosition = form.StartPosition.Manual
form.Location = dotnetobject "System.Drawing.Point" 600 100
form.Size = dotnetobject "System.Drawing.Size" 250 90
form.FormBorderStyle = form.FormBorderStyle.FixedDialog
ttIcons = #((dotnetclass "ToolTipIcon").None, (dotnetclass "ToolTipIcon").Error, (dotnetclass "ToolTipIcon").Info, (dotnetclass "ToolTipIcon").Warning)
--ttBacks = #(form.Backcolor.White, form.Backcolor.Pink, form.Backcolor.Cyan, form.Backcolor.Yellow)
tt = dotnetobject "ToolTip"
tt.ToolTipTitle = "Button"
btt = for k=0 to 3 collect
(
local bt = dotnetobject "Button"
bt.Text = "Button" + k as string
bt.BackColor = bt.BackColor.Transparent
bt.Bounds = dotnetobject "System.Drawing.Rectangle" (k*60+2) 4 60 24
tt.SetToolTip bt (" Press the Button - " + k as string)
bt
)
bts = for k=0 to 3 collect
(
local bt = dotnetobject "Button"
bt.BackColor = bt.BackColor.Transparent
bt.Bounds = dotnetobject "System.Drawing.Rectangle" (k*60+2) 34 60 24
local tt = dotnetobject "ToolTip"
tt.ToolTipIcon = ttIcons[k+1]
--tt.Backcolor = ttBacks[k+1]
tt.ToolTipTitle = bt.Text = tt.ToolTipIcon.ToString()
tt.SetToolTip bt " Press the Button"
bt
)
form.controls.addrange btt
form.controls.addrange bts
MaxHandleWrapper = dotnetobject "MaxCustomControls.Win32HandleWrapper" (dotnetobject "System.IntPtr" (windows.getmaxhwnd()))
form.Show MaxHandleWrapper
ok
)
try to reorganize the code to keep tracking (referencing) of all control-tooltip-control associations.
that means asking the form be able to get all tooltip objects, asking a tooltip object get all associated controls, asking a control get all tooltip objects that the control associated with.
The code that created the tooltip object, of course. But, if you write the code that creates the tooltip, you can keep a reference to it, maybe in a dictionary or something. That would avoid having to do tricks. Still, nice challenge