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

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

问题描述

我用组合框来保持数据。通常我创建2 组合框一个隐藏保持ID和一个具有真实数据。我需要的id知道它是在数据库,ID。然后,我用的SelectedIndex和事件两个复选框保持同步。有时我也会使用更多的组合框,并保持他们都在彼此同步使用的SelectedIndex和大量的事件。我有一种感觉,有一种更好的方式。

 私人无效czynnoscInstrumentyFinansoweComboID_SelectedIndexChanged(对象发件人,EventArgs五){
czynnoscInstrumentyFinansoweCombo。的SelectedIndex = czynnoscInstrumentyFinansoweComboID.SelectedIndex;
}



另外,我想有另一件事情是在<$自动完成/搜索C $ C>组合框。当用户使用组合框,并开始内部组合框,它只是反应的第一个字母打字,所以当你键入'开始',它会跳直通列表,取值再到 T 再到 A 等时,我想它,试图找到那句的东西开始。



我如何实现这两个的



编辑:



我我没有使用的数据集,我增加值这样的:

  //获取SQL数据,并把它转换成字符串/小数则这样将它添加到组合框
串VAR =SDS;
czynnoscInstrumentyFinansoweComboID.Add(VAR);


解决方案

好吧,如果我正确地理解你的问题,然后我得说你的感觉是正确的。你不必使用多个组合来跟踪数据/值(也就是,你的情况标识符)的。



通常要做到这一点,我们需要分配一个数据源到组合框,这样,当你将得到选定的索引更改事件,它会为你提供的文本,以及附加的ID。设置的DisplayMember包含文本数据集列和ValueMember属性设置为包含ID数据集列。

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

this.cbxLookup.DataSource = yourDataSource;
this.cbxLookup.DisplayMember =EmployeeName;
this.cbxLookup.ValueMember =雇员;



一旦SelectedIndexChanged事件,你可以得到的SelectedItem,的SelectedIndex,SelectedValue的。



另外,你可以做以下还有:

  int值= 1; 
cbxLookup.Items.Add(新的ListViewItem(你的名字,值));



- 编辑2 -
定义如结构以下内容:

 类KeyValueData 
{
公共KeyValueData(字符串文本)
{
文本=文本;
的ItemData = 0;
}

公共KeyValueData(字符串文本,诠释的ItemData)
{
文本=文本;
的ItemData =的ItemData;
}

公众诠释的ItemData
{
得到
{
返回的ItemData;
}

{
的ItemData =价值;
}
}

公共重写字符串的ToString()
{
返回文本;
}

保护字符串文本;
保护INT的ItemData;
}



//然后添加到组合类似以下内容:

  comboBox1.Items.Add(新KeyValueData(纽约人,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.

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

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