简体   繁体   English

如何使winforms文本框自动完成正确的大写?

[英]How to make winforms textbox autocomplete correct capitalisation?

Using a winforms textbox with autocomplete set to SuggestAppend I can type out part of a string and the rest will be suggested to me fine.使用自动完成设置为 SuggestAppend 的 winforms 文本框,我可以输入字符串的一部分,其余部分将被建议给我。

If a user types "smi" looking for "Smith, John" and then autocompletes the rest of the string by tabbing then the textbox contains "smith, John".如果用户键入“smi”以查找“Smith, John”,然后通过 Tab 键自动完成字符串的其余部分,则文本框包含“smith, John”。 But, if the user clicks on the name then the capitalisation is correct.但是,如果用户单击名称,则大写是正确的。

Is there a way I can get the autocomplete to re-capitalise the user inputted part of the string when the suggestion is accepted by tabbing?有没有办法让自动完成在Tabbing接受建议时重新计算字符串输入的部分?

在此处输入图像描述

Pressing tab leads to:按 T​​ab 会导致:

在此处输入图像描述

Clicking name leads to (this is what I want):单击名称会导致(这就是我想要的):

在此处输入图像描述

To handle this situation I handled the textbox Leave event.为了处理这种情况,我处理了文本框 Leave 事件。 The idea is to split the text by comma, uppercase the first letter of the resulting strings, then join the strings back together.这个想法是用逗号分割文本,将结果字符串的第一个字母大写,然后将字符串重新连接在一起。

private void textBox1_Leave(object sender, EventArgs e)
{
  string[] strings = this.textBox1.Text.Split(new char[] { ',' });

  for (int i = 0; i < strings.Length; i++)
  {
    strings[i] = string.Format("{0}{1}", char.ToUpper(strings[i][0]), strings[i].Substring(1));
  }

  this.textBox1.Text = string.Join(",", strings);
}

Here's the function I came up with the end, it replaces the textbox's content with a line from the AutoCompleteCustomSource of the textbox (sorted alphabetically).这是我最终提出的功能,它将文本框的内容替换为文本框的 AutoCompleteCustomSource 中的一行(按字母顺序排序)。

So, this will still work for any case (eg if user entered "aLLeN" it would still correct to "Allen,Charlie (ID:104)"因此,这仍然适用于任何情况(例如,如果用户输入“aLLeN”,它仍然会更正为“Allen,Charlie (ID:104)”

private void fixContent()
{
    String text = txtAutoComplete.Text;
    List<String> matchedResults = new List<String>();

    //Iterate through textbox autocompletecustomsource
    foreach (String ACLine in txtAutoComplete.AutoCompleteCustomSource)
    {
        //Check ACLine length is longer than text length or substring will raise exception
        if (ACLine.Length >= text.Length)
        {
            //If the part of the ACLine with the same length as text is the same as text, it's a possible match
            if (ACLine.Substring(0, text.Length).ToLower() == text.ToLower())
                matchedResults.Add(ACLine);
        }
    }

    //Sort results and set text to first result
    matchedResults.Sort();
    txtAutoComplete.Text = matchedResults[0] 
}

Thanks to OhBeWise I attached this to the textbox leave event:感谢 OhBeWise,我将此附加到文本框离开事件:

private void txtAutoComplete_Leave(object sender, EventArgs e)
{
    fixContent();
}

But also I needed to cover situations when the autocomplete has been accepted which occur when enter, tab, left and right are pressed.但我还需要涵盖在按下 enter、tab、left 和 right 时发生的自动完成被接受的情况。 Attaching this to the keydown event doesn't work because I think the autocomplete captures the event beforehand, so I attached to the previewkeydown event:将此附加到 keydown 事件不起作用,因为我认为自动完成会事先捕获事件,所以我附加到 previewkeydown 事件:

private void txtAutoComplete_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
    Keys key = (Keys)e.KeyCode;

    if (key == Keys.Enter || key == Keys.Tab || key == Keys.Left || key == Keys.Right)
    {
        fixContent();
    }
}
simple ;
 private void textbox_TextChanged(object sender, EventArgs e)
    {
        AutoCompleteStringCollection a = new AutoCompleteStringCollection();
        a = textbox.AutoCompleteCustomSource;

        for (int i = 0; i < a.Count; i++)
        {
            if (a[i].ToLower() == textbox.Text.ToLower())
            {
                textbox.Text= a[i].ToString();
                break;
            }
        }
    }

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

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