简体   繁体   中英

Bind two lists to combobox, one only for suggestions

I'm creating a program for birthday notification and also editor to add celebrants into "database". Editor works with 3 comboboxes - one for day, month and year.

Current situation is that user can use numeric keyboard to set day and year, but in case of month he must write name of the month or select one item from the roll menu with mouse.

An ideal situation would be that user could use only tab (that is solved by TabIndex) and numeric keyboard - suggestion behind month combobox would be based on two (somehow connected) lists - months names (Jan, Feb, ...) AND months numbers (1, 2, ...) - while the only one (the named) would be visible - so that user would have an opportunity to write either "j" or "1" to select "January" item. Is that possible?

My current Combobox settings are:

  • AutoCompleteMode: SuggestAppend
  • AutoCompleteSource: ListItems
  • DropDownStyle: DropDownList
  • Items: Names of months (January - December)

Thanks in advance for any advice.

Personally, I don't think I would expect a listbox to respond to two lists. If I saw a list of months as text, I would not expect to be able to select them by number. But it's entirely possible that's just me :-)

Assuming you've already considered alternatives, such as a calendar control, you could try trapping the KeyDown event on the combobox something like this?

private string keysTyped = string.Empty;

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
  KeysConverter conv = new KeysConverter();

  this.keysTyped+=conv.ConvertToString(e.KeyCode).Replace("NumPad", "");

  int numTyped;
  while (int.TryParse(this.keysTyped, out numTyped))
  {
    if (numTyped <= 12)
    {
      this.comboBox1.SelectedIndex = numTyped - 1;
      break;
    }
    this.keysTyped = this.keysTyped.Substring(1);
  }
}

This seems to work with the minimal testing I've done, and I think it answers the question, but it does feel a little convoluted, which generally makes me question whether I'm trying to do something I shouldn't. But if it's what you need...

Note that the reason it's complicated is because it is designed so that it will accept two digits rather than just the one. There is more than one way to do this, but the above seems simplest. So if you type "1" then "2", it will select December.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM