views:

515

answers:

1

Hi folks,

I'm trying to set a ToolTip onto a control and it's hanging my application.

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 :(

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.

here's the code :-

// 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.
    }
}

Any ideas? is it how I retrieve the reference to the existing PictureBox instance?

UPDATE:

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

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.

ChrisF
I placed the tooltip as a private variable that is only instantiated once. Then removed the line, as noted. Problem still exists :(
Pure.Krome
Can you post the relevant parts of your edited code in your question.
ChrisF
Done - updated the initial post.
Pure.Krome