C# C从另一个类更新标签

C# C从另一个类更新标签,c#,label,C#,Label,如何从另一个类更新标签,我已经看到了很多答案,但我的示例中没有函数。 这是我的密码: 类Form1:如下所示: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using Sy

如何从另一个类更新标签,我已经看到了很多答案,但我的示例中没有函数。 这是我的密码:

类Form1:如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace login
{
public partial class Form1 : Form
{

    public static Form1 Instance { get; private set; }


    public Form1()
    {
        InitializeComponent();
        Instance = this;
        MediaEvent device = new MediaEvent();

    }




    private void button2_Click(object sender, EventArgs e)
    {            
        MediaEvent msg = new MediaEvent();
        string u = textBox1.Text;
        string p = textBox2.Text;

        msg.checkLogin(u, p);
        label2.Text = msg.getMessage();

    }

    public void TextStatus(string aString)
    {
        // Place messages in Main Display list box window

        this.lstatus.Text = aString;
    }
    public void TextToBox(string aString)
    {
        // Place messages in Main Display list box window

        this.listBox1.Items.Insert(0, aString);
    }

    public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace login
{
class MediaEvent: IDisposable
{
    public string MyMsg { get; set; }
    public string State { get; set; }
    public string getMessage()
        {
            return MyMsg;
        }
    public void checkLogin(string u, string p)
    {
        if((u != "user") && (p != "password"))
        {
            MyMsg +="Id or password no correct, please try again.";
        }
    }
    Form1 MainForm;



    private ManagementEventWatcher watcherAttach;
    private ManagementEventWatcher watcherRemove;

    public MediaEvent()
    {
        if (watcherAttach == null)
        {
            watcherAttach = new ManagementEventWatcher();          
            watcherAttach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
            watcherAttach.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            //Form1.Instance.TextStatus("On");
            watcherAttach.Start();
        }

        if (watcherRemove == null)
        {
            watcherRemove = new ManagementEventWatcher();            
            watcherRemove.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
            watcherRemove.EventArrived += new EventArrivedEventHandler(watcher_EventRemoved);
            //Form1.Instance.TextStatus("Off");
            watcherRemove.Start();
        }
    }

    /// <summary>
    /// Used to dispose of the USB device watchers when the USBControl class is disposed of.
    /// </summary>
    public void Dispose()
    {
        if (watcherAttach != null)
        {
            watcherAttach.Stop();
            watcherAttach.Dispose();                
            watcherAttach = null;
        }

        if(watcherRemove != null)
        {
            watcherRemove.Stop();
            watcherRemove.Dispose();
            watcherRemove = null;                
        }

    }



    void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Form1.Instance.TextStatus("On");
        //MessageBox.Show("On");
    }

    void watcher_EventRemoved(object sender, EventArrivedEventArgs e)
    {
        Form1.Instance.TextStatus("Off");
        //MessageBox.Show("off");
    }




    ~MediaEvent()
    {
        this.Dispose();
    }

}

}
类MediaEvent如下所示:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace login
{
public partial class Form1 : Form
{

    public static Form1 Instance { get; private set; }


    public Form1()
    {
        InitializeComponent();
        Instance = this;
        MediaEvent device = new MediaEvent();

    }




    private void button2_Click(object sender, EventArgs e)
    {            
        MediaEvent msg = new MediaEvent();
        string u = textBox1.Text;
        string p = textBox2.Text;

        msg.checkLogin(u, p);
        label2.Text = msg.getMessage();

    }

    public void TextStatus(string aString)
    {
        // Place messages in Main Display list box window

        this.lstatus.Text = aString;
    }
    public void TextToBox(string aString)
    {
        // Place messages in Main Display list box window

        this.listBox1.Items.Insert(0, aString);
    }

    public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Windows.Forms;

namespace login
{
class MediaEvent: IDisposable
{
    public string MyMsg { get; set; }
    public string State { get; set; }
    public string getMessage()
        {
            return MyMsg;
        }
    public void checkLogin(string u, string p)
    {
        if((u != "user") && (p != "password"))
        {
            MyMsg +="Id or password no correct, please try again.";
        }
    }
    Form1 MainForm;



    private ManagementEventWatcher watcherAttach;
    private ManagementEventWatcher watcherRemove;

    public MediaEvent()
    {
        if (watcherAttach == null)
        {
            watcherAttach = new ManagementEventWatcher();          
            watcherAttach.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2");
            watcherAttach.EventArrived += new EventArrivedEventHandler(watcher_EventArrived);
            //Form1.Instance.TextStatus("On");
            watcherAttach.Start();
        }

        if (watcherRemove == null)
        {
            watcherRemove = new ManagementEventWatcher();            
            watcherRemove.Query = new WqlEventQuery("SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 3");
            watcherRemove.EventArrived += new EventArrivedEventHandler(watcher_EventRemoved);
            //Form1.Instance.TextStatus("Off");
            watcherRemove.Start();
        }
    }

    /// <summary>
    /// Used to dispose of the USB device watchers when the USBControl class is disposed of.
    /// </summary>
    public void Dispose()
    {
        if (watcherAttach != null)
        {
            watcherAttach.Stop();
            watcherAttach.Dispose();                
            watcherAttach = null;
        }

        if(watcherRemove != null)
        {
            watcherRemove.Stop();
            watcherRemove.Dispose();
            watcherRemove = null;                
        }

    }



    void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Form1.Instance.TextStatus("On");
        //MessageBox.Show("On");
    }

    void watcher_EventRemoved(object sender, EventArrivedEventArgs e)
    {
        Form1.Instance.TextStatus("Off");
        //MessageBox.Show("off");
    }




    ~MediaEvent()
    {
        this.Dispose();
    }

}

}

这是更清楚的完整代码。我需要您的帮助

您需要将代码更改为以下内容

public partial class Form1 : Form
    {
        private static Form1 _instance; 
        public Form1()
        {

            InitializeComponent();
            _instance = this; 
        }


        public string TextStatus
        {
            get { return StatusLabel.Text;  }
            set { StatusLabel.Text = value;  }

        }

        public static Form1 Instance { get { return  _instance;  }}



    }

void watcher_EventArrived(object sender, EventArrivedEventArgs e)
    {
        Form1.Instance.TextStatus ="On"; // nothing is changed in label
        //MessageBox.Show("On");
    }

您可以通过在表单加载上设置方法来引用表单

private void Form1_Load(object sender, EventArgs e)
{
    MyClass.SetUIComponents(this);
}
然后,在类中创建一个全局变量,并使用表单初始化变量

public class MyClass{
  //Constructor
  public MyClass(){...}

  public void SetUIComponents(Form1 Form1)
  {
      // Get MainForm to reference public UI objects 
      this.Form = Form;
  }
}

初始化Form1全局变量后,可以引用该表单。请记住,标签或按钮等对象的修改器必须在对象属性Form1[Design]中更改为public,以便您可以更改文本、名称等。

只是一个提示:您不应该从其他线程更改GUI控件。考虑使用主线程调用,而不是看到,例如,正确地初始化表单,并且观察实例属性引用的表单,代码应该工作。你能给我们看完整的Form1代码吗?谢谢回复。我将显示一个代码并检查重播链接此表单代码:public Form1{InitializeComponent;Instance=this;MediaEvent updater=new MediaEvent;}@derape您可以帮助我解决这个问题我正在试图理解您链接我的@Jeff Hubbard,但不知道如何实现它,我对这种语言还不熟悉。我相信有办法做到这一点!也许我错过了一些我没有评论的东西?在哪一行,因为我没有尝试itI delete private set{}在实例属性中尝试立即尝试这是错误消息error 1不能用作“login.Form1.TextStatus”成员不可调用的方法。c:\users\user\documents\visualstudio 2013\Projects\login\login\login MediaEvent.cs 124 28在modficiation之后您是否尝试过它?它是一个属性,而不是一个方法。需要使用BeginInvoke。