在WinForms / C#桌面应用程序(使用.NET Framework 4.5版)上设置工具提示时,我遇到以下问题。 应用程序具有数百个要在其中显示工具提示的表单元素。
当前实现如下:
例如
toolTip.SetToolTip(backBtn, LocalizationFile[0]);
toolTip.SetToolTip(myTextBox, LocalizationFile[1]);
它工作正常,工具提示正确显示。 我一到达ca 30次通话停止工作。 调用约30次SetToolTip方法来设置所需的工具提示,导致完全停止显示工具提示。 以前工作的工具提示文本不再显示。 没有异常或任何错误消息。
您能解释一下为什么多次调用SetToolTip方法后toolTip对象只是停止显示文本吗?在这种情况下是否有解决方法可以应用?
EDIT-1 以下解决方法是可行的,但是我仍然不确定最初的问题是什么。 我创建了一个在toolTip对象上调用SetToolTip的方法,调用该方法后,我使用“ new”运算符重新创建了ToolTip实例。这样就解决了问题。但是,由于我想随时禁用应用程序上的工具提示,因此我还将所有引用存储在列表中。在该列表上,我可以根据用户的需要进行迭代以启用/禁用所有工具提示引用。 基本上有一个按钮可以切换工具提示。
您有什么想法,为什么这实际上可以解决原始问题?
List<ToolTip> storeToolTipReferences = new List<ToolTip>(); //store tooltip references
//method begin called to set the tooltip on a control, store the reference of the tooltip and create a new instance
private void SetMyToolTip(Control ctrl, string toolTipText)
{
toolTip.SetToolTip(ctrl, toolTipText);
storeToolTipReferences.Add(toolTip);
toolTip = new ToolTip();
}
//called as the application loads or the user changes the language
private void SetAppLanguage(string[] LocalizationText)
{
storeToolTipReferences.ForEach(e => e.RemoveAll());
SetMyToolTip(ctrl1, LocalizationText[1]);
SetMyToolTip(ctrl2, LocalizationText[2]);
SetMyToolTip(ctrl3, LocalizationText[3]);
.....
}
//logic for tooltip enable/disable in my application
storeToolTipReferences.ForEach(t => t.Active = tooltip_control.Checked);
谢谢!