动态将项目添加到组合框 [英] Add item to combobox dynamically

查看:82
本文介绍了动态将项目添加到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

private void btnok_Click(object sender, EventArgs e)
{
    string name = tbxname.Text;
    Form2 ff = new Form2();
    ff.comboBox1.Items.Add(name);
    ff.ShowDialog();
}



上面给出的代码在form1中. btnok.
我想通过此代码向form2.combobox1添加一些项目(form2中的combobox声明为public).它在组合框项目中进行了更新.但是当我们关闭解决方案并再次运行它时,同样只有一个项目,旧的项目不见了.我该如何实现.



The above given code is present in form1. btnok.
I want to add some item to form2.combobox1 through this code(combobox in form2 is declared as public).it updated in the combobox items.But when we close the solution and again run it,again there is only one item,the old item is gone.how do i achieve it.
Your help is needed.

推荐答案

您知道,您实际上必须在组合框中设置所选项目,然后该项目才能显示在编辑字段中,对吗? />
You are aware that you actually have to set the selected item in the combobox before it shows up in the edit field, right?

ff.comboBox1.SelectedIndex = 0;





or

ff.comboBox1.SelectedItem = name;


除了John的答案,Form 2上的ComboBox还需要将其访问器更改为public.

如果您告诉我们但不起作用"的实际含义,则可能会帮助您获得更具体的答案.
In addition to John''s answer, the ComboBox on Form 2 needs to have its accessor changed to public.

It might help you to get a more specific answer if you tell us what ''But its not working.'' actually means. In what way is it not working?


如果我理解正确,那么您正在使用form1上的按钮在运行时动态地向form2.combobox添加其他项.但是,当关闭解决方案并再次运行"时,form2上的组合框仅显示一个项目(可能是您在设计时或应用程序加载时添加的项目)?

如果要在第二个下拉列表中动态添加项目,并使数据在应用程序中重新存在,则需要将数据存储在文件或数据库中,然后将组合框重新加载或绑定到所述文件/数据库每次加载应用程序时.

请澄清您的问题...
If I understand correctly, you''re using a button on form1 to dynamically add an additional item to form2.combobox at runtime. However, when you "close the solution and run again" the combobox on form2 only displays a single item (that you presumably added at design time or application load)?

If you want to dynamically add an item to the second drop down and have the data persist across an application restart you''ll need to store the data in a file or database, then re-load or bind the comboboxes to said file/database each time the appplication loads.

Please clarify your question...


这篇关于动态将项目添加到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆