1

How can i update label from another class, i have seen many answer, but not function for my example. here is my code:

class Form1: look like this:

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)
    {

    }
}
}

Class MediaEvent look like this:

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();
    }

}

}

This is the full code more clearly. i need your help

manout
  • 41
  • 2
  • 10
  • 1
    Just a hint: You should not change GUI controls from another thread. Consider using main thread invocation instead (see for example http://stackoverflow.com/questions/142003/cross-thread-operation-not-valid-control-accessed-from-a-thread-other-than-the) – DerApe Jul 28 '14 at 13:04
  • Asuming the form is correctly initialized and you are observing the form referenced by the `Instance` property, your code should work. Can you show us the whole `Form1` code? – DerApe Jul 28 '14 at 13:10
  • Thanks for replying. i will show a code and that check the replay links this the Form code: `public Form1() { InitializeComponent(); Instance = this; MediaEvent updater = new MediaEvent(); }` – manout Jul 28 '14 at 13:16
  • @derape you can please help me with solving the problem I'm trying to understand the @ Jeff Hubbard you've linked me, but not sure how to implement it, I'm new to this language. NET. – manout Jul 28 '14 at 13:33
  • i believe that there's a way to do this! maybe i miss something i didn't comment? – manout Jul 28 '14 at 13:51
  • I really want to help you but we just dont have enough information about what you are trying to do and how you want to achieve it as well as how your code looks. Please describe what your piece of code should do and update your question with the involved code (not in the comments) – DerApe Jul 28 '14 at 14:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/58175/discussion-between-derape-and-manout). – DerApe Jul 28 '14 at 14:37

2 Answers2

2

you need to change your code to something like the following

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");
    }
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
0

You can reference the Form by setting up a method on the Form_Load.

private void Form1_Load(object sender, EventArgs e)
{
    MyClass.SetUIComponents(this);
}

After that you create a global variable in your class and initialize your variable with the form.

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

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

After you have initialized Form1 global variable, you can reference the Form. Remember that objects such as lables or buttons must have their modifiers (Found in objects Properties Form1[Design]) changed to public so you can change the text, name, etc...