Need quick C# help

InverseOfNeo

Diamond Member
Nov 17, 2000
3,719
0
0
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?
 

clamum

Lifer
Feb 13, 2003
26,256
406
126
Is this a WinForm application? Maybe look in the "Designer Generated Code" and make the controls "public", or write get/set methods for them.
 

InverseOfNeo

Diamond Member
Nov 17, 2000
3,719
0
0
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.
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
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.
 

KB

Diamond Member
Nov 8, 1999
5,406
389
126
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();

}
}