简体   繁体   English

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

[英]c# referencing a label from another method

I want if mouse leave the visible status change to false,but I get this error message: Error CS7036 There is no argument given that corresponds to the required formal parameter 'e' of 'Form1.Repair_MouseLeave(object, EventArgs, Label)' How shoudl I fix it?我想如果鼠标将可见状态更改为 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;

    }

First of all, we need to set our event handlers for the MouseHover and MouseLeave methods for the Repair control.首先,我们需要为Repair控件的 MouseHover 和 MouseLeave 方法设置我们的事件处理程序。 I am assuming you know how to do this.我假设你知道如何做到这一点。 Still, binding to the events of Repair control can be achieved using the Properties window of your Form in design mode.尽管如此,可以在设计模式下使用窗体的属性窗口实现对修复控件事件的绑定。 Set event handlers to both MouseHover and MouseLeave .将事件处理程序设置为MouseHoverMouseLeave

As far as I can see, you are trying to display a label with some text when the mouse is hovering over this Repair control and want to hide it when the mouse leaves it.据我所知,当鼠标悬停在此修复控件上时,您试图显示带有一些文本的标签,并希望在鼠标离开时隐藏它。 But you are handling it incorrectly.但是你处理它不正确。 First of all, calling MouseLeave from inside MouseHover would immediately hide your new label and it would not be displayed at all.首先,从MouseHover内部调用MouseLeave会立即隐藏您的新标签,它根本不会显示。

And your method signature for Repair_MouseLeave is also incorrect.您的Repair_MouseLeave方法签名也不正确。 A standard event handler takes two parameters: (object sender, EventArgs e)标准事件处理程序采用两个参数: (object sender, EventArgs e)

Implement your event handlers like the following, having the new label repairText as an instance member of your class:实现您的事件处理程序,如下所示,将新标签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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM