如何使用TableLayoutPanel动态添加新的组合框? [英] How to use a TableLayoutPanel to dynamically add new comboBoxes?
本文介绍了如何使用TableLayoutPanel动态添加新的组合框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中如何获取用户在最后一个for循环中选择的comboBox项目?
ComboBox cmbx;
int k = 0 ;
for ( int i = 0 ; i < n; i ++)
{
cmbx = new 组合框();
cmbx.Name = string .Format( MyCmb_ {0},i.ToString());
cmbxf.Text = yes;
cmbxf.Items.Add( yes);
cmbxf.Items.Add( no);
tableLayoutPanel1.Controls.Add(cmbx);
}
for ( int i = 0 ; i< m; i ++)
if (cmbx [i] .SelectedItems == 1 )k ++; /// //怎么做?????????
解决方案
public class ClassName
{
// 定义班级中的组合框列表
public 列表<组合框>组合框{ get ; set ;}
public ClassName()
{
comboboxes = new List< combobox>();
}
private void AddItems()
你的for循环中的{
// 将项目添加到tableLayoutPanel1.Controls
< span class =code-comment> // 也将相同的项添加到下面的组合框中
// comboboxes.Add(cmbx);
}
private void CountItems()
{
// 现在您可以从组合框列表中查看所选项目数
}
} < / combobox > < / combobox > 跨度>
In following code how to get comboBox item selected by a user in the last for loop?
ComboBox cmbx;
int k=0;
for (int i = 0; i < n; i++)
{
cmbx = new ComboBox();
cmbx.Name = string.Format("MyCmb_{0}", i.ToString());
cmbxf.Text="yes";
cmbxf.Items.Add("yes");
cmbxf.Items.Add("no");
tableLayoutPanel1.Controls.Add(cmbx);
}
for (int i=0;i<m;i++)
if (cmbx[i].SelectedItems==1) k++; ///// How to doing it?????????
解决方案
public class ClassName { // define list of combobox in your class public List<combobox> comboboxes {get;set;} public ClassName() { comboboxes = new List<combobox>(); } private void AddItems() { // in your for loop add items to tableLayoutPanel1.Controls // also add the same item to comboboxes like below // comboboxes.Add(cmbx ); } private void CountItems() { // now you can check the selected items count from comboboxes list } }</combobox></combobox>
这篇关于如何使用TableLayoutPanel动态添加新的组合框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文