我發現,部分基於@Shimmy's答案的問題Displaying tooltip on mouse hover of a text分辨率。我不得不使用toolTip1.SetToolTip(pictureBox1,TextString)函數。工具提示標題不能是多行。
需要定時器來避免閃爍。當用戶將鼠標移到PictureBox上時,工具提示顯示多行文本。
public partial class Form1 : Form
{
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.PictureBox pictureBox1;
System.Windows.Forms.Timer ToolTipsTimer = new System.Windows.Forms.Timer();
public Form1()
{
InitializeComponent();
ToolTipsTimer.Tick += new EventHandler(ToolTipsTimerEventProcessor);
ToolTipsTimer.Interval = 300;
ToolTipsTimer.Start();
toolTip1.AutomaticDelay = 300;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ToolTipsTimerEventProcessor(Object myObject, EventArgs myEventArgs)
{
ToolTipsTimer.Enabled = false;
}
string TextString = "First line\n Second line";
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (!ToolTipsTimer.Enabled)
{
toolTip1.SetToolTip(pictureBox1, TextString);
ToolTipsTimer.Enabled = true;
}
}
}
謝謝@LarsTech。我找到了解決方案,並通過工作示例添加了我自己的答案。 – Alexander
@Alexander這不是我的答案嗎? – LarsTech
是的,它很多,但我提供了完整的工作示例。此外,我認爲使用符號「\ n」打破字符串是最短的方式。當用戶將鼠標移動到PictureBox上時,我也遇到了閃爍問題。這也得到了解決。 – Alexander