I am trying to make a class with a constructor that initializes the form controls. The constructor should take two parameters. But I cannot access the controls within the class I made. How do I do this?
Yes it is a WinForm app; sorry I didnt clarify that. I really dont understand the get/set stuff though. And by make the controls public, I assume change the "Modifiers" property to public. I did that but I still cant access them.
This sounds like a scope issue to me. Assuming that you created an object of the form you want to initialize, you should be able to access it from the constructor.
For that matter, can you access the controls you want from elsewhere in your class other than the constructor?
And remember, if you are using threads, you CANNOT access a forms controls from a thread directly.
Where are you trying to access the Controls ?
It should be easy if these are designer generated controls, unless you have set the controls as private.
//if these are designer generated
//constructor
public void Form1(){
this.NameTextBox.Text = "new text";
}
//outside the form
Form1 f = new Form1();
f.NameText.Text = "new text";
//if you aren't using designer generated controls and are creating the controls in the constructor then you need to store the variables for the controls in the class somewhere outside the contrsuctor
public class Form1(){
public TextBox NameTextBox;
public void Form1(){
this.NameTextBox = new TextBox();
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.