winforms 有没有办法在winform的文本框中使用多个单词的自动完成功能?

vd2z7a6w  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(95)

我想显示建议的话使用自动完成的文本框。例如,我键入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;
    }
}

}

ua4mk5z4

ua4mk5z41#

所以一个基本的“Roll Your Own”解决方案是这样工作的:

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;
}

这应该做你想要的,它将显示自动完成每个字!可以添加一些技巧,比如随着文本的移动移动自动完成框。

gstyhher

gstyhher2#

最简单的解决方案是实现一个基于Textbox的自定义控件,其中包含Textbox控件和列表框控件。使用文本更改事件用匹配用户选择的字符串填充列表框。重点仍然是控制不包含在应用程序形式的上下文中的控制。在内部,定义的用户控件必须处理来自包含的文本框和列表控件的任何事件。仅当自动完成列表包含多个匹配项时(根据规则),才显示列表框。
如果只存在一个匹配项,则(通常)在文本框中显示“完成”文本,其中模式为“追加”。如果建议不以“从...开始”为基础,这是不可行的。只有“建议”模式可以用“开始”以外的其他方式实现。在建议模式下,在匹配列表不为空的所有情况下都会显示列表框。

相关问题