1. 程式人生 > >WinForm 利用Tag快速獲取已賦值控制元件的條件

WinForm 利用Tag快速獲取已賦值控制元件的條件

一、\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}
[email protected]
{0} ", tag); paramList.Add(new SqlParameter("@"+tag, tb.Text.Trim())); } if(c is RadioButton && rb.Checked && rb.Text!="全部") { sb.AppendFormat(" and {0}[email protected]{0} ", tag); paramList.Add(new SqlParameter("@" + tag, rb.Text=="是")); } if (c is ComboBox && cb.Text.Trim()!="") { sb.AppendFormat(" and {0}
[email protected]
{0} ", tag); paramList.Add(new SqlParameter("@" + tag, cb.Text.Trim())); } } MessageBox.Show(sb.ToString()); } } }