简体   繁体   中英

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 ?

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());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM