如何在运行时在表单上添加comboBoxes,然后使用它们? [英] How can I add comboBoxes on a form at runtime, then use them?

查看:64
本文介绍了如何在运行时在表单上添加comboBoxes,然后使用它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我有点问题.我被卡住了.

假设我有一个表单,用户可以在其中输入变量的名称(与程序无关,所以我很喜欢这个名称).然后,对于该变量,有2,3,4,5,10个可能的值.我有一个包含对象的arraylist之类的东西,每个对象都是一个类实例,成员包括"varName"和varValues [].因此,事实是,用户可以存储多个变量名称,每个变量都有其可能的值.
我想做的是,在另一个窗体上显示一个添加了所有变量的列表(不是很难的),并且,对于每个变量,一个组合框都包含与该变量名关联的值.

事实是,当我启动应用程序时,我不知道用户会添加多少个变量.我只在打开第二个表格时才知道.如何在运行时添加组合框?他们必须有不同的名字,不是吗?在添加它们之后,我的意思是我该如何为运行该应用程序之前不存在的内容添加代码(如何访问它们)?

PS>我知道这里有很多信息,但是我已经为此撞了一段时间了,有点儿开始受伤了:)
任何想法都值得赞赏.

Hey guys, I''m having somewhat of a problem. I''m stuck.

Let''s say I have a form where the user enters the name of a variable (not program related, so just the name interests me). Then, for that variable, there are 2,3,4,5,10 possible values. I have something like an arraylist containing objects, and each of the object is a class instance with members like "varName" and varValues[]. So, the fact is, the user can store multiple names of variables, each with it''s possible values.
What I want to do, is display on another form a list with all the variables added (not hard) and also, for each of them a combobox containing the values associated with that variablename.

The fact is, when I start the app, I have no ideea how many variables will the user add. I find out only when opening that second form. how can I add the comboboxes at runtime? they have to have different names, no? And after I add them, I mean how can I add code for something that before i run the app does not exist (how can I access them)?

PS> I know there''s much info here, but I''ve been bumping my head on this for a while and it kinda'' started to hurt :)
Any ideas are appreciated.

推荐答案

ComboBox cb = new ComboBox();
Controls.Add(cb);


您可能想设置一个ComboBoxes数组/列表以供返回参考(尽管您可以通过遍历Form.Controls并检查这是ComboBox吗?"来访问它们)
您将需要设置每个ComboBox的Location并将其链接到您需要手动处理的任何事件中.


You may want to set up an array / list of ComboBoxes to refer back to (although you can access them by iterating through the Form.Controls and checking "is this a ComboBox?")
You will need to set the Location of each ComboBox and chain into any event you need to handle manually.


这篇关于如何在运行时在表单上添加comboBoxes,然后使用它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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