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

3

20

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?

Ariellearies answered 30/8, 2011 at 3:57 Comment(2)
+1 to et you back to zero, seems like a totally acceptable question. BTW: what tools are you using to see the ToolTip memory leak?Vitia
I'm using ANTS Memory Profiler. Here's a similar question i asked with screenshots: #7226459Ariellearies
G
32

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.

Generosity answered 30/8, 2011 at 4:16 Comment(2)
Problem with that is that I don't know how many I need. It's based upon some dynamic data. So i need to make them dynamically.Ariellearies
You only need one. One ToolTip component can provide tips for thousands of controls. When you call toolTip.SetToolTip(Control, Text), it adds the control and text to an internal collection and handles the control's mouse events. When you hover over a control, the ToolTip component looks up the associated text. To remove an entry from this collection, call toolTip.SetToolTip(Control, null)Generosity
S
6

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>");
Selfwinding answered 30/8, 2011 at 5:32 Comment(0)
T
2
  1. You only need one ToolTip instance per form.

  2. toolTip.SetToolTip(control, caption) - can use with many control, you can set caption for each control

  3. toolTip.ToolTipTitle - set tool tip title, the title is one for all control bonded with tool tip

for example :

public Form1()
{
    InitializeComponent();
    toolTip1.SetToolTip(button1, "btn1");
    toolTip1.SetToolTip(button2, "btn2");
    toolTip1.SetToolTip(button3, "btn3");
}


private void button4_Click(object sender, EventArgs e)
{
    toolTip1.ToolTipTitle = textBox1.Text;
}

enter image description here

Tacheometer answered 16/11, 2018 at 19:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.