How does one access a control from a static method

2020-01-27 08:08发布

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 get these message's text appended into a multi-line textbox on the MainForm.

I can send the message to a method in the MainForm by making the method static, but then the static method cannot access the MainForm's controls.

TheIncomingDataClass.cs

namespace TheApplicationName
{
     class TheIncomingDataClass
     {

     public void IncomingMessage(IncomingMessageType message)
     {
          TheApplicationName.MainForm.ReceiveMSG(message);
     }

MainForm.cs

public static void ReceiveMSG(string message)
{
     txtDisplayMessages.AppendText(message); //This line causes compile error
}

The compile error:

An object reference is required for the nonstatic field, method, or property 'TheApplicationName.MainForm.txtDisplayMessages'

标签: c# .net winforms
10条回答
够拽才男人
2楼-- · 2020-01-27 08:20

Seeing as you are new to C# I will keep this as simple as possible. You should have a Program.cs file that has a single method Main (this would have been generated by Visual Studio). You will need to make it look like the following:

class Program
{
    public static readonly MainForm MainForm;

    static void Main()
    {
        Application.EnableVisualStyles();
        MainForm = new MainForm(); // These two lines
        Application.Run(MainForm); // are the important ones.
    }
}

Now in your incoming message you will have a way to access that form.

 public void IncomingMessage(IncomingMessageType message)
 {
      Program.MainForm.RecieveMSG(message);
 }

That method in the form would then be a instance (not static) method. E.g.

 public void RecieveMSG(IncomingMessageType message) // NB: No static
 {
     txtDisplayMessages.Text = message.Text; // Or whatever.
 }

There are better ways to do it - but as a beginner I think this would be the best approach.

The difference between static and instance (instance is when you don't say static) is huge. To get to an instance method, field or property (which are collectively called members in C#) you need to have the containing instance. So:

 Person p = new Person(); // You now have an instance.
 p.Name = "Fred"; // You are using an instance property.

Static are the opposite, they are the same anywhere in your application (more technically within the same AppDomain - but if you are a beginner you won't need to worry about that for a while). You don't need an instance to get to them (props to codewidgets "Static methods can access only static members"). For example:

 // Where Planet is a class and People is a static property.
 // Somewhat confusingly the Add method is an instance - best left for the student :).
 Planet.People.Add(new Person("Fred")); 

Hopefully that gives you a good indication of what static and instance is and where to use them. The most important thing though is to try and avoid static members as best as you can - they can cause maintenance nightmares.

Microsoft has a whole write-up on the important concepts in regard to this.

查看更多
叼着烟拽天下
3楼-- · 2020-01-27 08:26
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;
    }
}
查看更多
唯我独甜
4楼-- · 2020-01-27 08:29

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.

查看更多
放我归山
5楼-- · 2020-01-27 08:31

raise an event from class which the form can subscribe to.

查看更多
女痞
6楼-- · 2020-01-27 08:33

A static method doesn't have access to members like txtDisplayMessages because it is not a part of that instance. I suggest you do some reading on the concepts of static methods and whatnot, because that is a fairly language agnostic concept. That method would best be served by removing the static modifier, because it doesn't need to be static - it appears that it would need to be called by that particular instance of that object.

查看更多
放我归山
7楼-- · 2020-01-27 08:38

Just remove the static modifier, you don't need it for your purposes. Read about statics here.

查看更多
登录 后发表回答