WinForm 利用Tag快速获取已赋值控件的条件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yenange/article/details/84648645

一、\WindowsFormsApp1\Form1.Designer.cs

namespace WindowsFormsApp1
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要修改
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.txt_username = new System.Windows.Forms.TextBox();
            this.label2 = new System.Windows.Forms.Label();
            this.cbUState = new System.Windows.Forms.ComboBox();
            this.cbTQList = new System.Windows.Forms.ComboBox();
            this.label3 = new System.Windows.Forms.Label();
            this.label4 = new System.Windows.Forms.Label();
            this.label5 = new System.Windows.Forms.Label();
            this.panel1 = new System.Windows.Forms.Panel();
            this.btnSearch = new System.Windows.Forms.Button();
            this.dataGridView1 = new System.Windows.Forms.DataGridView();
            this.rdoIsoltestqusYes = new System.Windows.Forms.RadioButton();
            this.rdoIsoltestqusNo = new System.Windows.Forms.RadioButton();
            this.rdo_isoltestqus_all = new System.Windows.Forms.RadioButton();
            this.panel2 = new System.Windows.Forms.Panel();
            this.rdo_istqaudit_all = new System.Windows.Forms.RadioButton();
            this.rdo_istqaudit_no = new System.Windows.Forms.RadioButton();
            this.rdo_istqaudit_yes = new System.Windows.Forms.RadioButton();
            this.panel1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
            this.panel2.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 14);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(77, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "按用户名搜索";
            // 
            // txt_username
            // 
            this.txt_username.Location = new System.Drawing.Point(100, 10);
            this.txt_username.Name = "txt_username";
            this.txt_username.Size = new System.Drawing.Size(100, 21);
            this.txt_username.TabIndex = 1;
            this.txt_username.Tag = "username";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(211, 14);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(89, 12);
            this.label2.TabIndex = 2;
            this.label2.Text = "按用户状态搜索";
            // 
            // cbUState
            // 
            this.cbUState.FormattingEnabled = true;
            this.cbUState.Location = new System.Drawing.Point(311, 10);
            this.cbUState.Name = "cbUState";
            this.cbUState.Size = new System.Drawing.Size(121, 20);
            this.cbUState.TabIndex = 3;
            this.cbUState.Tag = "userstate";
            // 
            // cbTQList
            // 
            this.cbTQList.FormattingEnabled = true;
            this.cbTQList.Location = new System.Drawing.Point(543, 10);
            this.cbTQList.Name = "cbTQList";
            this.cbTQList.Size = new System.Drawing.Size(121, 20);
            this.cbTQList.TabIndex = 4;
            this.cbTQList.Tag = "intestqus";
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(443, 14);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(89, 12);
            this.label3.TabIndex = 5;
            this.label3.Text = "按所属题库搜索";
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.Location = new System.Drawing.Point(12, 55);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(89, 12);
            this.label4.TabIndex = 6;
            this.label4.Text = "是否有离线试题";
            // 
            // label5
            // 
            this.label5.AutoSize = true;
            this.label5.Location = new System.Drawing.Point(300, 56);
            this.label5.Name = "label5";
            this.label5.Size = new System.Drawing.Size(89, 12);
            this.label5.TabIndex = 9;
            this.label5.Text = "是否有审题试题";
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.rdo_isoltestqus_all);
            this.panel1.Controls.Add(this.rdoIsoltestqusNo);
            this.panel1.Controls.Add(this.rdoIsoltestqusYes);
            this.panel1.Location = new System.Drawing.Point(105, 41);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(165, 40);
            this.panel1.TabIndex = 12;
            // 
            // btnSearch
            // 
            this.btnSearch.Location = new System.Drawing.Point(589, 50);
            this.btnSearch.Name = "btnSearch";
            this.btnSearch.Size = new System.Drawing.Size(75, 23);
            this.btnSearch.TabIndex = 14;
            this.btnSearch.Text = "搜索";
            this.btnSearch.UseVisualStyleBackColor = true;
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            // 
            // dataGridView1
            // 
            this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
            this.dataGridView1.Location = new System.Drawing.Point(13, 91);
            this.dataGridView1.Name = "dataGridView1";
            this.dataGridView1.RowTemplate.Height = 23;
            this.dataGridView1.Size = new System.Drawing.Size(651, 264);
            this.dataGridView1.TabIndex = 15;
            // 
            // rdoIsoltestqusYes
            // 
            this.rdoIsoltestqusYes.AutoSize = true;
            this.rdoIsoltestqusYes.Location = new System.Drawing.Point(74, 15);
            this.rdoIsoltestqusYes.Name = "rdoIsoltestqusYes";
            this.rdoIsoltestqusYes.Size = new System.Drawing.Size(35, 16);
            this.rdoIsoltestqusYes.TabIndex = 9;
            this.rdoIsoltestqusYes.TabStop = true;
            this.rdoIsoltestqusYes.Tag = "isoltestqus";
            this.rdoIsoltestqusYes.Text = "是";
            this.rdoIsoltestqusYes.UseVisualStyleBackColor = true;
            // 
            // rdoIsoltestqusNo
            // 
            this.rdoIsoltestqusNo.AutoSize = true;
            this.rdoIsoltestqusNo.Location = new System.Drawing.Point(115, 14);
            this.rdoIsoltestqusNo.Name = "rdoIsoltestqusNo";
            this.rdoIsoltestqusNo.Size = new System.Drawing.Size(35, 16);
            this.rdoIsoltestqusNo.TabIndex = 10;
            this.rdoIsoltestqusNo.TabStop = true;
            this.rdoIsoltestqusNo.Tag = "isoltestqus";
            this.rdoIsoltestqusNo.Text = "否";
            this.rdoIsoltestqusNo.UseVisualStyleBackColor = true;
            // 
            // rdo_isoltestqus_all
            // 
            this.rdo_isoltestqus_all.AutoSize = true;
            this.rdo_isoltestqus_all.Location = new System.Drawing.Point(23, 14);
            this.rdo_isoltestqus_all.Name = "rdo_isoltestqus_all";
            this.rdo_isoltestqus_all.Size = new System.Drawing.Size(47, 16);
            this.rdo_isoltestqus_all.TabIndex = 11;
            this.rdo_isoltestqus_all.TabStop = true;
            this.rdo_isoltestqus_all.Tag = "";
            this.rdo_isoltestqus_all.Text = "全部";
            this.rdo_isoltestqus_all.UseVisualStyleBackColor = true;
            // 
            // panel2
            // 
            this.panel2.Controls.Add(this.rdo_istqaudit_all);
            this.panel2.Controls.Add(this.rdo_istqaudit_no);
            this.panel2.Controls.Add(this.rdo_istqaudit_yes);
            this.panel2.Location = new System.Drawing.Point(395, 41);
            this.panel2.Name = "panel2";
            this.panel2.Size = new System.Drawing.Size(165, 40);
            this.panel2.TabIndex = 13;
            // 
            // rdo_istqaudit_all
            // 
            this.rdo_istqaudit_all.AutoSize = true;
            this.rdo_istqaudit_all.Location = new System.Drawing.Point(23, 14);
            this.rdo_istqaudit_all.Name = "rdo_istqaudit_all";
            this.rdo_istqaudit_all.Size = new System.Drawing.Size(47, 16);
            this.rdo_istqaudit_all.TabIndex = 11;
            this.rdo_istqaudit_all.TabStop = true;
            this.rdo_istqaudit_all.Tag = "";
            this.rdo_istqaudit_all.Text = "全部";
            this.rdo_istqaudit_all.UseVisualStyleBackColor = true;
            // 
            // rdo_istqaudit_no
            // 
            this.rdo_istqaudit_no.AutoSize = true;
            this.rdo_istqaudit_no.Location = new System.Drawing.Point(115, 14);
            this.rdo_istqaudit_no.Name = "rdo_istqaudit_no";
            this.rdo_istqaudit_no.Size = new System.Drawing.Size(35, 16);
            this.rdo_istqaudit_no.TabIndex = 10;
            this.rdo_istqaudit_no.TabStop = true;
            this.rdo_istqaudit_no.Tag = "istqaudit";
            this.rdo_istqaudit_no.Text = "否";
            this.rdo_istqaudit_no.UseVisualStyleBackColor = true;
            // 
            // rdo_istqaudit_yes
            // 
            this.rdo_istqaudit_yes.AutoSize = true;
            this.rdo_istqaudit_yes.Location = new System.Drawing.Point(74, 15);
            this.rdo_istqaudit_yes.Name = "rdo_istqaudit_yes";
            this.rdo_istqaudit_yes.Size = new System.Drawing.Size(35, 16);
            this.rdo_istqaudit_yes.TabIndex = 9;
            this.rdo_istqaudit_yes.TabStop = true;
            this.rdo_istqaudit_yes.Tag = "istqaudit";
            this.rdo_istqaudit_yes.Text = "是";
            this.rdo_istqaudit_yes.UseVisualStyleBackColor = true;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(683, 367);
            this.Controls.Add(this.panel2);
            this.Controls.Add(this.dataGridView1);
            this.Controls.Add(this.btnSearch);
            this.Controls.Add(this.panel1);
            this.Controls.Add(this.label5);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.cbTQList);
            this.Controls.Add(this.cbUState);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.txt_username);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Form1";
            this.panel1.ResumeLayout(false);
            this.panel1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
            this.panel2.ResumeLayout(false);
            this.panel2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.TextBox txt_username;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.ComboBox cbUState;
        private System.Windows.Forms.ComboBox cbTQList;
        private System.Windows.Forms.Label label3;
        private System.Windows.Forms.Label label4;
        private System.Windows.Forms.Label label5;
        private System.Windows.Forms.Panel panel1;
        private System.Windows.Forms.RadioButton rdo_isoltestqus_all;
        private System.Windows.Forms.RadioButton rdoIsoltestqusNo;
        private System.Windows.Forms.RadioButton rdoIsoltestqusYes;
        private System.Windows.Forms.Button btnSearch;
        private System.Windows.Forms.DataGridView dataGridView1;
        private System.Windows.Forms.Panel panel2;
        private System.Windows.Forms.RadioButton rdo_istqaudit_all;
        private System.Windows.Forms.RadioButton rdo_istqaudit_no;
        private System.Windows.Forms.RadioButton rdo_istqaudit_yes;
    }
}

二、WindowsFormsApp1\Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 递归获取所有控件(不包含容器控件)
        /// </summary>
        /// <param name="ctc"></param>
        /// <param name="list"></param>
        private void GetControl(Control.ControlCollection ctc,List<Control> list)
        {
            foreach (Control ct in ctc)
            {
                //C# 只遍历窗体的子控件,不遍历孙控件
                //当窗体上的控件有子控件时,需要用递归的方法遍历,才能全部列出窗体上的控件
                if (ct.HasChildren)
                {
                    GetControl(ct.Controls, list);
                }
                else
                {
                    list.Add(ct);
                }
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder("select * from user_data where 1=1 ");
            List<SqlParameter> paramList = new List<SqlParameter>();
            List<Control> list = new List<Control>();
            GetControl(this.Controls, list);
            foreach(Control c in list)
            {
                TextBox tb = c as TextBox;
                RadioButton rb = c as RadioButton;
                ComboBox cb = c as ComboBox;
                string tag = c.Tag as string;
                if (string.IsNullOrEmpty(tag))
                    continue;

                if (c is TextBox && tb.Text.Trim() != "")
                {
                    sb.AppendFormat(" and {0}=@{0} ", tag);
                    paramList.Add(new SqlParameter("@"+tag, tb.Text.Trim()));
                }
                if(c is RadioButton && rb.Checked && rb.Text!="全部")
                {
                    sb.AppendFormat(" and {0}=@{0} ", tag);
                    paramList.Add(new SqlParameter("@" + tag, rb.Text=="是"));
                }
                if (c is ComboBox && cb.Text.Trim()!="")
                {
                    sb.AppendFormat(" and {0}=@{0} ", tag);
                    paramList.Add(new SqlParameter("@" + tag, cb.Text.Trim()));
                }
            }
            MessageBox.Show(sb.ToString());
        }
    }
}

猜你喜欢

转载自blog.csdn.net/yenange/article/details/84648645