28

Ok so I am a complete beginner and have managed to put together a small app in C# where I enter a username in a textbox and the application gets the avatar of that username and displays it in a picturebox.

What i want to do is have a tooltip show the username that was typed in the textbox when mouse hovers over the loaded avatar. it should change each time a new avatar is loaded. I know how to use tooltips the normal way but this is a bit complex for me. Any help will be appreciated.

Thanks.

BrandNewDev
  • 755
  • 2
  • 9
  • 16

4 Answers4

46

Add a hover event to your picturebox with the following code.

private void pictureBox1_MouseHover(object sender, EventArgs e)
{
    ToolTip tt = new ToolTip();
    tt.SetToolTip(this.pictureBox1, "Your username");
}
Joe
  • 11,147
  • 7
  • 49
  • 60
  • it worked perfectly! i just swapped out "Your username" for textBox1.Text :D thank you! – BrandNewDev Feb 09 '11 at 10:41
  • 4
    glad to hear, it's one of those things that you wonder, now why didn't microsoft make a property called tooltip for the picturebox control... took me a while at first too – Joe Feb 09 '11 at 13:25
  • It works, but I got issues with multiple tooltips being displayed over each other. As an alternative, we can have one global `ToolTip` object and then use `ToolTip.Show()` rather than creating a new tooltip during each hover event ... ? See also [this answer](https://stackoverflow.com/a/17494690/8028981) – Amos Egel Apr 20 '23 at 08:55
23

Joes' answer does get the job done, but it is inefficient. The code creates a new ToolTip every time the PictureBox is hovered over. When you use the SetToolTip() method, it associates the created ToolTip with the specified control and keeps the ToolTip in memory. You only need to call this method once. I suggest you create only one ToolTip for each PictureBox in your forms constructor. I've tested this and it works fine, and the tool-tips show up faster for me:

public MyForm()
{
    InitializeComponent();

    // The ToolTip for the PictureBox.
    new ToolTip().SetToolTip(pictureBox1, "The desired tool-tip text.");
}
Brandon Miller
  • 2,247
  • 8
  • 36
  • 54
  • But does this approach allow to have a dynamic tooltip text that depends on the user input as asked in the original post? – Amos Egel Apr 20 '23 at 08:58
6

Brandon-Miller's answer is fine, except for the fact that he suggests to create one Tooltip per PictureBox. This is still ineffective, though better than Joe's approach - because you don't actually need more than one Tooltip object for the whole form! It can create thousands of 'tooltip definitions' (probably not an actual term) for different controls (bits and bobs on the forms). That's why you're defining the control as the first parameter when creating or setting the Tooltip.

The correct (or at least the least wasteful) approach, as far as I'm aware, is to create ONE Tooltip object per form and then use the SetTooltip function to create 'definitions' for different controls.

Example:

private ToolTip helperTip;
public MyForm()
{
    InitializeComponent();

    // The ToolTip initialization. Do this only once.
    helperTip = new ToolTip(pictureBox1, "Tooltip text");
    // Now you can create other 'definitions', still using the same tooltip! 
    helperTip.SetToolTip(loginTextBox, "Login textbox tooltip");
}

Or, slightly differently, with the initialization done beforehand:

// Instantiate a new ToolTip object. You only need one of these! And if
// you've added it through the designer (and renamed it there), 
// you don't even need to do this declaration and creation bit!
private ToolTip helperTip = new ToolTip();
public MyForm()
{
    InitializeComponent();

    // The ToolTip setting. You can do this as many times as you want
    helperTip.SetToolTip(pictureBox1, "Tooltip text");
    // Now you can create other 'definitions', still using the same tooltip! 
    helperTip.SetToolTip(loginTextBox, "Login textbox tooltip");
}

If you added the ToolTip in the Forms designer, you can omit the declaration at the beginning. You don't even need to initialize it (as far as I know, this should be done by the code generated by the designer), just use the SetToolTip to create new tooltips for different controls.

Jamie
  • 1,754
  • 16
  • 34
EvilGeorge
  • 61
  • 1
  • 2
  • +1 This is the best answer. You should be able to associate a Control with a ToolTip and the desired tooltip text in the designer without writing any code, or at least afaik (been awhile since I worked with Windows Forms.) – Brandon Miller May 22 '20 at 09:03
  • But does this approach allow to have a dynamic tooltip text that depends on the user input as asked in the original post? – Amos Egel Apr 20 '23 at 08:59
1
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        ToolTip tooltip1 = new ToolTip();
        tooltip1.Show(textBox1.Text, this.pictureBox1);
    }

      private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
    {
        pictureBox1.Invalidate();
    }
Rajesh Kumar G
  • 1,424
  • 5
  • 18
  • 30
  • I was just wondering... Why does the picture box get invalidated each time the mouse moves over it? In a typical situation, we could have the picture box being invalidated several times in a second... all in the name of updating the tooltip. This gross inefficiency is a serious non-starter. – Alex Essilfie Feb 09 '11 at 08:34