列表框选中的项目winform

时间:2012-03-13 05:34:36

标签: winforms

Listbox.SelectedItem = value对我不起作用。其实我想以编程方式更改选择。我没有要选择列表框的索引,只有文本。实际上,显示构件由组合框提供。当组合框中的选择发生变化时,结果更改也将反映在列表框中。

      void PopulateList()
    {
        this.list.DataSource = (IList)ClassDsnManager.GetDataSourceNames();
        this.list.DisplayMember = "Name";
        this.list.ValueMember = "Driver";

    }
 void ComboSelectedIndexChanged(object sender, EventArgs e)
      {
        if (Combo.SelectedIndex != -1)
        {
            ClassDatabase selecteditem = (ClassDatabase)Combo.SelectedItem;

            source.Text = selecteditem.source;

            string destination= selecteditem.SqlConn;
            if (!string.IsNullOrEmpty(destination))
            {
                string[] connectionValue = connection.Split(Convert.ToChar(";"));
                string dsnName = connectionValue[0].Substring(4, connectionValue[0].Length - 4);
                // this is the list box whose value i want to set
                lbDSN.SelectedItem = dsnName;

            }

        }

    }

1 个答案:

答案 0 :(得分:2)

解决方案1:SelectedIndexChanged Combobox中只需编写

listBox1.SelectedItem = comboBox1.SelectedItem.ToString();

解决方案2:DropDownStyle的{​​strong> Combobox 更改为Simple。它会将Combobox显示为Listbox