我试图找到一种有效的工具提示方法,该方法涉及将字符串注入(inject)表单底部的状态栏。我有两种方法,mouseEnter 和 mouseLeave,我想用它们来统一显示与我为其设置的任何随机用户控件关联的字符串。
mouseLeave 很简单:
private void mouseLeave(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = "";
}
但是 mouseEnter 要困难得多,因为每个控件都需要自己的字符串。如果需要,我可以将它们硬编码到程序中,我只是希望它能够工作,而不需要为每个对象使用不同的方法。
我正在尝试的一种方法如下所示(注释行均无效,但仅显示我的进度):
private void mouseEnter(object sender, EventArgs e)
{
//toolStripStatusLabel1.Text = pictureBoxLogo.GetAccessibilityObjectById(AccessibleDescription);
//toolStripStatusLabel1.Text = sender.ToString();
}
使用 GetAccessibilityObjectById 时,我收到错误消息:
Error 23 Cannot access protected member 'System.Windows.Forms.Control.GetAccessibilityObjectById(int)' via a qualifier of type 'System.Windows.Forms.PictureBox'; the qualifier must be of type '.MainForm' (or derived from it) \formMain.cs 59 59
并且在使用 sender.ToString(); 方法时,我在状态栏中得到了一个不连贯的文本字符串。这根本不是要使用的正确属性。
是否有我可以使用的所有控件的一个属性,然后我可以从它们中获取并作为字符串插入?如果没有,以任何方式将字符串分配给它们的最佳方法是什么,这样我就可以在 mouseEnter 中将其作为字符串获取?
谢谢。
请您参考如下方法:
如何利用 Control 类的 Tag 属性。因此,例如,在设计器中将 TextBox 的 Tag 设置为“This is my tooltip”。然后在 mouseEnter 中执行此操作:
var c = sender as Control;
if (c == null) { return; }
toolStripStatusLabel1.Text = Convert.ToString(c.Tag);
