foreach (Control c in this.Controls)
            {
                if (c is TextBox)
                {
                    if (string.IsNullOrEmpty((c as TextBox).Text))
                    {
                        MessageBox.Show("文本框不能为空");
                        break;
                    }
                    else
                    {
                        MessageBox.Show("添加成功");
                        this.Close();
                    }

流程:遍历所有组件
判断是否为textbox
判断textBox的文本是否为空
是则弹出“文本框不能为空”
否则弹出“添加成功”

ps:直接把代码套在按钮事件即可运行
例:

   private void button1_Click(object sender, EventArgs e)
    {
        foreach (Control c in this.Controls)
        {
            if (c is TextBox)
            {
                if (string.IsNullOrEmpty((c as TextBox).Text))
                {
                    MessageBox.Show("no");
                    break;
                }
                else
                {
                    MessageBox.Show("添加成功");
                    this.Close();
                }
            }
        }
    }

更多推荐