How does one access a control from a static method?

前端 未结 10 1920
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-30 12:50

I have an application in C# .NET which has a MainForm and a few classes.

One of these classes receives incoming data messages from a network. I need to

相关标签:
10条回答
  • 2020-11-30 13:37

    Ok here goes. Static methods can access only static members. Your ReceiveMSG method is static. txtDisplayMessages is not and hence you cant access it. Why does your method need to be static? Needless to say, if you remove the static keyword that will fix your problem.

    Just make ReceiveMSG part of a class, create an instance of the class and then call the method on the instance.

    I think you should post the kind the solution you are expecting.

    0 讨论(0)
  • 2020-11-30 13:42
    private void FormMain_Load(object sender, EventArgs e)
    {
        TheIncomingDataClass.SetupControl(textBox1);
    }
    
    public class TheIncomingDataClass
    {
        public static TextBox textbox = new TextBox();
        public static void SetupControl(TextBox txt)
        {
            textbox = txt;
        }
        public void IncomingMessage(string message)
        {
            textbox.Text = message;
        }
    }
    
    0 讨论(0)
  • Its possible to pass in a reference to the current form like this:

    public static void ReceiveMSG(string message, MainForm mainform)
    {
         mainform.txtDisplayMessages.AppendText(message); 
    }
    

    Although as suggested an event is probably a better way of doing it.

    0 讨论(0)
  • 2020-11-30 13:48

    To continue the way you've been doing it, your "TheIncomingDataClass" should have a reference to the MainForm object with which it should interface. When you create an instance of this class (presumably from an instance method of MainForm), you will need to pass in a reference to this MainForm object.

    class TheIncomingDataClass{
        MainForm form;
    
        public TheIncomingDataClass(MainForm form){
            this.form = form;
        }
    }
    
    class MainForm : Form{
        MainForm(){
            new TheIncomingDataClass(this);
        }
    }
    

    However, as suggested by Bugs, you probably would be better off making this an event on TheIncomingDataClass and subscribing to it from MainForm.

    class IncomingMessageEventArgs : EventArgs{
        IncomingMessageType message;
    
        public IncomingMessageType Message{get{return message;}}
    
        public IncomingMessageEventArgs(IncomingMessageType message){
            this.message = message;
        }
    }
    
    class TheIncomingDataClass{
        public event EventHandler<IncomingMessageEventArgs> MessageReceived;
    
        protected virtual void OnMessageReceived(IncomingMessageEventArgs e){
            if(MessageReceived != null)
                MessageReceived(this, e);
        }
    
        public void IncomingMessage(IncomingMessageType message){
            OnMessageReceived(new IncomingMessageEventArgs(message));
        }
    }
    
    class MainForm : Form{
        MainForm(){
            new TheIncomingDataClass().MessageReceived +=
                (s, e)=>txtDisplayMessages.AppendText(e.Message.ToString());
        }
    }
    
    0 讨论(0)
提交回复
热议问题