如何创建自动完成组合框或文本框以筛选包含字符串的文本

时间:2010-11-30 14:17:56

标签: c# winforms autocomplete

如何创建基于字符串过滤文本的自动完成组合框或文本框?

例如:如果我在TextBox中键入“a”,我只能看到包含“a”的所有字符串。

6 个答案:

答案 0 :(得分:8)

如果您的意思是显示建议,那么在您选择的IDE中选择TextBox时,更改属性是一件简单的事情:

图片中显示的选项允许您更改文本框中自动填充文本的规则以及建议的来源。 (Visual Studio 2010)

Properties

您可以转到以下链接以了解有关TextBox控件的更多信息。

MSDN Text Box Control

答案 1 :(得分:3)

Windows Forms的自动完成实现使用Shell的自动完成对象,该对象只能在Windows Vista之前执行“BeginWith”匹配。 如果您可以要求用户升级到Windows Vista,则可以use IAutoComplete2::SetOptions指定ACO_NOPREFIXFILTERING。否则我恐怕你需要write your own

答案 2 :(得分:1)

以下是我在comboDropDown框中为特定值自动完成的方法。

    void comboBox_Leave(object sender, EventArgs e)
    {
       ComboBox cbo = (sender as ComboBox);
        if (cbo.Text.Length > 0)
        {
            Int32 rowIndex = cbo.FindString(cbo.Text.Trim());
            if (rowIndex != -1)
            {
                cbo.SelectedIndex = rowIndex;
            }
            else
            {
                cbo.SelectedIndex = -1;
            }
        }
        else
        {
            cbo.SelectedIndex = -1;
        }

    }

答案 3 :(得分:0)

根据用户输入过滤结果。优化大型列表,填充您自己的数据和错误处理,以便您完成:

public partial class Form1 : Form
    {

        List<String> data;
        ListView lst = new ListView();
        TextBox txt = new TextBox();

        public Form1()
        {
            InitializeComponent();
            data = new List<string>("Lorem ipsum dolor sit amet consectetur adipiscing elit Suspendisse vel".Split());
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Controls.Add(txt);
            lst.Top = 20;
            txt.TextChanged += new EventHandler(txt_TextChanged);
            lst.View = View.List;
            this.Controls.Add(lst);
            list_items("");
        }

        void txt_TextChanged(object sender, EventArgs e)
        {
            list_items(txt.Text);
        }

        void list_items(string filter)
        {
            lst.Items.Clear();
            List<string> results = (from d in data where d.Contains(filter) select d).ToList();
            foreach (string s in results)
            {
                lst.Items.Add(s);
            }
        }
    }

要使组合框自动完成,请设置AutoCompleteSource和AutoCompleteMode属性:

cbo.AutoCompleteSource = AutoCompleteSource.ListItems;
cbo.AutoCompleteMode = AutoCompleteMode.SuggestAppend;

ListItems源告诉组合使用它的项目集合作为自动完成源。

答案 4 :(得分:0)

我知道这是一个古老的话题,但我也很努力地找到c#autocomplete过滤的解决方案,而且无法找到任何解决方案,所以我想出了我自己简单易行的方法,所以我只是分享那些可能认为有用并想在他们的项目中使用的人。它不使用控件的自动完成功能。它的作用只是简单地从组合框输入文本,在源代码中搜索它,然后只显示来自源代码的匹配代码作为组合框&#39;新来源。我在组合框中实现了它。 KeyUp事件。

让我们说(实际上这就是我在几乎所有需要自动填充的情况下所做的事情)我们将全局分配的DataTable称为dt_source作为组合框&#39; source,它有两列:id(int)和name(string)。

DataTable dt_source = new DataTable("table");
dt_source.Columns.Add("id", typeof(int));
dt_source.Columns.Add("name", typeof(string));

这就是我的KeyUp方法:

private void cmb_box_KeyUp(object sender, KeyEventArgs e)
{
  string srch = cmb_box.Text;
  string srch_str = "ABackCDeleteEFGHIJKLMNOPQRSpaceTUVWXYZD1D2D3D4D5D6D7D8D9D0"; 
  if (srch_str.IndexOf(e.KeyCode.ToString()) >= 0)
  {
    cmb_box.DisplayMember = "name"; // we want name list in the datatable to be shown
    cmb_box.ValueMember = "id"; // we want id field in the datatable to be the value
    DataView dv_source = new DataView(dt_source);  // make a DataView from DataTable so ...
    dv_source.RowFilter = "name LIKE '%"+ srch +"%'"; // ... we can filter it
    cmb_box.DataSource = dv_source; // append this DataView as a new source to the combobox         
    /* The 3 lines below is the tricky part. If you repopulate a combobox, the first
       item in it will be automatically selected so let's unselect it*/
    cmb_box.SelectedIndex = -1; // unselection
    /* Again when a combobox repopulated the text region will be reset but we have the
       srch variable to rewrite what's written before */
    cmb_box.Text = srch;
    /* And since we're rewriting the text region let's make the cursor appear at the
       end of the text - assuming 100 chars is enough*/
    cmb_box.Select(100,0);
    cmb_box.DroppedDown = true; // Showing the dropdownlist
   }
}

答案 5 :(得分:-2)

我认为最好的办法是覆盖OnKeyDown(KeyEventArgs e)事件,并使用该值来过滤ComboBox.AutoCompleteCustomSource。然后,如上所述,将AutoCompleteSource更改为AutoCompleteSource.ListItems。

相关问题