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 Answer
Reset to default 0Here 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
版权声明:本文标题:c# - Access ComboBox.SelectedItem from a different form - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736312250a1935029.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Form2
raise an event.Form1
can handle that event and pass the data back toForm2
via thee
parameter. That way,Form2
doesn't have to know thatForm1
exists and you maintain loose coupling. See here for more info. – jmcilhinney Commented Nov 21, 2024 at 12:00Form2
is created and the value is used? – jmcilhinney Commented Nov 21, 2024 at 12:01frm1
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 replacefrm1
withForm1
, i.e. the type name. That will refer to the default instance of theForm1
type and, assuming thatForm1
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