C# c语言中鼠标悬停事件未在面板上触发#
我正在尝试使用c编写winform应用程序中面板鼠标悬停事件的代码。这是我的密码C# c语言中鼠标悬停事件未在面板上触发#,c#,winforms,panel,mousehover,C#,Winforms,Panel,Mousehover,我正在尝试使用c编写winform应用程序中面板鼠标悬停事件的代码。这是我的密码 private void viewscreen_MouseHover(object sender, EventArgs e) { statuspnl.Enabled = true; statuspnl.Visible = true; } 但问题是,当我将鼠标移到viewscreen面板上时,事件没有触发 // viewscreen //
private void viewscreen_MouseHover(object sender, EventArgs e)
{
statuspnl.Enabled = true;
statuspnl.Visible = true;
}
但问题是,当我将鼠标移到viewscreen面板上时,事件没有触发
// 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);
你有这个密码吗
this.viewscreen.MouseHover += new System.EventHandler(this.viewscreen_MouseHover);
它应该自动添加,但请确保您不更改此。。。
另一种方法-删除鼠标悬停事件并再次添加它
希望这会有所帮助。您的查看屏幕是表单还是面板?
您是否启用了面板/窗体?请创建一个名为“panelvisible”的窗口应用程序。添加一个表格作为“表格1” 并将这些代码添加到相应的文件中 设计者代码
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;
}
}
如何附加此事件处理程序?我的意思是,您的代码如何知道viewscreen\u MouseHover处理viewscreen控件的MouseHover事件?您的代码如何知道此事件处理程序用于该控件的特定事件?自动生成的设计器代码?是否在表单加载时显式设置事件处理程序?断点/调试的结果是什么?看一看你的帖子,你会发现没有多少人会出于某种原因帮助你。@DeeMac你的问题的答案已经在发布的代码中了。请看问题的最后一行代码。他将处理程序显式附加到保存viewscreen控件的同一类中的viewscreen_MouseHover方法。注意控件和方法前面的“this”?谢谢。。我不能投你的票。。bcoz那家伙吃了我的Rep当我在onpaint方法中将鼠标悬停在我绘制的正方形上时,这不起作用:(
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;
}
}
}