[英]How to add Items to a ComboBox from a different Form in C#
I have tried this many times, many different ways. 我已经尝试了很多次,许多不同的方式。 And I can't get the
ComboBox
populated. 而且我无法填充
ComboBox
。
public partial class Login : Form
{
public void populateTenants(dynamic tenants)
{
Form1 main = new Form1();
foreach (dynamic tenant in tenants.tenants)
{
string tenantName=tenant.name;
main.addTenant(tenantName);
}
}
}
public partial class Form1 : Form
{
public void addTenant(string item)
{
cbTenants.Items.Add(item);
}
}
I can do this and it works great: 我可以做到,效果很好:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
cbTenants.Items.Add("Test");
}
}
And I can do this: 我可以这样做:
public partial class Form1 : Form
{
public void addTenant(string item)
{
MessageBox.Show(item);
}
}
And I get a ton of MessageBoxes. 我得到了大量的MessageBoxes。
You can make first Form's combobox 'Modifires' Private to Public and can get items other forms a simple foreach loop; 您可以将第一个Form的组合框“ Modifires”设置为“公开”私有,并可以通过简单的foreach循环获取其他形式的项目;
You can get items: 您可以获得物品:
Form1 frm = new Form1();
foreach (string item in frm.comboBox1.Items)
{
comboBox1.Items.Add(item);
}
Like this ... 像这样 ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.