简体   繁体   English

将两个列表绑定到组合框,一个仅用于建议

[英]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. 编辑器可使用3个组合框-一个组合用于日,月和年。

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. 理想的情况是用户只能使用制表符(由TabIndex解决)和数字键盘-月组合框后面的建议将基于两个(以某种方式连接)列表-月名称(1月,2月,...)和月数字(1、2,...)-唯一的一个(已命名)是可见的-这样用户将有机会编写“ j”或“ 1”以选择“一月”项目。 Is that possible? 那可能吗?

My current Combobox settings are: 我当前的组合框设置为:

  • AutoCompleteMode: SuggestAppend AutoCompleteMode:SuggestAppend
  • AutoCompleteSource: ListItems AutoCompleteSource:ListItems
  • DropDownStyle: DropDownList 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? 假设您已经考虑过其他方法,例如日历控件,则可以尝试在组合框中捕获KeyDown事件,如下所示?

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. 因此,如果您输入“ 1”然后输入“ 2”,它将选择December。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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