For example that I have main form called frmForm, and text box on it txtText. How can I change Text property of txtText from class clsClass.
I tried with, but it doesn't seems to be working :
class clsClass{
public void changeText(){
frmForm.txtText.Text = "Some text";
}}
It has to be non-value returning method (void);
Please help, thanks :)
Sam HobbsPosted Mar 9, 2010, 6:22 PM
NainilPosted Mar 9, 2010, 1:46 PM
I believe in the class Klasa, the method Tekst is/should be instantiating an instance of class frmForm and not Form1. If you change Form1 to Klasa, it should work fine. In any case, what is the error you are getting?
NevenPosted Mar 9, 2010, 1:35 PM
I wrote the following code in class frmForm:
public string getText() //getter method
{ return txtText.Text;}
public void setText(string addr) //setter method
{
string addr1 = addr;
txtText.Text = addr1;
}
private void button1_Click(object sender, EventArgs e) //Button click event to dispaly txt in txtText
{
Klasa cls = new Klasa();
cls.Tekst("Some Text");
}
And here is the class Klasa from which i want to set txtText.Text
class Klasa
{
public void Tekst(string str){
Form1 form = new Form1();
form.setText(str);
form.getText();
}
}
I don't know what I'm doing wrong... :(
NainilPosted Mar 9, 2010, 10:06 AM
class frmForm
{
public string TextBoxText
{
get { return txtText.Text; }
set { txtText.Text = value ; }
}
}
Now from the class clsClass, you can simply instantiate the frmForm object and use the TextBoxText property to get or set the txtText.Text property.
2. In the class frmForm, change the Modifiers property of the txtText control to Public. After this you can use your code and it should run fine.
It is generally a good practice to keep the modifiers of controls as private. You can expose a property in the class and can access any of the sub properties of the control as illustrated in optoin 1. Hope this helps.