WinForm ToolTip.SetToolTip挂起我的应用程序:( [英] WinForm ToolTip.SetToolTip is Hanging my application :(
问题描述
我正在尝试将 ToolTip
设置到控件上,并且它挂起了我的应用程序.
I'm trying to set a ToolTip
onto a control and it's hanging my application.
我以编程方式将PictureBox添加到FlowLayoutPanel.效果很好.然后,我选择一个PictureBox来设置工具提示和..吊杆!应用已挂起:(
I programatically add PictureBox's to a FlowLayoutPanel. Works great. I then pick out one of the PictureBoxes to set the ToolTip and .. boom! app hung :(
如果我在第一次创建每个图片框并将其添加到flowlayoutpanel的位置设置了工具提示,则它不会挂起并且可以正确显示/渲染.
If I set the ToolTip at the point where i first create each picturebox and add it to the flowlayoutpanel, it doesn't hang and it is displayed/rendered correctly.
这是代码:-
// Toggle the button to green.
var pictureBoxs = flowLayoutPanel1.Controls.Find("Image_" + FileId, true);
if (pictureBoxs.Length > 0 &&
pictureBoxs[0] is PictureBox)
{
var pictureBox = pictureBoxs[0] as PictureBox;
if (pictureBox != null)
{
pictureBox.Image = Resources.GreenButton;
ToolTip toolTip = new ToolTip();
// Hangs after this line
toolTip.SetToolTip(pictureBox, "Started Parsing On: " +
DateTimeOffset.Now);
int i=0; i++; // NEVER GETS CALLED.
}
}
有什么想法吗?是如何检索对现有PictureBox实例的引用?
Any ideas? is it how I retrieve the reference to the existing PictureBox instance?
根据要求,我更改了以下代码.
As requested, this the following code i've changed..
public partial class Form1 : Form
{
... <snip>various private fields</snip>
private ToolTip _toolTip; // Added this.
...
private void InitialiseStuff()
{
PictureBox pictureBox = new PictureBox
{
Image = Resources.RedButton,
Name = "Image_" + someId,
Width = 35
};
_toolTip = new ToolTip();
_toolTip.SetToolTip(pictureBox, "Haven't yet parsed this file...");
flowLayoutPanel1.Controls.Add(pictureBox);
}
private void foo_OnStartParsingData(object sender, DateTimeEventArgs e)
{
... <snip>some boring code</snip>
// Toggle the button to green.
var pictureBoxes = flowLayoutPanel1.Controls.Find("Image_" +
someId, true);
if (pictureBoxes.Length > 0)
{
var pictureBox = pictureBoxes[0] as PictureBox;
if (pictureBox != null)
{
pictureBox.Image = Resources.GreenButton;
// Hangs after it runs the line below.
_toolTip.SetToolTip(pictureBox,
"Started Parsing On: " + e.DateTimeOffset);
}
}
}
}
推荐答案
您只需要一个 Tooltip
作为类变量,然后调用即可:
You just need one Tooltip
as a class variable and the your call:
toolTip.SetToolTip(pictureBox,
string.Format("Started Parsing On: {0}", e.DateTimeOffset));
应该工作.我已经成功使用了,所以
should just work. I've used this successfully so
因此删除该行:
ToolTip toolTip = new ToolTip();
从循环中将其放入构造函数或其他初始化程序代码中.
from your loop and put it in the constructor or other initialiser code.
更新
看着新代码,我看不到任何明显错误的地方.
Looking at the new code I can't see anything obviously wrong.
我只能建议您从工具提示的设置中拆分字符串的构建.可能是 e.DateTimeOffset
导致了挂起&这将证实这一点.
I can only suggest that you split the building of the string from the setting of the tooltip. It could be that e.DateTimeOffset
is causing the hang & this would verify that.
这篇关于WinForm ToolTip.SetToolTip挂起我的应用程序:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!