1

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

我以编程方式将 PictureBox 添加到 FlowLayoutPanel。效果很好。然后我选择其中一个图片框来设置工具提示和 .. 繁荣!应用挂起:(

如果我在第一次创建每个图片框并将其添加到 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);
            }
         }
    }
}
4

1 回答 1

0

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

So remove the line:

ToolTip toolTip = new ToolTip();

from your loop and put it in the constructor or other initialiser code.

UPDATE

Looking at the new code I can't see anything obviously wrong.

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.

于 2009-07-14T09:13:56.400 回答