在 C# 中显示文本弹出窗口
本文关键字:窗口 文本 显示 | 更新日期: 2023-09-27 18:33:20
在表单中,我有一个标签,但标签文本超出了面板边界,当鼠标悬停在标签上一段时间时,我该怎么办 显示包含所有文本的弹出窗口?
编辑:这里的代码:
public partial class AppMenuItem : UserControl
{
public string path;
public string name
{
get { return label1.Text; }
}
public AppMenuItem(string path, string name)
{
InitializeComponent();
label1.Text = name;
this.path = path;
pictureBox1.Image = ShortcutsHelper.GetIcon(path);
}
private void pinToStartToolStripMenuItem_Click(object sender, EventArgs e)
{
TilesHelper.AddTile(this.name, this.path);
}
private void label1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
contextMenuStrip1.Show(this.PointToScreen(Point.Empty));
}
else
{
System.Diagnostics.Process.Start(path);
((AppForm)this.ParentForm).DialogResult = DialogResult.OK;
((AppForm)this.ParentForm).Close();
((AppForm)this.ParentForm).textBox1.Text = "";
}
}
private void label1_MouseHover(object sender, EventArgs e)
{
new ToolTip().Show(this.name, this.ParentForm);
}
}
您是否正在尝试添加工具提示?
var fullText = "quick brown fox jumps over the lazy dog";
System.Windows.Forms.ToolTip tt = new System.Windows.Forms.ToolTip();
tt.SetToolTip(this.label1, fullText);
编辑:您应该删除MouseHover
事件,并将工具提示添加到构造函数中的标签:
public AppMenuItem(string path, string name)
{
InitializeComponent();
label1.Text = name;
this.path = path;
pictureBox1.Image = ShortcutsHelper.GetIcon(path);
ToolTip tt = ToolTip();
tt.SetToolTip(label1, name);
}
您必须创建一个 ToolTIp 对象并设置所需的文本:
System.Windows.Forms.ToolTip ToolTip1 = new System.Windows.Forms.ToolTip();
ToolTip1.SetToolTip(this.Label1, this.Label1.Text);
检查 MSDN: http://msdn.microsoft.com/en-us/library/system.windows.forms.label.autoellipsis.aspx