jay_t55
jay_t55

Reputation: 11662

Cannot access control on another form even though Modifier is set to PUBLIC

I am trying to access a on my MainForm from another Form. This control is a FlowLayoutPanel, and I have set its Access Modifier to Public. I don't know why I can't access it from another form, because this method has always worked for me in the past.

MainForm.cs:

void button1_Click(object sender, EventArgs e)
{
    using(var editor = new Editor())
    {
        editor.ShowDialog();
    }
}

Editor.cs:

void button1_Click(object sender, EventArgs e)
{
    int count = MainForm.flow.Count;
}

Why can I not access this control from another form - even though its modifier is set to public?

Upvotes: 0

Views: 264

Answers (1)

martin
martin

Reputation: 299

you are accessing the control/property wrong.

You should do it like this.

MainForm.cs

private void button1_Click(object sender, EventArgs e)
{
  var frm = new Editor();
  frm.ShowDialog(this);
}

Editor.cs

private void button1_Click(object sender, EventArgs e)
{
    var f = (this.Owner as MainForm);
    int count = f.flow.Count;
    MessageBox.Show(count.ToString());
}

Upvotes: 2

Related Questions