c# 从另一个方法引用标签

c# referencing a label from another method

我想如果鼠标离开可见状态更改为 false,但我收到此错误消息: 错误 CS7036 没有给定的参数对应于 'Form1.Repair_MouseLeave(object, EventArgs, Label)' 的所需形式参数 'e' 我该如何解决?

    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 方法设置事件处理程序。我假设你知道如何做到这一点。仍然, 在设计模式下使用窗体的属性 window 可以实现绑定到 Repair 控件的事件。将事件处理程序设置为 MouseHoverMouseLeave.

据我所知,您试图在鼠标悬停在此修复控件上时显示带有一些文本的标签,并希望在鼠标离开时隐藏它。但是您处理不当。首先,从 MouseHover 内部调用 MouseLeave 会立即隐藏您的新标签,并且根本不会显示。

并且您 Repair_MouseLeave 的方法签名也不正确。标准事件处理程序有两个参数:(object sender, EventArgs e)

像下面这样实现你的事件处理程序,将新标签 repairText 作为你的 class:

的实例成员
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;
        }
    }
}