【问题标题】:WinForm ToolTip.SetToolTip is Hanging my application :(WinForm ToolTip.SetToolTip 正在挂起我的应用程序 :(
【发布时间】:2010-11-02 06:48:45
【问题描述】:

我正在尝试将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);
            }
         }
    }
}

【问题讨论】:

  • 一些旁白:它的盒子不是盒子。此外,您测试 pictureBoxs[0] 是否是 PictureBox 的次数过多。最后,为什么要为每个图片框创建一个新的工具提示?您只需要一个工具提示。您可以将多个控件分配给同一个工具提示。
  • 那么我该如何更改工具提示?而不是创建一个新的然后设置那个?我不知道如何获取现有工具提示的实例......(它没有 id?)
  • 为什么不让工具提示成为包含流布局面板的表单或用户控件的成员变量?您可以从组件菜单将其添加到设计器中。然后,您可以重复调用 tooltip.SetToolTip
  • 我可以,但我想看看是否有其他方法。 (我通常不喜欢这样做,除非我真的必须这样做)。

标签: .net winforms tooltip


【解决方案1】:

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

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

应该可以工作。我已经成功使用了这个

所以删除该行:

ToolTip toolTip = new ToolTip();

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

更新

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

我只能建议您将字符串的构建与工具提示的设置分开。可能是 e.DateTimeOffset 导致挂起,这将验证这一点。

【讨论】:

  • 我将工具提示放置为一个只实例化一次的私有变量。然后删除线,如前所述。问题依然存在:(
  • 你能在你的问题中发布你编辑的代码的相关部分吗?
猜你喜欢
  • 2013-01-16
  • 1970-01-01
  • 2011-05-10
  • 2010-11-16
  • 2010-10-12
  • 1970-01-01
  • 2013-08-23
相关资源
最近更新 更多