有什么办法可以在winform的文本框中使用多个单词自动完成功能? [英] Is there any way to use autocomplete with multiple words inside the textbox in a winform?

查看:50
本文介绍了有什么办法可以在winform的文本框中使用多个单词自动完成功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用自动完成功能为文本框显示建议的单词.例如,我键入Tod ...,将显示数据库中所有以"tod"开头的单词,但是如果我选择了另一个单词(例如"was"),则在选择一个单词之后,下面将不再显示建议的单词.可能是因为字符串集合没有"today was".我想显示仅以"was"开头而不是"today was"的建议单词.我正在完成一个预测最常使用的单词的项目.这是我的代码

I want to show the suggested words using autocomplete for the textbox. For instance, i type Tod... all words from my database that starts with "tod" will show but after I chose a word if i type another word like "was" there will be no suggested words below. Probably because the string collection doesn't have "today was". I want to show suggested words that only starts with "was" not "today was". I am attaining to make a project that predicts the most frequent words used. Here is my code

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

}

推荐答案

因此,基本的自行拥有"解决方案将像这样工作:

So a basic "Roll Your Own" solution will work like so:

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

这应该做您想要的,它将显示每个单词的自动完成功能!可以增加一些技巧,例如随着文本的继续移动自动完成框.

This ought to do what you want, it will show autocomplete for each word! One could add a little finesse, such as moving the autocomplete box around as the text moves on.

这篇关于有什么办法可以在winform的文本框中使用多个单词自动完成功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆