【问题标题】:Is there any way to use autocomplete with multiple words inside the textbox in a winform?有没有办法在winform的文本框中使用多个单词的自动完成功能?
【发布时间】:2018-02-22 16:09:18
【问题描述】:

我想使用文本框的自动完成来显示建议的单词。例如,我输入 Tod……我的数据库中所有以“tod”开头的单词都会显示,但在我选择一个单词后,如果我输入另一个单词,例如“was”,下面将不会显示任何建议的单词。可能是因为字符串集合没有“today was”。我想显示仅以“was”而不是“today was”开头的建议词。我正在制作一个预测最常用词的项目。这是我的代码

public partial class Form1 : Form
{
    string[] arr = new string[] { "sn k", "sn k", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };



    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void txtInput_TextChanged(object sender, EventArgs e)
    {
        predict();
    }



    void predict()
    {

        txtInput.AutoCompleteMode = AutoCompleteMode.Suggest;
        txtInput.AutoCompleteSource = AutoCompleteSource.CustomSource;
        AutoCompleteStringCollection col = new AutoCompleteStringCollection();

        col.AddRange(arr);
        txtInput.AutoCompleteCustomSource = col;
    }
}

}

【问题讨论】:

  • AutoComplete 非常愚蠢,并且没有(m)任何扩展点。如果您需要的东西没有提供,您现在已经推出了自己的自动完成功能,但这太宽泛了,无法在这里回答。
  • 你的答案是正确的。您真的不必在每次更改 TextBox 文本时都重新创建 AutoCompleteCustomSource。你甚至不需要AutoCompleteStringCollectionAutoCompleteCustomSource 已经是该类型,因此您执行了两次相同的操作。你可以:txtInput.AutoCompleteCustomSource.AddRange(arr);
  • 我的问题是,当我输入第二个单词时,它不再提示单词了。例如,我输入“J”,文本框下方会建议一些选项,例如“一月”“七月”等。所以当我选择一月时,它会在文本框内加起来,但当我输入第二个单词“是”时,将没有显示建议的单词。可能是因为字符串集合中没有“January was”。如果可能的话,我想对在文本框中输入的多个单词使用自动完成功能。

标签: c# .net winforms autocomplete textbox


【解决方案1】:

所以一个基本的“滚动你自己”的解决方案将像这样工作:

string[] arr = new string[] { "sn k", "sn k", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };

private ListBox autoListBox;

private void Form1_Load(object sender, EventArgs e)
{
    this.txtInput.TextChanged += txtInput_TextChanged;
    CreateAutocompleteListBox();
}

private void txtInput_TextChanged(object sender, EventArgs e)
{
    showAutoCompleteList();
}

private void CreateAutocompleteListBox()
{
    this.autoListBox = new ListBox()
    {
        Left = this.txtInput.Left,
        Top = this.txtInput.Top + this.txtInput.Height,
        Width = 100,
        Height = 75
    };

    this.autoListBox.Click += autoListBox_Click;
    this.autoListBox.KeyDown += autoListBox_KeyDown;
    this.autoListBox.Visible = false;
    this.Controls.Add(this.autoListBox);
}

private void autoListBox_Click(object sender, EventArgs e)
{
    AutocompleteFinished();
}

private void autoListBox_KeyDown(object sender, KeyEventArgs e)
{
    var finishCodes = new List<Keys> { Keys.Return, Keys.Space };
    if (finishCodes.Contains(e.KeyCode))
    {
        AutocompleteFinished();
    }
}

private string GetLastWord(TextBox txt)
{
    return (" " + txt.Text).Split(' ').LastOrDefault() ?? "";
}

private void showAutoCompleteList()
{
    this.autoListBox.Left = this.txtInput.Left;
    this.autoListBox.Top = this.txtInput.Top + this.txtInput.Height + 2;
    var lastWord = GetLastWord(this.txtInput);

    this.autoListBox.Items.Clear();
    this.autoListBox.Items.AddRange(this.arr.Where(aw => aw.ToLower().StartsWith(lastWord.ToLower())).ToArray());
    this.autoListBox.Visible = true;
}

private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
    // These keys show auto-complete selector
    var activatorCodes = new List<Keys> { Keys.Up, Keys.Down };
    if (activatorCodes.Contains(e.KeyCode))
    {
        SwitchToAutoCompleteList();
    }
}

private void SwitchToAutoCompleteList()
{
    this.autoListBox.Focus();
    this.autoListBox.SelectedIndex = 0;
}

private void AutocompleteFinished()
{
    var lastWord = GetLastWord(this.txtInput);
    var nextWord = this.autoListBox.Text;
    this.txtInput.Text = this.txtInput.Text.Substring(0, this.txtInput.Text.Length - lastWord.Length);
    this.txtInput.AppendText(nextWord);
    this.autoListBox.Visible = false;
}

这应该做你想做的,它会为每个单词显示自动完成!可以添加一些技巧,例如随着文本的移动移动自动完成框。

【讨论】:

  • 它也适用于我的。我想要做的是对多个单词使用自动完成。例如,如果我输入“Jan”,我将在下面的建议单词中附加一月,然后当我输入“was”时,会出现另一个仅以“was”开头的建议单词。
  • 嗯..我不知道这是否可行..一旦您接受第一个单词,您就会得到建议..然后它将重置自动完成行为。
  • 我正在用 c# 制作一个项目,该项目仅通过用户输入的几个第一个字符来预测单词。就像移动设备中的自动预测的工作原理一样。
  • 嘿@Kat,我已经用一个足够简单的自动完成更新了答案,可以满足你的要求。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-14
  • 1970-01-01
  • 2023-03-16
  • 2022-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多