admin管理员组

文章数量:1122833

I have two Forms (Form1 and Form2), in Form1 I create the instance of Form2 and show it.

public void buttonFrom1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.Show();
}

How can I access the ComboBox in Form1 from Form2? I tried to access from the From1 instance created in another form but I receive the error: "cs0103 the name "frm1" does not exist in the current context".

public void buttonForm2_Click(object sender, EventArgs e)
{
    string item = frm1.BanksComboBox.SelectedItem.ToString();
    MessageBox.Show(item);
}

I have two Forms (Form1 and Form2), in Form1 I create the instance of Form2 and show it.

public void buttonFrom1_Click(object sender, EventArgs e)
{
    Form2 frm2 = new Form2();
    frm2.Show();
}

How can I access the ComboBox in Form1 from Form2? I tried to access from the From1 instance created in another form but I receive the error: "cs0103 the name "frm1" does not exist in the current context".

public void buttonForm2_Click(object sender, EventArgs e)
{
    string item = frm1.BanksComboBox.SelectedItem.ToString();
    MessageBox.Show(item);
}
Share Improve this question edited Nov 21, 2024 at 10:37 wohlstad 26.9k16 gold badges54 silver badges83 bronze badges asked Nov 21, 2024 at 9:06 F.T.F.T. 51 silver badge6 bronze badges 4
  • 1 The proper way to do this would be to have Form2 raise an event. Form1 can handle that event and pass the data back to Form2 via the e parameter. That way, Form2 doesn't have to know that Form1 exists and you maintain loose coupling. See here for more info. – jmcilhinney Commented Nov 21, 2024 at 12:00
  • 1 Can we assume that the selection can change between when Form2 is created and the value is used? – jmcilhinney Commented Nov 21, 2024 at 12:01
  • 1 I'm not sure why you would think that frm1 would exist there anyway. You didn't declare it anywhere so why would it exist? If you want to do it the dodgy way then replace frm1 with Form1, i.e. the type name. That will refer to the default instance of the Form1 type and, assuming that Form1 is the startup form for the app, that will be the correct instance. See here for more info. – jmcilhinney Commented Nov 21, 2024 at 12:04
  • The following may be of interest: stackoverflow.com/a/69743297/10024425 (it's written for VB.NET, but the same concepts apply to C#). – It all makes cents Commented Nov 21, 2024 at 17:28
Add a comment  | 

1 Answer 1

Reset to default 0

Here is a "tightly coupled" solution, the opposite of "loosely coupled", which was CORRECTLY suggested by jmcilhinney in the comments.

It's accomplished via the Owner property of the Form, which you set when you call Show():

public partial class Form1 : Form
{

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm2 = new Form2();
        frm2.Show(this); // <-- pass in this Form1 instance as the "Owner".
    }

}

Then, in Form2, you cast the Owner back to Form1 and access the ComboBox (assuming its Modifiers property is public):

public partial class Form2 : Form
{

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 frm1 = this.Owner as Form1;
        if (frm1 != null)
        {
            string item = frm1.BanksComboBox.SelectedItem.ToString();
            MessageBox.Show(item);
        }
    }

}

I like this approach as it's pretty easy to understand and also implement without changing much code. Only use this approach if Form2 only ever gets used in conjunction with Form1. In other words, if Form2 is useless without Form1, then this approach might be a reasonable option. If Form2 is more generic in nature and will be used with other Forms besides Form1, then a loosely coupled approach would make more sense.

本文标签: cAccess ComboBoxSelectedItem from a different formStack Overflow