Mouse Hover event not firing over a panel in c#

戏子无情 提交于 2019-12-14 03:43:05

问题


i am trying to write a code on Mouse_Hover event of a panel in my winform app using c#. This is my code ..

private void viewscreen_MouseHover(object sender, EventArgs e)
    {
        statuspnl.Enabled = true;
        statuspnl.Visible = true;
    }

but the problem is the event is not firing when i am taking the mouse over the viewscreen panel

 // viewscreen
        // 
        this.viewscreen.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
        this.viewscreen.Controls.Add(this.statuspnl);
        this.viewscreen.Location = new System.Drawing.Point(208, 16);
        this.viewscreen.Name = "viewscreen";
        this.viewscreen.Size = new System.Drawing.Size(370, 290);
        this.viewscreen.TabIndex = 0;
        this.viewscreen.MouseHover += new System.EventHandler(this.viewscreen_MouseHover);

回答1:


Please create one window app with the name as "panelvisible". Add one form as "Form1" and add these code in respective files.

Code for designer

namespace panelvisible
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (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.panel1 = new System.Windows.Forms.Panel();
            this.label1 = new System.Windows.Forms.Label();
            this.panel2 = new System.Windows.Forms.Panel();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.BackColor = System.Drawing.Color.Red;
            this.panel1.Controls.Add(this.panel2);
            this.panel1.Controls.Add(this.label1);
            this.panel1.Location = new System.Drawing.Point(101, 36);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(200, 100);
            this.panel1.TabIndex = 0;
            this.panel1.MouseHover += new System.EventHandler(this.panel1_MouseHover);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(49, 42);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(31, 13);
            this.label1.TabIndex = 0;
            this.label1.Text = "Hello";
            this.label1.Visible = false;
            // 
            // panel2
            // 
            this.panel2.BackColor = System.Drawing.Color.Gray;
            this.panel2.Location = new System.Drawing.Point(33, 70);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(118, 10);
            this.panel2.TabIndex = 1;
            this.panel2.Visible = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(494, 205);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Panel panel2;
    }
}

Code for code behind

using System;
using System.Windows.Forms;

namespace panelvisible
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void panel1_MouseHover(object sender, EventArgs e)
        {
            this.label1.Visible = true;
            this.label1.Enabled = true;
            this.panel2.Visible = true;
            this.panel2.Enabled = true;
        }


    }
}



回答2:


Did you have this code?

this.viewscreen.MouseHover += new System.EventHandler(this.viewscreen_MouseHover);

It should be added automatically but make sure you don't change this... Another way - remove mouse hover event and add it again.

Hope this will help.




回答3:


Is you view screen a form or is it realy a panel? And is you panel / form enabled?



来源:https://stackoverflow.com/questions/12403375/mouse-hover-event-not-firing-over-a-panel-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!