• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 436
  • Last Modified:

Tooltip tricks

Hi all.

I have a panel on my form. On the panel, I draw many object like: circle, rectangle, elip ...
I have all information about the co-ordinate of these figures.

Now when I move the mouse over these figure, I want it will show a tooltip, depend on with firgure the mouse over.

Example, when mouse over a circle, tooltip text should be "Circle",
              When mouse over a rectangle, tooltip text should be "rectangle"

Have you got any idea or any link about this?
0
melodiesoflife
Asked:
melodiesoflife
  • 3
1 Solution
 
Thandava VallepalliCommented:
You need to store the circle, rectangle co-ordinates to get this functionality.

in form mouse event hadler,  you have to compare the  current mouse coordinates with the  circle or rectanle coordinates. if mouse is in objects, you can re-set the tooltip test....... here is the sample code..... how to reset the tooltip test according to current mouse coordinats..

============================
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TOOLTIP
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.ToolTip toolTip1;
            private System.ComponentModel.IContainer components;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (components != null)
                        {
                              components.Dispose();
                        }
                  }
                  base.Dispose( disposing );
            }

            #region Windows Form Designer generated code
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {
                  this.components = new System.ComponentModel.Container();
                  this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(292, 273);
                  this.Name = "Form1";
                  this.Text = "Form1";
                  this.toolTip1.SetToolTip(this, "Hello Form");
                  this.Load += new System.EventHandler(this.Form1_Load);
                  this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_mouse);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void Form1_Load(object sender, System.EventArgs e)
            {
            
            }

            private void Form1_mouse(object sender, System.Windows.Forms.MouseEventArgs e)
            {
                  //MessageBox.Show(this, e.X.ToString(), "Show");
                  //MessageBox.Show(this, e.Y.ToString(), "Hi");
                  if( e.X > 200 && e.Y > 200 )
                        this.toolTip1.SetToolTip(this, "Hello You win this game" );
            }

      }
}

==============================

i am changing the tooltip text when mouse x, y co-ordinates are > 200.

itsvtk
0
 
Thandava VallepalliCommented:
Here is sample code to draw circle and change the tooltip when the mouse is on that circle....

==================
using System;
using System.Windows.Forms;
using System.Threading;
using System.Drawing;

class Demo : Form
{      
      private System.Windows.Forms.ToolTip toolTip1;
      private System.ComponentModel.IContainer components;
      public Demo()
      {
            this.components = new System.ComponentModel.Container();      
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            Text = "Testing....";
            BackColor = Color.BlanchedAlmond;
            FormBorderStyle = FormBorderStyle.FixedSingle;
            Cursor = Cursors.Hand;
            StartPosition = FormStartPosition.CenterScreen;
            this.MouseMove += new MouseEventHandler(this.mycircle);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.WindowState = FormWindowState.Maximized;
      }
      
      public static void Main()
      {
          Application.Run(new Demo());
      }
      
      protected override void OnPaint(PaintEventArgs pea)
      {
            System.Drawing.Graphics graphics = this.CreateGraphics();
            System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle(100, 100, 200, 200);
            graphics.DrawEllipse(System.Drawing.Pens.Black, rectangle);
            graphics.DrawRectangle(System.Drawing.Pens.Red, rectangle);            
      }

      public void mycircle(object s, MouseEventArgs e)
      {
            if( e.X >= 100 && e.X <= 300 && e.Y >= 100 && e.Y <= 300 )
            {
                  toolTip1.SetToolTip( this, "MyCircle" );
            }else
            {
                  toolTip1.SetToolTip( this, "Hello This is Windows Application" );
            }
      }
}

======================

when tool tip is no circle   MyCircle will be displayed   in remainging places  Hello This is Windows Application  will be displayed
0
 
melodiesoflifeAuthor Commented:
Thanks  itsvtk

But I hope a real tooltip. I don't want my tooltip display immediately, I want my tooltip display when my mouse over on the figure for a second (hover event).
Any idea
0
 
Thandava VallepalliCommented:
then try this

public void mycircle(object s, MouseEventArgs e)
     {
          if( e.X >= 100 && e.X <= 300 && e.Y >= 100 && e.Y <= 300 )
          {
               toolTip1.SetToolTip( this, "MyCircle" );
          }else
          {
               toolTip1.SetToolTip( this, "" );
          }
     }
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now