天天看點

C#界面設計之ComboBox與ListBox的使用

同樣先來效果圖:

C#界面設計之ComboBox與ListBox的使用

主要代碼如下:

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

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

        private void Form1_Load(object sender, EventArgs e)
        {
            //設定cmbHouXuan隻能從ComboBox中的已有候選值選擇
            this.cmbHouXuan.DropDownStyle = ComboBoxStyle.DropDownList;
            //lstResult隻能執行單選,并且對所有值進行排序
            this.lstResults.SelectionMode = SelectionMode.One;
            this.lstResults.Sorted = true;
            this.GenerateCombItems( );          //産生ComboBox中的可選項
        }

        private void GenerateCombItems( )
        {
            this.cmbHouXuan.Items.Clear( );     //移除原有的資料
            Random rd = new Random();
            for (int i = ; i < ; i++)        //随機生成10個新的資料
            {
                string item = string.Format("Item-{0:X8}", rd.Next( ));
                this.cmbHouXuan.Items.Add(item);   //添加到ComboBox中
            }
            this.cmbHouXuan.SelectedIndex = ;      //預設選中第一條
        }
        //重新生成ComboBox中的侯選項
        private void btnFresh_Click(object sender, EventArgs e)
        {
            this.GenerateCombItems( );      //重新生成CombBox中的候選項
        }
        //将CombBox中選中的值添加到ListBox中
        private void btnAddOne_Click(object sender, EventArgs e)
        {
            //通過ComboBox.SelectedItem擷取目前選中的候選項,然後添加到ListBox中
            string item = (string)this.cmbHouXuan.SelectedItem;     
            this.lstResults.Items.Add(item);
        }
        //從ListBox中移除目前選中項
        private void btnRemoveOne_Click(object sender, EventArgs e)
        {
            if (this.lstResults.SelectedIndex >= )     //如果目前ListBox中有選中條目,移除它
            {
                this.lstResults.Items.RemoveAt(this.lstResults.SelectedIndex);
            }
        }
        //從ListBox中移除所有項
        private void btnRemovAll_Click(object sender, EventArgs e)
        {
            this.lstResults.Items.Clear( );
        }
    }
}
           

控件的标注資訊代碼如下:

namespace ComboListBox
{
    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.cmbHouXuan = new System.Windows.Forms.ComboBox( );
            this.lstResults = new System.Windows.Forms.ListBox( );
            this.label1 = new System.Windows.Forms.Label( );
            this.btnFresh = new System.Windows.Forms.Button( );
            this.btnAddOne = new System.Windows.Forms.Button( );
            this.btnRemoveOne = new System.Windows.Forms.Button( );
            this.btnRemovAll = new System.Windows.Forms.Button( );
            this.SuspendLayout( );
            // 
            // cmbHouXuan
            // 
            this.cmbHouXuan.FormattingEnabled = true;
            this.cmbHouXuan.Location = new System.Drawing.Point(, );
            this.cmbHouXuan.Name = "cmbHouXuan";
            this.cmbHouXuan.Size = new System.Drawing.Size(, );
            this.cmbHouXuan.TabIndex = ;
            // 
            // lstResults
            // 
            this.lstResults.FormattingEnabled = true;
            this.lstResults.ItemHeight = ;
            this.lstResults.Location = new System.Drawing.Point(, );
            this.lstResults.Name = "lstResults";
            this.lstResults.Size = new System.Drawing.Size(, );
            this.lstResults.TabIndex = ;
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(, );
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(, );
            this.label1.TabIndex = ;
            this.label1.Text = "候選項";
            // 
            // btnFresh
            // 
            this.btnFresh.Location = new System.Drawing.Point(, );
            this.btnFresh.Name = "btnFresh";
            this.btnFresh.Size = new System.Drawing.Size(, );
            this.btnFresh.TabIndex = ;
            this.btnFresh.Text = "重新整理候選項";
            this.btnFresh.UseVisualStyleBackColor = true;
            this.btnFresh.Click += new System.EventHandler(this.btnFresh_Click);
            // 
            // btnAddOne
            // 
            this.btnAddOne.Location = new System.Drawing.Point(, );
            this.btnAddOne.Name = "btnAddOne";
            this.btnAddOne.Size = new System.Drawing.Size(, );
            this.btnAddOne.TabIndex = ;
            this.btnAddOne.Text = "添加到清單==>";
            this.btnAddOne.UseVisualStyleBackColor = true;
            this.btnAddOne.Click += new System.EventHandler(this.btnAddOne_Click);
            // 
            // btnRemoveOne
            // 
            this.btnRemoveOne.Location = new System.Drawing.Point(, );
            this.btnRemoveOne.Name = "btnRemoveOne";
            this.btnRemoveOne.Size = new System.Drawing.Size(, );
            this.btnRemoveOne.TabIndex = ;
            this.btnRemoveOne.Text = "<==從清單移除";
            this.btnRemoveOne.UseVisualStyleBackColor = true;
            this.btnRemoveOne.Click += new System.EventHandler(this.btnRemoveOne_Click);
            // 
            // btnRemovAll
            // 
            this.btnRemovAll.Location = new System.Drawing.Point(, );
            this.btnRemovAll.Name = "btnRemovAll";
            this.btnRemovAll.Size = new System.Drawing.Size(, );
            this.btnRemovAll.TabIndex = ;
            this.btnRemovAll.Text = "<<<==全部移除";
            this.btnRemovAll.UseVisualStyleBackColor = true;
            this.btnRemovAll.Click += new System.EventHandler(this.btnRemovAll_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(F, F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(, );
            this.Controls.Add(this.btnRemovAll);
            this.Controls.Add(this.btnRemoveOne);
            this.Controls.Add(this.btnAddOne);
            this.Controls.Add(this.btnFresh);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.lstResults);
            this.Controls.Add(this.cmbHouXuan);
            this.Name = "Form1";
            this.Text = "ComboBox和ListBox執行個體";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout( );

        }

        #endregion

        private System.Windows.Forms.ComboBox cmbHouXuan;
        private System.Windows.Forms.ListBox lstResults;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Button btnFresh;
        private System.Windows.Forms.Button btnAddOne;
        private System.Windows.Forms.Button btnRemoveOne;
        private System.Windows.Forms.Button btnRemovAll;
    }
}