PictureBox Tooltip changing C#
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.
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");
}
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'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.
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();
}
精彩评论