页面树结构
转至元数据结尾
转至元数据起始

要在组合框中一个特定项目被选中时填充C1ComboBox,按如下步骤使用SelectedItemChanged事件:

  1. 在窗体上添加两个C1ComboBoxe控件。
  2. 使用String Collection Editor 在第一个C1ComboBox控件中添加字符串 "Pittsburgh"。
  3. 在C1ComboBox的Properties窗口中双击SelectedItemChanged事件来创建SelectedItemChanged事件的事件处理程序。
  4. 在SelectedIndexChanged事件处理程序中添加如下代码:

Visual Basic

Visual Basic

Private Sub comboBox1_SelectedItemChanged(sender As Object, e As EventArgs) c1ComboBox2.Items.Clear()
If c1ComboBox1.SelectedItem.ToString() = "Pittsburgh" Then c1ComboBox2.Items.Add("Pittsburgh is known as the Steel City and the City of Bridges.") c1ComboBox2.Items.Add("Pittsburgh has 446 Bridges")
Else
c1ComboBox2.Items.Add("You did not select Pittsburgh.") End If
End Sub

C#

C#

private void comboBox1_SelectedItemChanged(object sender, EventArgs e)
{
c1ComboBox2.Items.Clear(); if (c1ComboBox1.SelectedItem.ToString() == "Pittsburgh")
{
c1ComboBox2.Items.Add("Pittsburgh is known as the Steel City and the City of Bridges."); c1ComboBox2.Items.Add("Pittsburgh has 446 Bridges");
} else
{
c1ComboBox2.Items.Add("You did not select Pittsburgh.");

}
}

  1. 运行程序然后再第一个C1ComboBox中选择Pittsburgh。
  2. 点击第二个C1ComboBox的下拉按钮。注意到按照SelectedItemChanged事件处理程序项目已经被添加到下拉列表中。

  • 无标签