Access a form's control e.g. Label1 directly from a Self Defined Class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Que Est
    New Member
    • Dec 2011
    • 1

    Access a form's control e.g. Label1 directly from a Self Defined Class

    How on planet earth do you access a form's component e.g Label from a self defined class.

    Class SelfDefinedClas s
    {
    ....
    public void myMethod()
    {
    Label1.Text = "yzyzyzyz";
    }
    }
  • adriancs
    New Member
    • Apr 2011
    • 122

    #2
    go to the designer of that form.
    for example: Form2.Designer. cs
    change the properties of the component from private to public
    example:
    change this
    Code:
    private System.Windows.Forms.Label label1;
    to this
    Code:
    public System.Windows.Forms.Label label1;
    then upon creating/or after closing the Form, you'll be able to gain access the value of the component.
    example:
    Code:
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.label1.Text = "Change to this text";
        f2.ShowDialog();
        MessageBox.Show(f2.label1.text);
    }

    Comment

    Working...