locked
Mouse Hover Controls RRS feed

  • Question

  • I have this control with numerous buttons, say about 20.   Instead of showing ToolTip for each control (it does look cumbersome to me), I would show the tips text on the status strip.

    Now the question is, is there a simple way to get the instance of mouse hovering events instead of listing each button's mouse hover events?

    I hope I am clear!

    P.S.
    I happened to find this in my Visual Studio 2010.   I think this one is the answer.   Can anyone explain to me on how to use this...

                      this.Controls_MouseHover(object sender, EventArgs e)


    Thanks in Advance!

    Ron Boucher
    Monday, July 26, 2010 1:26 PM

Answers

  • Hi,

    The following code I have been tried which works fine:

      public partial class Form1 : Form
      {
        ToolStripStatusLabel tssl = new ToolStripStatusLabel();
    
        public Form1()
        {
          InitializeComponent();
    
          statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { tssl });
          CreateMouseHoverEvents(this);
          
        }
        private void CreateMouseHoverEvents(Control currentControl)
        {
          foreach (Control ctrl in currentControl.Controls)
          {
            if (ctrl is GroupBox)
              CreateMouseHoverEvents(ctrl);
            else
            {
              ctrl.MouseHover += new EventHandler(Controls_MouseHover);
              ctrl.MouseLeave += new EventHandler(Controls_MouseLeave);
            }
          }
        }
    
    
    
        void Controls_MouseLeave(object sender, EventArgs e)
        {
          tssl.Text = "";
        }
    
        void Controls_MouseHover(object sender, EventArgs e)
        {
          Control ctrl = (Control)sender;
          tssl.Text = ctrl.Name;
        }
     
    
    
      }

    I am not sure you mean the control name is not displayed on ToolStripStatusLabel.

    Thanks

    Binze

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Friday, July 30, 2010 6:59 AM
    Wednesday, July 28, 2010 10:09 AM

All replies


  • For each of your buttons, you would have a line as such:
     
                button1.MouseHover += new EventHandler(MyMouseHoverHandler);
    then declare a method:
     
            MyMouseHoverHandler(object sender, EventArgs e)
            {
                // show your tooltip, or whatever...
            }

    --
    Mike
    Monday, July 26, 2010 1:42 PM
  • Attach all the Hover events to the same hover function.

    1. First, create one hover event.

    2. In that method include:

    Button btn = sender as
     Button;
    That way you can refer to the specific button in every hover event.

    3. Then, for each button, click on its hover event and from the drop-down list, pick the first method that you created in stage 1.


    Noam B.
    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Monday, July 26, 2010 2:34 PM
    Monday, July 26, 2010 1:42 PM
  • This is what I've figured out... But this didn't show the name of control when mouse hovering the numericUpDown Control.

    ToolStripStatusLabel tssl = new ToolStripStatusLabel();
    
    		int status = 0;
    		int xCurrentLoc = 0;
    		int yCurrentLoc = 0;
    		int zCurrentLoc = 0;
    
    		public MotorApp()
    		{
    			InitializeComponent();
    
    			motor.OpenPort();
    
    			rbCircle.CheckedChanged += new EventHandler(SelectAntennaShape);
    			rbSquare.CheckedChanged += new EventHandler(SelectAntennaShape);
    
    			statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { tssl });
    
    			CreateMouseHoverEvents(this);
    		}
    
    		private void CreateMouseHoverEvents(Control currentControl)
    		{
    			foreach (Control ctrl in currentControl.Controls)
    			{
    				if (ctrl is GroupBox)
    					CreateMouseHoverEvents(ctrl);
    				else
    				{
    					ctrl.MouseHover += new EventHandler(Controls_MouseHover);
    					ctrl.MouseLeave += new EventHandler(Controls_MouseLeave);
    				}
    			}
    		}
    
    		
    		
    		void Controls_MouseLeave(object sender, EventArgs e)
    		{
    			tssl.Text = "";
    		}
    
    		void Controls_MouseHover(object sender, EventArgs e)
    		{
    			Control ctrl = (Control) sender;
    			tssl.Text = ctrl.Name;
    		}
    

     


    Ron Boucher
    Monday, July 26, 2010 2:14 PM
  • Hi,

    The following code I have been tried which works fine:

      public partial class Form1 : Form
      {
        ToolStripStatusLabel tssl = new ToolStripStatusLabel();
    
        public Form1()
        {
          InitializeComponent();
    
          statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { tssl });
          CreateMouseHoverEvents(this);
          
        }
        private void CreateMouseHoverEvents(Control currentControl)
        {
          foreach (Control ctrl in currentControl.Controls)
          {
            if (ctrl is GroupBox)
              CreateMouseHoverEvents(ctrl);
            else
            {
              ctrl.MouseHover += new EventHandler(Controls_MouseHover);
              ctrl.MouseLeave += new EventHandler(Controls_MouseLeave);
            }
          }
        }
    
    
    
        void Controls_MouseLeave(object sender, EventArgs e)
        {
          tssl.Text = "";
        }
    
        void Controls_MouseHover(object sender, EventArgs e)
        {
          Control ctrl = (Control)sender;
          tssl.Text = ctrl.Name;
        }
     
    
    
      }

    I am not sure you mean the control name is not displayed on ToolStripStatusLabel.

    Thanks

    Binze

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Bin-ze Zhao Friday, July 30, 2010 6:59 AM
    Wednesday, July 28, 2010 10:09 AM