如何找到具体的动态控制并获得价值

时间:2017-09-27 06:44:30

标签: c# winforms

我正在制作学生出勤系统。我正在创建动态控件并从数据库中分配值。现在我想知道如何找到所需的动态控制以及如何从中获得价值。

我不知道如何使用foreach循环找到所需的控件。

这是我创建动态控件的代码。

public void genControl(StudentAttendence sta)
{
    StudentAttendenceBSLDAL stabd = new StudentAttendenceBSLDAL();
    List<string[]> liName = stabd.takStudent(sta);
    counts = Convert.ToInt16(stabd.takStudent(sta).Count);
    for (int i=0; i< stabd.takStudent(sta).Count;i++)
    {
        for(int j = 0; j<liName[i].Count();j++)
        {
            Label lblStudentname = new Label();
            Label lblStId = new Label();
            lblStId.Name = "lblStId"+i.ToString()+j.ToString();
            lblStudentname.Name = "liName"+i.ToString()+j.ToString();
            lblStId.AutoSize = true;
            lblStudentname.AutoSize = true;
            if (j==0)
            {
                lblStId.Text = liName[i][j].ToString(); 

            }
            if(j==1)
            {
                lblStudentname.Text = liName[i][j].ToString();

            }
            lblStId.AutoSize = true;

            lblStudentname.AutoSize = true;
            if (i == 1)
            {
                lblStId.Location = new Point(41, 229);
                lblStudentname.Location = new Point(153, 7);
            }
            else
            {
                lblStId.Location = new Point(3, 7 + 20);
                lblStudentname.Location = new Point(153, 7 + 20);
            }

            this.Controls.Add(lblStId);
            panel1.Controls.Add(lblStudentname);
        }
        CheckBox cba = new CheckBox();
        cba.Name = "cba" + i.ToString() ;
        cba.Text = "A";
        cba.AutoSize = true;

        CheckBox cbp = new CheckBox();
        cbp.Name = i.ToString()  ;
        cbp.Text = "P";
        cbp.AutoSize = true;

        CheckBox cbl = new CheckBox();
        cbl.Name = "cbl" + i.ToString() ;
        cbl.Text = "L";
        cbl.AutoSize = true;
        if (i == 1)
        {
            cbp.Location = new Point(590, 3);
            cba.Location = new Point(631, 3);
            cbl.Location = new Point(670, 3);
        }
        else
        {
            cbp.Location = new Point(590, 3 + 23);
            cba.Location = new Point(631, 3 + 23);
            cbl.Location = new Point(670, 3 + 23);
        }
        panel1.Controls.Add(cbp);
        panel1.Controls.Add(cba);
        panel1.Controls.Add(cbl);
    }
}

这是按钮控制事件,我在其中尝试查找控件并获取其值。

private void button2_Click(object sender, EventArgs e)
{
    StudentAttendence sta = new StudentAttendence();
    StudentAttendenceBSLDAL stabd = new StudentAttendenceBSLDAL();
  //  List<string[]> liName = stabd.takStudent(sta);
    for (int i=0;i<counts;i++)
    {
         CheckBox cbP = panel1.Controls.OfType<CheckBox>().FirstOrDefault(b => b.Name.Equals("cbp"+i.ToString()));

     //   Label stid = panel1.Controls.Find("lblStId" + i.ToString(), false).First() as Label;
        if(!cbP.IsChecked)
        {
            MessageBox.Show("control found");
        }
     }
}

1 个答案:

答案 0 :(得分:0)

您可以将控制列表保存到SortedList中。 您可以使用此排序列表进行进一步处理