標籤:public 文字框
25.下拉式方塊控制項
組合框(comboBox)控制項是一個文字框和一個列表框的組合。
1.常用的屬性。
a.DropDownStyle,設定組合框的樣式,有三種可選值:
Simple:沒有下拉框,所以不能選擇,可以輸入,與Text控制項類似;
DropDown:具有下拉框,可以選擇和輸入;
DropDownList:具有下拉框,只能選擇,不能輸入。
b.MaxDropDownItems屬性,用於顯示清單項目的個數。
2.常用的事件。
a.Click和SelectedIndexChanged事件。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication10
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
switch (comboBox1.SelectedIndex)
{
case 0:
comboBox2.Items.Clear();
comboBox2.Items.Add( "北京" );
comboBox2.Items.Add( "上海" );
comboBox2.Items.Add( "武漢" );
comboBox2.SelectedIndex = 0;
break;
case 1:
comboBox2.Items.Clear();
comboBox2.Items.Add("華盛頓");
comboBox2.Items.Add("紐約");
comboBox2.Items.Add("芝加哥");
comboBox2.SelectedIndex = 0;
break;
case 2:
comboBox2.Items.Clear();
comboBox2.Items.Add("倫敦");
comboBox2.Items.Add("愛爾蘭");
comboBox2.Items.Add("考文垂");
comboBox2.SelectedIndex = 0;
break;
default:
comboBox2.Items.Clear();
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
string str = comboBox1.SelectedItem.ToString() + ":" + comboBox2.SelectedItem.ToString();
MessageBox.Show( str,"提示",MessageBoxButtons.OK,MessageBoxIcon.Information );
}
}
}
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6A/45/wKioL1UrezWzvM4pAADRWZC3jj0948.jpg" title=")N33]I`}(X3]~`E@SV8@SJH.png " alt="wKioL1UrezWzvM4pAADRWZC3jj0948.jpg" />
本文出自 “郭俊的部落格” 部落格,轉載請與作者聯絡!
C#自學之路25