سؤال

As you can see, the class names I used are default names of classes generated by Visual C#. How can I go about changing values in a TextBox named "textBox2" (this TextBox is placed in the Form1 design already) from the "Program" class? I have tried a lot of things and every thing I tried results in this error (or similar to): An object reference is required for the non-static field, method, or property 'WindowsFormsApplication1.Form1.textBox2'

Please, if you can, try to keep your answers simple, thank-you.

هل كانت مفيدة؟

المحلول

First, you generally shouldn't access controls such as textboxes from your Program class. Instead you should do this in the Form1.cs file. Form1 is a class, and it has a protected field for textBox2 so it is inaccessible outside the class. If you want to change the value from Program.cs, you should add a public function to Form1 that sets the value of textBos2.Text.

Secondly, you appear to be just typing class names instead of the name of the instance. The difference is Textbox is a class, textBox1 and textBox2 are instances. Textbox.Text is invalid because you need to specify WHICH textbox you are trying to get or set the text for. It's the same with Form1.textBox2. Form1 is a class and there can be many of them. You must specify the name of the instance of the form to access its public members.

UPDATE:

I'm just going to give you a brief explanation of the difference between a class and an instance of a class, static fields and non-static fields. Please forgive any wordiness.

When you create a new windows forms application Visual Studio will create a Form1 type for you. Form1 is a class. Program then uses Form1 to create a form instance and show it. The code would look something like:

Form1 form = new Form1;

In this case, form is the instance. You can create multiple instances of Form1. Each instance will have the textBox2 you created, which is an instance of the Textbox class. Just like you have to do textBox2.Text to get the text of the second Textbox you created on the form, you must specify form.textBox2 (or your public method that sets the textBox2.Text value). Form1, even though it has a number after it is a class, and form is the instance. They have the same relationship as Textbox and textBox2.

Non-static members are accessible to an instance. Static members are accessible to the class. A static member can not access a non-static member unless it is through an instance.

نصائح أخرى

You'll need to have an instance of your Form1 to do this.

Form1 frm = new Form1();

Then you'll have to build a public method to access to your textbox, cause it's a private member.

Form1.cs:

public void UpdateText(string newValue)
{
    this.textbox2.Text = newValue;
}

Finally:

frm.UpdateText("new text");

You have to create a new instance of the Form1 class.
You can't "reach" a non static var without new()

var form1 = new Form1();
form1.textBox2.Text= "aaa";

Make textBox2 public or internal. To do so adjust the Modifier property of it from the designer (Properties). Then do this in Main from "Program" class:

    Form1 f = new Form1();
    f.textBox2.Text = "sdfsdf";
    Application.Run(f);

This is absolutely a bad design anyways.. Tell us why you would want this, we would be helpful in dealing with the real problem.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top