主要代码如下:
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 = 0; i < 10; i++) //随机生成10个新的数据
{
string item = string.Format("Item-{0:X8}", rd.Next( ));
this.cmbHouXuan.Items.Add(item); //添加到ComboBox中
}
this.cmbHouXuan.SelectedIndex = 0; //默认选中第一条
}
//重新生成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 >= 0) //如果当前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(19, 44);
this.cmbHouXuan.Name = "cmbHouXuan";
this.cmbHouXuan.Size = new System.Drawing.Size(159, 20);
this.cmbHouXuan.TabIndex = 0;
//
// lstResults
//
this.lstResults.FormattingEnabled = true;
this.lstResults.ItemHeight = 12;
this.lstResults.Location = new System.Drawing.Point(217, 12);
this.lstResults.Name = "lstResults";
this.lstResults.Size = new System.Drawing.Size(222, 352);
this.lstResults.TabIndex = 1;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(22, 27);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(41, 12);
this.label1.TabIndex = 2;
this.label1.Text = "候选项";
//
// btnFresh
//
this.btnFresh.Location = new System.Drawing.Point(40, 70);
this.btnFresh.Name = "btnFresh";
this.btnFresh.Size = new System.Drawing.Size(113, 35);
this.btnFresh.TabIndex = 3;
this.btnFresh.Text = "刷新候选项";
this.btnFresh.UseVisualStyleBackColor = true;
this.btnFresh.Click += new System.EventHandler(this.btnFresh_Click);
//
// btnAddOne
//
this.btnAddOne.Location = new System.Drawing.Point(40, 166);
this.btnAddOne.Name = "btnAddOne";
this.btnAddOne.Size = new System.Drawing.Size(113, 35);
this.btnAddOne.TabIndex = 3;
this.btnAddOne.Text = "添加到列表==>";
this.btnAddOne.UseVisualStyleBackColor = true;
this.btnAddOne.Click += new System.EventHandler(this.btnAddOne_Click);
//
// btnRemoveOne
//
this.btnRemoveOne.Location = new System.Drawing.Point(40, 229);
this.btnRemoveOne.Name = "btnRemoveOne";
this.btnRemoveOne.Size = new System.Drawing.Size(113, 35);
this.btnRemoveOne.TabIndex = 3;
this.btnRemoveOne.Text = "<==从列表移除";
this.btnRemoveOne.UseVisualStyleBackColor = true;
this.btnRemoveOne.Click += new System.EventHandler(this.btnRemoveOne_Click);
//
// btnRemovAll
//
this.btnRemovAll.Location = new System.Drawing.Point(40, 301);
this.btnRemovAll.Name = "btnRemovAll";
this.btnRemovAll.Size = new System.Drawing.Size(113, 35);
this.btnRemovAll.TabIndex = 3;
this.btnRemovAll.Text = "<<<==全部移除";
this.btnRemovAll.UseVisualStyleBackColor = true;
this.btnRemovAll.Click += new System.EventHandler(this.btnRemovAll_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(468, 382);
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;
}
}