天天看点

C# 遍历容器中所有控件(一)

备注 :代码为借鉴,目的为方便自己做项目

  1. 直上代码

    1. 首先要明确遍历的目的,以及容器内各个控件的ID ,精确定位方便查看该控件的具体属性及参数

    2. 遍历容器内的控件,可以为后续可拖拽控件功能的实现带来功能属性获悉。

foreach (Control c in gb_SerailBox.Controls)
            {
                if (c is ComboBox )
                {
                    String strName= c.Name;
                    String strselectedIndexName = null;
                    int ui_selIndex;
                    if (strName.Equals("cbb_Port"))
                    {

                        ui_selIndex = cbb_Port.SelectedIndex;
                        strselectedIndexName = cbb_Port.SelectedItem.ToString();  
                        MessageBox.Show("端口号:"+ strselectedIndexName+"\n"+"下拉框Index:"+ ui_selIndex);     
                    }
                    else if (strName.Equals("cbb_BaudRate"))
                    {
                        ui_selIndex = cbb_BaudRate.SelectedIndex;
                        strselectedIndexName = cbb_BaudRate.SelectedItem.ToString();
                        MessageBox.Show("波特率:" + strselectedIndexName + "\n" + "下拉框Index:" + ui_selIndex);
                    }
                    else if (strName.Equals("cbb_DataBits"))
                    {
                        ui_selIndex = cbb_DataBits.SelectedIndex;
                        strselectedIndexName = cbb_DataBits.SelectedItem.ToString();
                        MessageBox.Show("数据位:" + strselectedIndexName + "\n" + "下拉框Index:" + ui_selIndex);
                    }
                    else if (strName.Equals("cbb_StopBits"))
                    {
                        ui_selIndex = cbb_StopBits.SelectedIndex;
                        strselectedIndexName = cbb_StopBits.SelectedItem.ToString();
                        MessageBox.Show("停止位:" + strselectedIndexName + "\n" + "下拉框Index:" + ui_selIndex);
                    }
                    else if (strName.Equals("cbb_StopBits"))
                    {
                        ui_selIndex = cbb_StopBits.SelectedIndex;
                        strselectedIndexName = cbb_StopBits.SelectedItem.ToString();
                        MessageBox.Show("停止位:" + strselectedIndexName + "\n" + "下拉框Index:" + ui_selIndex);
                    }
                    else if (strName.Equals("cbb_Parity"))
                    {
                        ui_selIndex = cbb_Parity.SelectedIndex;
                        strselectedIndexName = cbb_Parity.SelectedItem.ToString();
                        MessageBox.Show("校验位:" + strselectedIndexName + "\n" + "下拉框Index:" + ui_selIndex);
                    }
                    else if (strName.Equals("cbb_FlowControl"))
                    {
                        ui_selIndex = cbb_FlowControl.SelectedIndex;
                        strselectedIndexName = cbb_FlowControl.SelectedItem.ToString();
                        MessageBox.Show("数据流:" + strselectedIndexName + "\n" + "下拉框Index:" + ui_selIndex);
                    }
                }
                if (c is Label )
                {
                    String strName = c.Name;
                    MessageBox.Show(c.Text);
                }
            }
           

图例

C# 遍历容器中所有控件(一)
C# 遍历容器中所有控件(一)