如何使 winforms 文本框自动完成正确的大小写?
How to make winforms textbox autocomplete correct capitalisation?
使用自动完成设置为 SuggestAppend 的 winforms 文本框,我可以输入字符串的一部分,其余部分会很好地建议给我。
如果用户键入 "smi" 查找 "Smith, John",然后通过 Tab 键自动完成字符串的其余部分,则文本框包含 "smith, John"。但是,如果用户单击名称,则大写是正确的。
有什么方法可以让我在通过 Tab 键接受建议时自动完成 re-capitalise 用户输入的部分字符串?
按 Tab 键导致:
单击名称会导致(这就是我想要的):
为了处理这种情况,我处理了文本框离开事件。这个想法是用逗号分割文本,将结果字符串的第一个字母大写,然后将字符串重新连接在一起。
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);
}
这是我最后想到的函数,它将文本框的内容替换为文本框的 AutoCompleteCustomSource 中的一行(按字母顺序排序)。
因此,这仍然适用于任何情况(例如,如果用户输入 "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]
}
感谢 OhBeWise,我将其附加到文本框离开事件中:
private void txtAutoComplete_Leave(object sender, EventArgs e)
{
fixContent();
}
但我还需要涵盖当按下回车键、制表符、左右键时发生的自动完成已被接受的情况。将此附加到 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;
}
}
}
使用自动完成设置为 SuggestAppend 的 winforms 文本框,我可以输入字符串的一部分,其余部分会很好地建议给我。
如果用户键入 "smi" 查找 "Smith, John",然后通过 Tab 键自动完成字符串的其余部分,则文本框包含 "smith, John"。但是,如果用户单击名称,则大写是正确的。
有什么方法可以让我在通过 Tab 键接受建议时自动完成 re-capitalise 用户输入的部分字符串?
按 Tab 键导致:
单击名称会导致(这就是我想要的):
为了处理这种情况,我处理了文本框离开事件。这个想法是用逗号分割文本,将结果字符串的第一个字母大写,然后将字符串重新连接在一起。
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);
}
这是我最后想到的函数,它将文本框的内容替换为文本框的 AutoCompleteCustomSource 中的一行(按字母顺序排序)。
因此,这仍然适用于任何情况(例如,如果用户输入 "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]
}
感谢 OhBeWise,我将其附加到文本框离开事件中:
private void txtAutoComplete_Leave(object sender, EventArgs e)
{
fixContent();
}
但我还需要涵盖当按下回车键、制表符、左右键时发生的自动完成已被接受的情况。将此附加到 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;
}
}
}