?
屬性:
DropDownStyle設置為DropDown則下拉框中的內容可以被編輯,設置為DropDownList則下拉框中的內容為只讀形式。
Items的Add屬性可向下拉框中添加內容。
SelectedIndex屬性為下拉框中內容的索引,可以通過其設置默認顯示的Items中的內容。
事件:
SelectedIndexChanged為改變選擇內容后要執行的事件。
如下:通過添加、清除選項和改變選項記錄次數為例進行演示。
namespace _003_下拉框
{
public partial class 下拉框 : Form
{
public 下拉框()
{
InitializeComponent();
}
int selCount = 0;
private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
this.lblChangeCount.Text = selCount++.ToString();
}
private void 下拉框_Load(object sender, EventArgs e)
{
this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
this.comboBox.Items.Add("--請選擇--");
this.comboBox.Items.Add("商品1");
this.comboBox.Items.Add("商品2");
this.comboBox.SelectedIndex = 0;
}
private void btAdd_Click(object sender, EventArgs e)
{
this.comboBox.Items.Add("商品3");
}
private void btClear_Click(object sender, EventArgs e)
{
this.comboBox.Items.Clear();
}
}
}
該文章在 2025/4/15 9:40:37 編輯過