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

问题描述 投票:0回答:2

我想使用文本框的自动完成功能来显示建议的单词。例如,我输入 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;
    }
}

}

c# .net winforms autocomplete textbox
2个回答
0
投票

因此,基本的“自己动手”解决方案将像这样工作:

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

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


0
投票

最简单的解决方案是实现一个基于Textbox的自定义控件,其中包含Textbox控件和列表框控件。使用文本更改事件使用与用户选择匹配的字符串填充列表框。重点仍然是控制,而不是申请表上下文中包含的控制。在内部,定义的用户控件必须处理来自包含的文本框和列表控件的任何事件。仅当您的自动完成列表包含多个匹配项(根据您的规则)时,才会显示列表框。

如果仅存在一个匹配项,则它(通常)显示在文本框中,并突出显示“完成”文本,其中模式为“附加”。如果建议不是基于“开始于”,那么这是不可行的。除了“starts with”之外,只能实现“suggest”模式。在建议模式下,在匹配列表不为空的所有情况下都会显示列表框。

© www.soinside.com 2019 - 2024. All rights reserved.