-5

I have a button on my form, but I am unable to access it from my code. My code (from Program.cs):

Form1.button1.Text = "Button text"

I cannot access it due to it's protection level. What shall I do to change this field protection level or something?

misticos
  • 718
  • 5
  • 22

2 Answers2

1

On the properties window you can change the privacy of the components. You need to change the button privacy to public.

--EDIT
What fildor wrote is a better aproach

gabemilani
  • 54
  • 1
  • 4
0

Changing the modifier of an object is a bad idea. Better would be to create a public property on Form1, for example Button1Text and call that in stead of accesing a private object.

public string Button1Text
{  
   get { return Button1.Text; }
   set {Button1.Text = value; }
}

then call it like this

form1.Button1Text = "I am a button"

Make sure that form1 is the created instance for your form, not the class name !

GuidoG
  • 11,359
  • 6
  • 44
  • 79