WinForm ToolTip.SetToolTip挂着我的应用程序:(

时间:2009-07-13 14:04:52

标签: .net winforms tooltip

我正在尝试将ToolTip设置到控件上,并且它正在挂起我的应用程序。

我以编程方式将PictureBox添加到FlowLayoutPanel。效果很好。然后我挑出一个PictureBoxes来设置ToolTip和..繁荣!应用程序挂起:(

如果我将ToolTip设置在我首次创建每个图片框并将其添加到flowlayoutpanel的位置,它就不会挂起并且会正确显示/呈现。

这是代码: -

// 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实例的引用?

更新

根据要求,以下代码我已经改变了..

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);
            }
         }
    }
}

1 个答案:

答案 0 :(得分:0)

您只需要一个Tooltip作为类变量和您的调用:

toolTip.SetToolTip(pictureBox, 
    string.Format("Started Parsing On: {0}", e.DateTimeOffset));

应该正常工作。我成功地使用了这个

删除该行:

ToolTip toolTip = new ToolTip();

从循环中将其放入构造函数或其他初始化代码中。

<强>更新

查看新代码我看不出任何明显错误。

我只能建议您从工具提示的设置中拆分字符串的构建。可能是e.DateTimeOffset导致挂起&amp;这样可以验证。