保持ID和值同步使用ComboBox WinForms C# [英] Keeping ID and Value in sync using ComboBox WinForms C#

查看:201
本文介绍了保持ID和值同步使用ComboBox WinForms C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 ComboBox 保存数据。通常我创建2 ComboBox 一个与真实数据。我需要id知道它在数据库中的id。然后我使用SelectedIndex和事件的两个复选框,以保持它们同步。有时我甚至使用更多的组合框,并保持它们所有的同步使用SelectedIndex和大量的事件。我有一种感觉有更好的方法。

  private void czynnoscInstrumentyFinansoweComboID_SelectedIndexChanged(object sender,EventArgs e){
czynnoscInstrumentyFinansoweCombo。 SelectedIndex = czynnoscInstrumentyFinansoweComboID.SelectedIndex;
}

另一件我想要的是autocomplete / search within ComboBox 。当用户使用combobox并开始在combobox里面打字时,它只是对第一个字母作出反应,所以当你输入'start'时,它将跳过列表 s 然后到 t 然后到 a 等等。当我想它尝试并找到短语开始的东西。



如何实现这两者?



编辑:



我不使用DataSets,我添加了像这样的值:

  //获取sql数据,并把它放入字符串/将它添加到comboBox 
string var =sds;
czynnoscInstrumentyFinansoweComboID.Add(var);好的,如果我正确理解你的问题,那么我' d说你的感觉是正确的。您不必使用多个组合来跟踪数据/值(即您的情况下的标识符)。



通常,我们需要分配一个数据源到组合框,以便当您将获得所选的索引更改事件时,它将为您提供文本以及附加的ID。将DisplayMember设置为包含文本的数据集列,并将ValueMember属性设置为包含ID的数据集列。

  DataSet yourDataSource = SomeGetDataSourceMethod(); //准备好你的数据源。 

this.cbxLookup.DataSource = yourDataSource;
this.cbxLookup.DisplayMember =EmployeeName;
this.cbxLookup.ValueMember =EmployeeID;

在SelectedIndexChanged事件中,您可以获取SelectedItem,SelectedIndex,SelectedValue。



或者,您也可以执行以下操作:

  int value = 1; 
cbxLookup.Items.Add(new ListViewItem(Your Name,value));

- EDIT 2 -
定义结构

  class KeyValueData 
{
public KeyValueData(string Text)
{
text = Text;
itemData = 0;
}

public KeyValueData(string Text,int ItemData)
{
text = Text;
itemData = ItemData;
}

public int ItemData
{
get
{
return itemData;
}
set
{
itemData = value;
}
}

public override string ToString()
{
return text;
}

protected string text;
protected int itemData;
}

//然后添加到combo中,如下所示:

  comboBox1.Items.Add(new KeyValueData(New Yorkers,21)); 

- 编辑1 -



对于第二部分,假设您使用.NET 2.0,您可以获取KeyPress事件并使用 ComboBox.FindString 方法。 此示例可能有帮助。


I'm using ComboBox to keep data in. Usually i create 2 ComboBox one hidden to keep ID and one with the real data. I need id to know which id it is in database. I then use SelectedIndex and event for both checkboxes to keep them in sync. Sometimes I even use more comboboxes and keep them all in sync with each other using SelectedIndex and lots of events. I have a feeling there's a better way.

    private void czynnoscInstrumentyFinansoweComboID_SelectedIndexChanged(object sender, EventArgs e) {
        czynnoscInstrumentyFinansoweCombo.SelectedIndex = czynnoscInstrumentyFinansoweComboID.SelectedIndex;
    }

Also another thing that I would like to have is autocomplete/search inside ComboBox. When user uses combobox and starts typing inside combobox it just reacts on first letter so when you type 'start' it will jump thru the list to s then to t then to a etc. when i would like it to try and find the phrase "Start of something".

How do I achieve both of those?

EDIT:

I am not using DataSets, and i am adding values like that:

  // get sql data and put it into strings/decimals then add it like that to comboBox
  string var = "sds";
  czynnoscInstrumentyFinansoweComboID.Add(var);

解决方案

Well, if I understand your question correctly then I'd say your feeling is correct. You dont have to use multiple combos to keep track of data/value(that is, the identifier in your case).

Usually to do that we need to assign a datasource to the combo box, so that when you would get the selected index changed event, it would provide you with text as well as attached id. Set DisplayMember to your dataset column that contains text, and set ValueMember property to your dataset column that contains the ID.

DataSet yourDataSource = SomeGetDataSourceMethod(); //get your data source ready.

this.cbxLookup.DataSource = yourDataSource;
this.cbxLookup.DisplayMember ="EmployeeName";
this.cbxLookup.ValueMember = "EmployeeID";

Upon SelectedIndexChanged event, you can get SelectedItem, SelectedIndex, SelectedValue.

Alternatively you can do following as well:

int value=1;
cbxLookup.Items.Add(new ListViewItem("Your Name", value));

--EDIT 2-- Define a structure like following:

  class KeyValueData
  {
      public KeyValueData(string Text)
      {
          text = Text;
          itemData = 0;
      }

      public KeyValueData(string Text, int ItemData)
      {
          text = Text;
          itemData = ItemData;
      }

      public int ItemData
      {
          get
          {
              return itemData;
          }
          set
          {
              itemData = value;
          }
      }

      public override string ToString()
      {
          return text;
      }

      protected string text;
      protected int itemData;
  }

//and then add into combo like following:

comboBox1.Items.Add(new KeyValueData("New Yorkers", 21));

--EDIT 1--

For your second part, assuming that you are using .NET 2.0, you can get the KeyPress event and use ComboBox.FindString method. This example might help.

这篇关于保持ID和值同步使用ComboBox WinForms C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆