0

Been trying to figure out the cause of why my balloon sizes are changed and the text in them is getting cropped as a result. I thought it had to do with re-setting the tip text for a control. Nope. It turns out it is the cancelling of popOpen. Once you cancel, it will adversely affect the next balloon to open.

Anyone have some brilliant ideas on a work-around?

Here's the code: (to reproduce the bug, run it and mouseOver l3, then l2, then l, then l3 - in that order).

public partial class Form7 : Form
{
    private Label l, l2, l3;

    public Form7()
    {
        InitializeComponent();

        toolTip1.IsBalloon = true;
        toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);

        l = new Label();
        l.Name="l";
        l.Text = "Label 1";
        l.Top = 100;
        l.Left = 100;

        l2 = new Label();
        l2.Name="l2";
        l2.Text = "Label 2";
        l2.Top = 150;
        l2.Left = 100;

        l3 = new Label();
        l3.Name = "l3";
        l3.Text = "Label 3";
        l3.Top = 200;
        l3.Left = 100;

        this.Controls.Add(l);
        this.Controls.Add(l2);
        this.Controls.Add(l3);

        toolTip1.SetToolTip(l, "Hello.");
        toolTip1.SetToolTip(l2, "This is longer.");
        toolTip1.SetToolTip(l3, "This is even longer than Label 2.");

    }

    void toolTip1_Popup(object sender, PopupEventArgs e)
    {
       Control c = e.AssociatedControl;

       if (c.Name == "l")
           e.Cancel = true;  // <--- This is the culprit!
       else
           e.ToolTipSize = new Size(400, 100);  //  <--- This sems to have no effect when isBalloon == true.
    }

}
Patrick
  • 17,669
  • 6
  • 70
  • 85
user1646737
  • 169
  • 7

1 Answers1

1

Sigh, those annoying ToolTip bugs. At least part of the reason appears to be that the native Windows control doesn't actually support canceling the popup. Winforms emulates it by setting the tooltip window size to 0 x 0 pixels. That appears the affect the outcome of the next popup, it seems to generate a window size that's calculated from that 0 x 0 size and assumes the text should be wrapped. But doesn't actually wrap the text.

A hack to solve the problem is to whack the native control after canceling so that it cannot memorize the size. This worked:

   if (c.Name == "l") {
       e.Cancel = true;
       this.BeginInvoke(new Action(() => {
           toolTip1.IsBalloon = !toolTip1.IsBalloon;
           toolTip1.IsBalloon = !toolTip1.IsBalloon;
       }));
   }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Well, if I could vote up your answer, I would. It says I do not have enough reputation points yet. That was a brilliant solution! Can you explain the nature of the use of BeginInvoke and how that operates? I tried to use your !IsBalloon statements without the BeginInvoke, and naturally, it does not work. I'd like to know more about this BeginInvoke so I can employ it in other places where I might get stuck. – user1646737 Oct 14 '12 at 21:09