【问题标题】:Autocomplete that looks for symbols a string contains rather than starts with自动完成查找字符串包含而不是开头的符号
【发布时间】:2023-04-10 14:48:02
【问题描述】:

我正在编写一个程序,该程序在某一时刻实现了带有自动完成功能的TextBox。目前,为了简单起见,我在设计时使用由多个条目手动填充的CustomSource。虽然自动完成工作正常,但我希望它提出的建议不仅仅是当前输入的文本开始,而是包含存储选项中的任何位置。

例如,如果单词“globe”、“lobe”和“glide”是存储的选项,则输入“gl”正确提示“globe”和“glide”。

但是,当输入“lob”时,我希望它同时建议“globe”和“lobe”。我不确定如何处理这个问题。

以前有人做过吗? VB.NET 或 C# 都可以,只要我能找到合适的 .NET 方法来做到这一点。

干杯! = )

【问题讨论】:

    标签: .net string autocomplete textbox


    【解决方案1】:

    您正在搜索的列表 尝试使用条件是否进入列表

    stringItemInList.IndexOf("txtwhatyousearchfor",StringComparison.OrdinalIgnoreCase) != -1
    

    如果您连接到数据库,请在查询中使用

    parameter LIKE '%txthere%'
    

    问候

    更新

    在你评论之后

    我认为最好的选择是创建您自己的 IAutoComplete 实现。这是它的信息:http://msdn.microsoft.com/en-us/library/bb776292%28VS.85%29.aspx

    您还可以搜索一些创建自己的 IAutoComplete 实现的人的代码示例。

    问候

    【讨论】:

    • 谢谢,但这并不是我想要的。手动编译合适的结果列表并不难。但是,似乎无法访问必须重载才能用其默认搜索例程替换此函数的函数。这是我的主要困难。
    • 如果您不介意告诉功能代码,更好地帮助您
    • 没有功能码。这是一个启用了自动完成功能的TextBox。据我所知,除了创建自己的控件之外,没有其他方法可以做到这一点。
    • 抱歉误会,我使用的是我自己的控件,我向您询问了这个问题......我推荐一个解决方案作为答案编辑,问候
    【解决方案2】:

    所以我一直在寻找这样的东西。带有自动完成功能的文本框,带有包含搜索而不是 StartsWith。

    此版本来自WinForms | C# | AutoComplete in the Middle of a Textbox?

    我能够做到这一点,这是我使用包含的自动完成版本。我希望每个人都觉得这个有用。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing;
    
    namespace TowInvoicing
    {
        //from https://stackoverflow.com/questions/1437002/winforms-c-sharp-autocomplete-in-the-middle-of-a-textbox
        public class AutoCompleteTextBox : TextBox
        {
            private ListBox _listBox;
            private bool _isAdded;
            private String[] _values;
            private String _formerValue = String.Empty;
    
            public AutoCompleteTextBox()
            {
                InitializeComponent();
                ResetListBox();
            }
    
            private void InitializeComponent()
            {
                _listBox = new ListBox();
                this.KeyDown += this_KeyDown;
                this.KeyUp += this_KeyUp;
            }
    
            private void ShowListBox()
            {
                if (!_isAdded)
                {
                    Parent.Controls.Add(_listBox);
                    _listBox.Left = Left;
                    _listBox.Top = Top + Height;
                    _isAdded = true;
                }
                _listBox.Visible = true;
                _listBox.BringToFront();
            }
    
            private void ResetListBox()
            {
                _listBox.Visible = false;
            }
    
            private void this_KeyUp(object sender, KeyEventArgs e)
            {
                UpdateListBox();
            }
    
            private void this_KeyDown(object sender, KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Enter:
                    case Keys.Tab:
                        {
                            if (_listBox.Visible)
                            {
                                Text = _listBox.SelectedItem.ToString();
                                ResetListBox();
                                _formerValue = Text;
                                this.Select(this.Text.Length, 0);
                                e.Handled = true;
                            }
                            break;
                        }
                    case Keys.Down:
                        {
                            if ((_listBox.Visible) && (_listBox.SelectedIndex < _listBox.Items.Count - 1))
                                _listBox.SelectedIndex++;
                            e.Handled = true;
                            break;
                        }
                    case Keys.Up:
                        {
                            if ((_listBox.Visible) && (_listBox.SelectedIndex > 0))
                                _listBox.SelectedIndex--;
                            e.Handled = true;
                            break;
                        }
    
    
                }
            }
    
            protected override bool IsInputKey(Keys keyData)
            {
                switch (keyData)
                {
                    case Keys.Tab:
                        if (_listBox.Visible)
                            return true;
                        else
                            return false;
                    default:
                        return base.IsInputKey(keyData);
                }
            }
    
            private void UpdateListBox()
            {
                if (Text == _formerValue)
                    return;
    
                _formerValue = this.Text;
                string word = this.Text;
    
                if (_values != null && word.Length > 0)
                {
                    string[] matches = Array.FindAll(_values,
                                                     x => (x.ToLower().Contains(word.ToLower())));
                    if (matches.Length > 0)
                    {
                        ShowListBox();
                        _listBox.BeginUpdate();
                        _listBox.Items.Clear();
                        Array.ForEach(matches, x => _listBox.Items.Add(x));
                        _listBox.SelectedIndex = 0;
                        _listBox.Height = 0;
                        _listBox.Width = 0;
                        Focus();
                        using (Graphics graphics = _listBox.CreateGraphics())
                        {
                            for (int i = 0; i < _listBox.Items.Count; i++)
                            {
                                if (i < 20)
                                    _listBox.Height += _listBox.GetItemHeight(i);
                                // it item width is larger than the current one
                                // set it to the new max item width
                                // GetItemRectangle does not work for me
                                // we add a little extra space by using '_'
                                int itemWidth = (int)graphics.MeasureString(((string)_listBox.Items[i]) + "_", _listBox.Font).Width;
                                _listBox.Width = (_listBox.Width < itemWidth) ? itemWidth : this.Width;;
                            }
                        }
                        _listBox.EndUpdate();
                    }
                    else
                    {
                        ResetListBox();
                    }
                }
                else
                {
                    ResetListBox();
                }
            }
    
            public String[] Values
            {
                get
                {
                    return _values;
                }
                set
                {
                    _values = value;
                }
            }
    
            public List<String> SelectedValues
            {
                get
                {
                    String[] result = Text.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    return new List<String>(result);
                }
            }
    
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-04
      • 2013-08-09
      • 2019-11-25
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多