Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the text of an existing ToolTip control, in a PictureBox in my WinForm application?

I have a winform application which has a dynamic number (based on a database value) of PictureBoxes. Each P-Box has a Tooltip control.

How can I change the ToolTip Text without having any memory leaks? Right now, I've got the following code, but it's leaking memory => the previous ToolTip controls are not getting GC'd.

BTW, this is a background thread that is trying to update the main UI thread....

if (pictureBox == null || !pictureBox.IsHandleCreated) {
    continue;
}

Action setTooltipAndImage = () => {
    var toolTip = new ToolTip();
    GameServer tempGameFile = gameServer;
    toolTip.SetToolTip(pictureBox, string.Format(...));
    pictureBox.Image = Resources.RedButton;
};

if (pictureBox.InvokeRequired) {                        
    pictureBox.Invoke(setTooltipAndImage);
} else {
    setTooltipAndImage();
}

As I said - this works but it's leaking.

Anyone have any suggestions?

like image 875
Pure.Krome Avatar asked Aug 30 '11 03:08

Pure.Krome


People also ask

How do I set ToolTip to control pragmatically in Windows form application?

In Visual Studio, add a ToolTip component to the form. Select the control that will display the ToolTip, or add it to the form. In the Properties window, set the ToolTip on ToolTip1 value to an appropriate string of text.

How do I use ToolTip in winform?

From within the designer: 1) From the ToolBox, drop a ToolTip control to your form. 2) Check the properties of this toolTip1 object to make sure Active is set to true. 3) Click on your button, and in its Property page, set the ToolTip on toolTip1 entry.

How do you write ToolTip in text?

Basic Tooltip HTML: Use a container element (like <div>) and add the "tooltip" class to it. When the user mouse over this <div>, it will show the tooltip text. The tooltip text is placed inside an inline element (like <span>) with class="tooltiptext" .

What is the use of ToolTip control?

This property is used to get or set the period of time the ToolTip remains visible if the pointer is stationary on a control with specified ToolTip text. This property is used to get or set the background color for the control. This property is used to get or set the foreground color of the control.


2 Answers

Don't create a new ToolTip each time. Add a ToolTip to the form using the visual designer, like you would for any other control or component. Call toolTip.SetToolTip(...) on the form's tool tip each time. The ToolTip will be disposed when the Form is disposed.

like image 117
Hand-E-Food Avatar answered Oct 23 '22 23:10

Hand-E-Food


Yes, you do not need to create a new ToolTip each time, a single ToolTipwill do. There is no issue if you do not know how many ToolTips you want, because if there is only one ToolTip say toolTip1, then you can use the following every time you want to change the ToolTip caption and control on some event. You only need one ToolTip instance per form.

toolTip1.SetToolTip(Current_pictureBox, "<tool tip string>");
like image 5
Shweta patel Avatar answered Oct 23 '22 22:10

Shweta patel