c#从另一个方法引用标签
我希望鼠标将可见状态更改为false,但我收到以下错误消息: 错误CS7036未给出与“Form1.Repair_MouseLeave(object,EventArgs,Label)”的必需形式参数“e”相对应的参数 我该怎么修c#从另一个方法引用标签,c#,methods,reference,windows-forms-designer,C#,Methods,Reference,Windows Forms Designer,我希望鼠标将可见状态更改为false,但我收到以下错误消息: 错误CS7036未给出与“Form1.Repair_MouseLeave(object,EventArgs,Label)”的必需形式参数“e”相对应的参数 我该怎么修 private void Repair_MouseHover(object sender, EventArgs e) { Label RepairText = new Label(); RepairText = new L
private void Repair_MouseHover(object sender, EventArgs e)
{
Label RepairText = new Label();
RepairText = new Label();
RepairText.Location = new Point(161, 12);
RepairText.Text = "This what the program will do";
this.Controls.Add(RepairText);
RepairText.AutoSize = true;
Repair_MouseLeave(RepairText);
}
private void Repair_MouseLeave(object sender, EventArgs e,Label repairtext)
{
repairtext.Visible = false;
}
首先,我们需要为
Repair
控件的MouseHover和MouseLeave方法设置事件处理程序。我想你知道怎么做。尽管如此,
在设计模式下,可以使用窗体的“属性”窗口实现对修复事件控件的绑定。将事件处理程序设置为MouseHover
和MouseLeave
据我所知,当鼠标悬停在修复控件上时,您试图显示带有一些文本的标签,并希望在鼠标离开时将其隐藏。但你处理得不对。首先,从MouseHover
内部调用MouseLeave
会立即隐藏您的新标签,并且根本不会显示它
而且您的Repair\u MouseLeave
方法签名也不正确。标准事件处理程序接受两个参数:(对象发送方,EventArgs e)
实现如下事件处理程序,将新标签repairText
作为类的实例成员:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Label repairText;
private void Repair_MouseHover(object sender, EventArgs e)
{
if(repairText == null)
{
repairText = new Label();
repairText.Location = new Point(161, 12);
repairText.Text = "This what the program will do";
repairText.AutoSize = true;
this.Controls.Add(repairText);
}
repairText.Visible = true;
}
private void Repair_MouseLeave(object sender, EventArgs e)
{
if(repairText != null)
{
repairText.Visible = false;
}
}
}
您应该绑定到事件。首先,从
Repair\u MouseLeave
方法签名中删除Label repairtext
参数。使RepairText成为表单的实例成员,或者,在将标签添加到控件集合之前为其指定一个名称,然后使用控件在Repair\u MouseLeave
中查找标签。find()
每次鼠标离开控件时,是否确实可以创建新标签并将其添加到表单中?(否)您不应尝试自己调用事件处理程序<代码>修复鼠标删除(修复文本)代码>是错误的。即使成功调用此方法,您的repairText标签在几分之一纳秒内都不会在事件中可见。