简体   繁体   English

图片框工具提示更改 C#

[英]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.好的,所以我是一个完整的初学者,并设法在 C# 中组合了一个小应用程序,我在文本框中输入用户名,应用程序获取该用户名的头像并将其显示在图片框中。

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.每次将鼠标悬停在PictureBox上时,代码都会创建一个新的ToolTip When you use the SetToolTip() method, it associates the created ToolTip with the specified control and keeps the ToolTip in memory.当您使用SetToolTip()方法,它关联的创建ToolTip使用指定的控制和保持ToolTip中的内存。 You only need to call this method once.你只需要调用一次这个方法。 I suggest you create only one ToolTip for each PictureBox in your forms constructor.我建议您只为表单构造函数中的每个PictureBox创建一个ToolTip 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. Brandon-Miller 的回答很好,只是他建议为每个图片框创建一个工具提示。 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!这仍然是无效的,虽然比 Joe 的方法更好 - 因为对于整个表单,您实际上并不需要一个以上的 Tooltip 对象! 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.据我所知,正确(或至少是最不浪费的)方法是为每个表单创建一个 Tooltip 对象,然后使用SetTooltip函数为不同的控件创建“定义”。

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.你甚至不需要初始化它(据我所知,这应该由设计器生成的代码来完成),只需使用 SetToolTip 为不同的控件创建新的工具提示。

        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();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM