如何使用代码 enable/disable 工具提示
How to enable/disable tool tips with code
如何使用代码 enable/disable 工具提示。
我可以在表单加载时使用代码为按钮启用工具提示,但无法使用复选框代码关闭工具提示。
private void Form1_Load(object sender, EventArgs e)
{
// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();
// Set up the ToolTip text for a Button.
toolTip1.SetToolTip(this.btnRunExtApp, "Run the external application");
}
完美运行。
我现在正在尝试让用户选择 on/off 带有复选框的工具提示。
private void ChkEnableTips_CheckedChanged(object sender, EventArgs e)
{
ToolTip toolTip1 = new ToolTip();
if (ChkEnableTips.Checked == true)
{
toolTip1.SetToolTip(this.btnRunExtApp, "Run the external application");
}
if (ChkEnableTips.Checked == false)
{
toolTip1.SetToolTip(this.btnRunExtApp, null);
}
}
可能是因为我又声明了toolTip1?
我试图将其更改为 toolTip2 但这也不起作用。
但是如果我根本没有在 ChkEnableTips_CheckedChanged 中声明它,那么我会得到一个错误(名称 'toolTip1' 在当前上下文中不存在)。
是的,因为你又声明了。写到你的顶部class一个全局变量。
ToolTip toolTip1 = new ToolTip();
然后事件(也是构造函数)可以使用它:
private void ChkEnableTips_CheckedChanged(object sender, EventArgs e)
{
if (ChkEnableTips.Checked == true)
toolTip1.SetToolTip(this.btnRunExtApp, "Run the external application");
if (ChkEnableTips.Checked == false)
toolTip1.SetToolTip(this.btnRunExtApp, null);
}
不要在 Load
事件中设置 toolTip1
,而是转到设计器视图并将您的复选框设置为默认勾选。这无论如何都会引发事件,上面的处理程序会为您完成这项工作。同样,通过这种方式,您可以避免第二次调用 SetToolTip()
.
(顺便说一句,当你想在 constructor
中初始化时,一些关于你未来设计的信息 - 而不是 Load
-:字段初始化发生在构造函数中的行之前。)
编辑:
也许 工具箱 中有一个名为 ToolTip 的组件。您可以拖放。它不会改变任何东西!只需移动这一行:
ToolTip toolTip1 = new ToolTip();
到designer.cs
如何使用代码 enable/disable 工具提示。 我可以在表单加载时使用代码为按钮启用工具提示,但无法使用复选框代码关闭工具提示。
private void Form1_Load(object sender, EventArgs e)
{
// Create the ToolTip and associate with the Form container.
ToolTip toolTip1 = new ToolTip();
// Set up the ToolTip text for a Button.
toolTip1.SetToolTip(this.btnRunExtApp, "Run the external application");
}
完美运行。
我现在正在尝试让用户选择 on/off 带有复选框的工具提示。
private void ChkEnableTips_CheckedChanged(object sender, EventArgs e)
{
ToolTip toolTip1 = new ToolTip();
if (ChkEnableTips.Checked == true)
{
toolTip1.SetToolTip(this.btnRunExtApp, "Run the external application");
}
if (ChkEnableTips.Checked == false)
{
toolTip1.SetToolTip(this.btnRunExtApp, null);
}
}
可能是因为我又声明了toolTip1? 我试图将其更改为 toolTip2 但这也不起作用。 但是如果我根本没有在 ChkEnableTips_CheckedChanged 中声明它,那么我会得到一个错误(名称 'toolTip1' 在当前上下文中不存在)。
是的,因为你又声明了。写到你的顶部class一个全局变量。
ToolTip toolTip1 = new ToolTip();
然后事件(也是构造函数)可以使用它:
private void ChkEnableTips_CheckedChanged(object sender, EventArgs e)
{
if (ChkEnableTips.Checked == true)
toolTip1.SetToolTip(this.btnRunExtApp, "Run the external application");
if (ChkEnableTips.Checked == false)
toolTip1.SetToolTip(this.btnRunExtApp, null);
}
不要在 Load
事件中设置 toolTip1
,而是转到设计器视图并将您的复选框设置为默认勾选。这无论如何都会引发事件,上面的处理程序会为您完成这项工作。同样,通过这种方式,您可以避免第二次调用 SetToolTip()
.
(顺便说一句,当你想在 constructor
中初始化时,一些关于你未来设计的信息 - 而不是 Load
-:字段初始化发生在构造函数中的行之前。)
编辑:
也许 工具箱 中有一个名为 ToolTip 的组件。您可以拖放。它不会改变任何东西!只需移动这一行:
ToolTip toolTip1 = new ToolTip();
到designer.cs