views:

650

answers:

3

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);
+1  A: 

Dzien Dobry,

First.... Why dont you put an OBJECT into the combobox that has the string and the ID value? There is always Selecteditem to get it (not SelectedIndex)... saves you a second combobox.

Second... get proper tools. Infragistics, Devexpress - the Microsoft internal UI elements are SERIOUSLY limited in Winforms. Gets better with WPF, but Windowms it just is really primitive. And it wont change - MS relies on the ecosystem to provide proper tooling here.

I never touch any of the Winform integrated controls - totally on Infragistics, and that for a reason.

TomTom
If i could afford having Infragistics i would buy them. I was playing with them for a bit and they are great but they cost money which my company won't spend. Not now at least. So I need some harder / free ways to do it.
MadBoy
Also what about 2nd part of my question?
MadBoy
@TomTom While I think the whole idea of using multiple ComboBoxes here is poor design, I like your suggestion. One suggestion, if you create a custom object to put in the Items collection of a ComboBox : you'll probably want to override 'ToString so that a "satisfying" string representation appears at run-time in the ComboBox. In general I think suggesting questioners buy 3rd. party tools is a waste of time on SO, but I agree with you that many of the inherent controls of WinForms are pretty poor, and I use one suite of 3rd. party controls myself.
BillW
+2  A: 

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.

KMan
I'm not using datasets, and i am not sure how to prepare one from strings / decimals etc that i have (I am using old way of getting data from database) ;) Your alternative method seems easier to implement with my current setup.
MadBoy
What about 2nd part of my question?
MadBoy
@KMan I think you meant to use 'ListViewItem, not 'ListItem ?
BillW
@BillW: Yep, thanks for the correction.
KMan
@MadBoy: Please see my edits. Btw, its always better to ask each question in a separate thread; that helps the helpers as well as those looking for help (0:
KMan
@BillW: I have added an example for custom data type as well (0: see my edit number 2.
KMan
Thanks, i guess i'll follow the 1 question per thread then ;)
MadBoy
@KMan +1 For a good solid answer, with an over-ride for ToString(). Note that this could be done also using a KeyValuePair<string,int> object or similar: the trick there would be you would need to implement code in the ComboBox's 'Format event to "massage" the run-time representation like so : e.Value = ((KeyValuePair<string, int>) e.ListItem).Key; I think one reason I've avoided using ComboBoxes is the fact their Items collection is made of objects, and you're going have to get into casting the object back into usable form at some point (unless you are dealing with a databound scenario).
BillW
A: 

Dzien Dobry rowniez:) I'm not sure, but are you missing the fact that you can easily keep both id and value in combobox?

And total offtopic, but your naming convention for controls just provoked big eruption of laugh here, a moment ago. Thank you for that.

StupidDeveloper
Unfortunately for me I've got a lot of code like that. I've assumed by reading a book when i was learning that it should be descriptive so i would know from code what it is :-) Now I'm stuck with very long names but it makes no sense to change code now. At least I do know what each variable / combobox stands for and i can easly find it from within code.And yes i was missing the fact that i can keep both id and value in one combobox. Seems like KMan answer provided me how to. If you can add something to that feel free ;-)
MadBoy