Salah Salah
Salah Salah

Reputation: 65

access controls in Form1 from another class

I have one control textBox1 that sits in my main form Form1. I want to be able to change the textBox1 text from another class another_class, but I can't do it. My another_class has a event teacher that I handle from Form1 by doing the following

private void button1_Click(object sender, EventArgs e)
{
    another_class myNew_another_class = new another_class();
    myNew_another_class.teacher(sender, e);
}

So I can't create the following in another_class because it will mess with my handler above and red tag it

public another_class (Form1 anytext_Form)
{
    this.anytext_Form = anytext_Form;
} 

Upvotes: 0

Views: 5313

Answers (5)

Ken Kin
Ken Kin

Reputation: 4683

Correct the syntax by:

partial class Form1 {
    private void button1_Click(object sender, EventArgs e) {
        another_class myNew_another_class=new another_class(this);
        myNew_another_class.teacher(sender, e);
    }
}

public partial class another_class {
    Form anytext_Form;

    public void teacher(object sender, EventArgs e) {
        // do something
    }

    public another_class(Form anytext_Form) {
        this.anytext_Form=anytext_Form;
    }
}

Upvotes: 1

Ruly
Ruly

Reputation: 360

I don't think you state your question clearly. What is teacher method doing?

However, as mentioned by others, all control access modifier is Private, so you cannot just directly access it. You can try to change the access modifier in the property of the object, or create a property:

public class Form1 : Form {
    public String TextboxText {
        set { this.myTextbox.Text = value; }
        get { return this.myTextbox.Text; }
    }
}

Upvotes: 0

jordanhill123
jordanhill123

Reputation: 4182

Change to this:

private void button1_Click(object sender, EventArgs e)
{
     another_class myNew_another_class = new another_class(this); //where this is Form1
     myNew_another_class.teacher(sender, e);
}

This then is the constructor for your "another_class" as you had it.

public another_class (Form1 anytext_Form)
{
         this.anytext_Form = anytext_Form;
} 

Upvotes: 0

Grzegorz W
Grzegorz W

Reputation: 3517

change this:

another_class myNew_another_class = new another_class();

to this:

another_class myNew_another_class = new another_class(this);

Upvotes: 0

ken2k
ken2k

Reputation: 48975

I think you should explain what you actually what to do, because your event management doesn't look good IMO. Maybe the event isn't useful, or maybe you could refactor it if you tell us what you want to achieve actually.

To answer the question in your title, controls in another form are private members, so you can't access them outside the scope of the parent form. What you can do is expose public method that'll do the job:

public class Form1 : Form
{
    public void SetMyText(string text)
    {
        this.myTextbox.Text = text;
    }
}

public class Form2 : Form
{
    public void Foo()
    {
        var frm1 = new Form1();
        frm1.SetMyText("test");
    }
}

Upvotes: 0

Related Questions