如题,WINFORM程序中是将textBox1——textBox20中的内容保存   
kin.Dal.HisRecord dal = new kin.Dal.HisRecord();
                        kin.Model.HisRecord model;
dal.Add(model);
再打印出来。
想在保存时循环判断textBox是否为空,若为空不可保存。
应该怎么写循环判断。

解决方案 »

  1.   

    foreach(control con in this.controls){
    if(con.typeof(textbox){}
    }貌似是这样。 具体的代码等会贴上来吧。。 
      

  2.   

     private void GetControl(Control.ControlCollection ctc)
                  {
                 foreach (Control ct in ctc)
                      {
                       //调用AddControlInofToListBox方法获取控件信息
                          AddControlInofToListBox(ct);
                     //C#只遍历窗体的子控件,不遍历孙控件
                        //当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
                     if (ct.HasChildren)
                          {
                              GetControl(ct.Controls);
                          }
                      }
                  }        private void AddControlInofToListBox(Control ct)
                  {
                switch (ct.GetType().Name)
                      {
                     //如果是ListBox、CheckBox、Button
                    case "ListBox":
                    case "GroupBox":
                    case "Button":
                              listBox1.Items.Add("控件名:" + ct.Name);
                        break;
                     //如果是CheckBox
                    case "CheckBox":
                        if (((CheckBox)ct).Checked)
                              {
                                  listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:是");
                             //((CheckBox)ct).Checked = false;
                              }
                        else
                              {
                                  listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:否");
                             //((CheckBox)ct).Checked = true;
                              }
                        break;
                     //如果是RadioButton
                    case "RadioButton":
                         RadioButton rdb = (RadioButton)ct;
                        if (rdb.Checked)
                              {
                                  listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:是");
                                  //rdb.Checked = false;
                              }
                        else
                              {
                                  listBox1.Items.Add("控件名:" + ct.Name + ",是否选中:否");
                                  //rdb.Checked = true;
                              }
                        break;
                    //其它值
                     default:
                              listBox1.Items.Add("控件名:" + ct.Name + ",值:" + ct.Text);
                         break;
                      }
                  }
      

  3.   

    谢谢。
    我是用的判断  foreach (Control c in groupBox1.Controls)
                {
                    if (c is TextBox)
                    {
                        if (c.Text == "")
                        {
                            MessageBox.Show("内容不完整,无法保存!");
                            return;
                        }
                    }
                }
      

  4.   

    但是现在textbox放在多个groupBox,
    groupBox1\groupBox2\groupBox3,只会分别写三个上边的判断,有没有其它整合方法。
      

  5.   

    foreach (Control c in this.Controls)
    {
        GroupBox gbx = c as GroupBox;
        if (gbx != null)
        {
            foreach (Control item in gbx.Controls)
            {
                TextBox tbx = item as TextBox;
                if (tbx != null)
                {   
                    if (tbx.Text == "")
                    {
                        MessageBox.Show("内容不完整,无法保存!");
                        return;
                    }
                }
            }
        }
    }