简体   繁体   English

自定义控件-如何在组合框上绘制文本框

[英]Custom Control - How to Draw Textbox on combobox

I'm developing Win Form Application In my UI, I have to make ComboBox which should be dropdown only at first but after selecting any item, it should be partially editable. 我正在开发Win Form Application。在我的UI中,我必须制作ComboBox ,该ComboBox仅应首先下拉,但是在选择任何项目之后,它应该可以部分编辑。

EG Options are like, EG选项就像

Item Value - 10 物品价值-10

Item Value - 20 物品价值-20

Item Value - 30 etc. 物品价值-30等

Now, if Item Value - 20 is selected, number 20 should be editable (20-29 of course) 现在,如果选择了“ Item Value - 20 ,则数字20应该是可编辑的(当然是20-29)

But here editing should be allowed only to change numeric value, not Text part. 但是这里只允许更改数值,而不能更改文本部分。 and also only numeric part should be selected to make it more user friendly. 并且应该只选择数字部分以使其更加用户友好。

If there may some internal properties in ComboBox to do so (which I guess will not be the case), it will be straight forward way. 如果在ComboBox中可能有一些内部属性可以做到这一点(我想并非如此),那将是直接的方法。

Else to do so, I was thinking of having a TextBox drawn/placed on ComboCox accurately. 否则,我正在考虑将TextBox准确地绘制/放置在ComboCox

In this approach I'm not clear how to put TextBox so accurately that my custom user control looks like single unit and it don't overlap "text" part of combobox ? 在这种方法中,我不清楚如何精确地放置TextBox以使我的自定义用户控件看起来像单个单元,并且不与combobox的“ text”部分重叠

I don't think the combo box control was intended to be used in this way as a simple validated text box seems to be a better control for you and the user if they must enter data. 我不认为组合框控件应以这种方式使用,因为如果您和用户必须输入数据,则简单的经过验证的文本框似乎对您和用户来说是更好的控件。 Granted, you could simply add all the numbers you need into the combo box, but the user would have to scroll to the number they wanted. 当然,您可以将所需的所有数字简单地添加到组合框中,但是用户必须滚动到所需的数字。 If this list is large then it won't be very user friendly. 如果此列表很大,那么它将不是非常用户友好的。 The problem with the user typing something into the combo box is a matter of what to do when the user finishes typing. 用户在组合框中键入内容的问题与用户完成键入时的操作有关。 Even with suggestions on, when the user finishes typing something into the combo box, nothing happens until the user presses the Enter key. 即使有建议,当用户完成在组合框中键入内容时,也不会发生任何事情,直到用户按下Enter键。

If the user typed something that matches one of the items in the list then presses the Enter key, the combo boxes SelectedIndexChanged event will get fired. 如果用户键入的内容与列表中的一项相匹配,然后按Enter键,则会触发组合框SelectedIndexChanged事件。 However, if the user types something that is NOT currently in the items list and presses the Enter key the SelectedIndexChanged event will NOT get fired. 但是,如果用户键入的项目列表中当前未包含的内容Enter键,则不会触发SelectedIndexChanged事件。

Using the combo box KeyDown event, you could capture the “EnterKey” when the user presses the enter key in the combo box, as this is something the user would do anyway to select an existing item. 使用组合框KeyDown事件,您可以在用户按下组合框的Enter键时捕获“ EnterKey”,这是用户无论如何都会选择现有项的操作。 So when the user types something that already exist in the list and presses the Enter key BOTH SelectedIndexChanged AND KeyDown events get fired in that order. 因此,当用户键入列表中已经存在的内容并按Enter键时, KeyDown顺序将同时激发SelectedIndexChangedKeyDown事件。 If the user types something that is NOT in the items list, then only the KeyDown event gets fired. 如果用户键入的项目列表中未包含某些内容,则只会触发KeyDown事件。

Using the KeyDown event a check is made on what the selected value is. 使用KeyDown事件检查所选值是什么。 If the item typed by the user is in the items list then we can ignore these as they were handled in the previous call to SelectedIndexChanged . 如果用户键入的项目在项目列表中,那么我们可以忽略它们,因为它们在上一次对SelectedIndexChanged调用中已得到处理。 When the user typed something new, obviously you would possibly need to check the range of the values then simply call your method with the users input. 当用户键入新内容时,显然您可能需要检查值的范围,然后仅使用用户输入来调用您的方法。

Again this seems hacky and IMHO, a validated text box would be easier on you and simple for the user especially if they have to type it anyway. 再次,这似乎很麻烦,而且恕我直言,经过验证的文本框将使您更容易使用,并且对用户来说也很简单,特别是如果他们仍然必须键入它。

List<string> comboData;
public Form1() {
  InitializeComponent();
  comboData = new List<string>();
  for (int i = 1; i < 100; i++) {
    comboData.Add(i.ToString());
  }
  comboBox1.DataSource = comboData;
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
  MessageBox.Show("selection changed  " + comboBox1.Text);
}
private void comboBox1_KeyDown(object sender, KeyEventArgs e) {
  if (e.KeyCode == Keys.Enter) {
    if (comboData.Contains(comboBox1.Text.ToString())) {
      MessageBox.Show("User entered existing data: " + comboBox1.Text);
      // selection changed event has already handled this, however
      // if the user just pressed "enter" previously
      // then selection changed event wont get fired because the selection did not change
    }
    else {
      MessageBox.Show("User entered NEW data: " + comboBox1.Text);
      // we have data that is NOT currently in the list
      // so selection changed WONT get fired
      // need to call your method with user typed value
    }
  }
}
// make user user only enters numbers
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e) {
  if (!Char.IsNumber(e.KeyChar)) {
    e.Handled = true;
  }
}

Note: I cannot tell what exactly what your combo items list contains. 注意:我无法确定您的组合项目列表包含的内容。 If there is text in the items like “Item Value –”... this seems unnecessary. 如果项目中有文本,例如“项目值–”,这似乎是不必要的。 In the code above I simply used the needed info ie.. the numbers in the combo box items list. 在上面的代码中,我只是使用了所需的信息,即组合框项目列表中的数字。 Also I used a key press event to filter out unwanted alpha characters. 另外,我还使用按键事件来过滤掉不需要的字母字符。

Hope this helps. 希望这可以帮助。

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

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