我可以在ToolTip.Show上修改我的代码吗? [英] Can my codes on ToolTip.Show be corrected for me please?

查看:88
本文介绍了我可以在ToolTip.Show上修改我的代码吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

我正在使用窗口表单应用程序,这是我的一组代码。

Hi everyone!
I'm working on a window form app and this is a set of my codes.

private void ToolTips(Control A)
{
    ToolTip toolTip = new ToolTip();
    toolTip.Show(A.Tag.ToString(), A);
}

private void pictureBox_MouseHover(object sender, EventArgs e)
{
    ToolTips(sender.???????);
}



我打算只与我的表格上的每个控件共享一个MouseHover事件。

请帮帮我。

谢谢。


I intend to share only one MouseHover Event with every control I have on my form.
Please help me out.
Thanks.

推荐答案

在鼠标悬停事件中,调用System.Windows.Forms.ToolTip.Show方法之一(就像你一样)已经完成):

http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.show%28v=vs.110%29.aspx [ ^ ]。



但你的问题不同了。显然,你对编程基础很困惑,对方法,变量,成员,实例和其他基本内容都不太了解。您的 toolTip 对象由局部变量引用;调用 ToolTips 类后,它会脱离上下文;并且一次又一次地创建对象并最终被GC破坏。你不需要这种方法。相反,您可以创建类型 ToolTip 类的实例私有成员,只需一个;并在每次要显示一些文本时调用其显示方法。



-SA
On your mouse hover event, call one of the System.Windows.Forms.ToolTip.Show methods (as you already done):
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.show%28v=vs.110%29.aspx[^].

But your problem is different. Apparently, you are well confused with programming basics, don't know well about methods, variable, members, instances and other elementary stuff. Your toolTip object is referenced by a local variable; it goes out of context after you call the ToolTips class; and the object is created again and again and eventually destroyed by GC. You don't need this method. Instead, you can create an instance private member of your class of the type ToolTip, just one; and call its Show method each time you want to show some text.

—SA


除了谢谢你给你的优秀建议之外,在这里,我想补充说,一旦你创建了你的工具提示(一次),它就可以重复使用了:那么你可以像这样写你的MouseHover EventHandler:
In addition to the excellent advice Sergey gave you, here, I'd like to add that once you do get your ToolTip created (once), and it's reusable: then you can write your MouseHover EventHandler like this:
private void pictureBox_MouseHover(object sender, EventArgs e)
{
    Control theControl = sender as Control;

    if(theControl != null)
    {
        ToolTips(theControl);
    }
}

虽然你可以正确地假设测试'确实不需要',但实现它不会对任何事情造成伤害,如果有的话可能只是有助于调试问题。



我假设您要为链接到此MouseHover EventHandler的每个Control分配一个Tag属性...正确吗?

While you can correctly assume the test for 'null is not really needed, implementing it won't hurt anything and may just be helpful in debugging if there is a problem.

I assume that you are assigning every Control that you link to this MouseHover EventHandler a Tag property ... correct ?


这篇关于我可以在ToolTip.Show上修改我的代码吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆