Click here to Skip to main content
15,888,113 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So in program.cs I wrote the following code:
Form1.textBox1.Text = RSAObj.ToXmlString(true);


The problem is that I get the following errors: 'Form1.Form1.textBox1' is inaccessible due to its protection level

and

An object reference is required for the non-static field, method, or property 'Form1.Form1.textBox1'

Please help?

What I have tried:

Research, I don't know how to fix this problem man
Posted
Updated 26-Jan-19 8:06am
v5

1 solution

You have to use an instance of Form1 to access its textBox1 control.
The way you wrote it, is like you are expecting the textbox to be a static element of the Form1 class, which it obviously is not.
One way to do that is to keep a reference to the instance at the time you create it (most probably, in the Main method of the Program class):
C#
static void Main(string[] args) {
  // ...
  Form1 form = new Form1();
  form.textBox1.Text = "Whatever...";
  Application.Run(form);
}

PS: I just saw you have reposted your question instead of editing already existing one. Please do not repost the same question several times; it will not help you getting better answers.
 
Share this answer
 
v2
Comments
Member 14130699 26-Jan-19 14:37pm    
Hi man, I still get the same errors. This is my code:

static void Main(string[] args) {
Application.Run(new Form1());
Form1 form = new Form1();
Form1.textBox1.Text = "Whatever...";
}
Member 14130699 26-Jan-19 14:41pm    
'Form1.Form1.textBox1' is inaccessible due to its protection level

and

An object reference is required for the non-static field, method, or property 'Form1.Form1.textBox1'
Dave Kreskowiak 26-Jan-19 17:23pm    
You've got huge gabs in your understanding of how object oriented code works.

You're Program code, Main method created TWO instances of Form1, one of which it showed, the other you tried to modify the textbox on and didn't show at all.

You're code needs to make ONE instance of Form1, modify it, then show it.

static void Main(string[] args)
{
    // Create an instance of Form1.
    Form1 form = new Form1();

    // Modify the textbox value on it.
    form.SetTextBox1Value("Whatever...");

    // Show the application form.
    Application.Run(form);
}


To make this work, you have to expose a method in your Form1 code to set the value of the textbox.
    public void SetTextBox1Value(string text)
    {
        TextBox1.Text = text;
    }
phil.o 27-Jan-19 5:14am    
My virtual 5 :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900