Add tooltip to a rectangle

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jbro
    New Member
    • May 2010
    • 2

    #1

    Add tooltip to a rectangle

    hello,

    I'm trying to figure out how to add a ToolTip to a RectangleF, so when the user moves the mouse over the rectangle a tooltip with information regarding that rectangle will pop up. I hope someone out there knows how to do this. For example this is what I have:

    Code:
    private void Form1_Paint (object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        RectangleF r = new RectangleF(20.5, 20.3, 100.45, 200.68);
        e.FillRectangle(Brushes.Red, r);
    
        ToolTip myTip = new ToolTip();
        myTip.SetToolTip(r, "This is my Red RectangleF");
    }
    However, this gives an error because RectangleF is not a user control. So, is the best solution to make another class like Rectangle but is a user control, and if so how? Or, is there a component that I could use (like a button)? However, it is important that the control be able to use floats for the location, width, and height, which eliminates the Button control.

    Not sure how to do this using floats to designate the SizeF and Location? Hope someone can help.

    Thank you,
    Joe
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Try the mouse move event on the form or parent control. If the mouse coordinates intersect the RectangleF object, you can display the tooltip at the cursor on the parent control.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      Another way would be to use a Control (or some class that inherits from Control...inclu ding any of your own classes)
      And add it to the tooltip's list with a tooltip. Allowing the tooltip component to do it's thing naturally.

      Comment

      • jbro
        New Member
        • May 2010
        • 2

        #4
        Thanks Gary,
        I'm working through this, but am getting stuck here trying to get the tooltip to show up at the mouse location. I'm using the ToolTip Show() method, which uses the string and a IWin32Window (this is where the problem comes in).

        private void Form1_MouseMove (object sender, MouseEventArgs e)
        {
        if (r.Contains(e.X , e.Y))
        {
        ToolTip t1 = new ToolTip();
        t1.Show("My Red Rectangle", this);
        }
        }

        Now, I have to have that second parameter in the Show() method, and it has to be an IWin32Window. I thought I could just pass the form in, but that doesn't work. Any ideas? Thank you.

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          That code should work, it's just that the tool tip won't go away when you leave the rectangle. To that end, you'd need an else condition there to hide the tooltip when it's not intersecting. You could also have a flag to see when you've shown the tooltip or not so you don't keep reshowing it (unless you want it to move with the mouse). I also wouldn't create a new ToolTip object every time. Just create one on the form and use that.

          Code:
                          if (m_rect.Contains(e.X, e.Y))
                          {
                              m_tool3.Show("The rectangle!", this, e.X, e.Y);
                          }
                          else
                          {
                              m_tool3.Hide(this);
                          }
          That said, Platter's suggestion also sparked me to dive in and try, and that works quite nicely too.

          So you've got some options here :D

          Comment

          Working...