导读:本期聚焦于小伙伴创作的《C#怎么实现类似搜索框的提示列表?C#如何使用AutoComplete技巧》,敬请观看详情,探索知识的价值。以下视频、文章将为您系统阐述其核心内容与价值。如果您觉得《C#怎么实现类似搜索框的提示列表?C#如何使用AutoComplete技巧》有用,将其分享出去将是对创作者最好的鼓励。

在C#的WinForms应用开发中,为输入框添加自动提示列表是提升用户交互体验的常见需求,借助内置的AutoComplete功能可以快速实现基础效果,也可以自定义逻辑适配复杂场景。

C#怎么实现类似搜索框的提示列表?C#如何使用AutoComplete技巧

C#内置AutoComplete基础用法

WinForms的TextBox控件自带AutoComplete相关属性,只需要配置好数据源和模式就能实现基础提示效果。核心属性包含三个:AutoCompleteMode设置提示触发模式,AutoCompleteSource设置提示数据来源,AutoCompleteCustomSource设置自定义提示数据集合。

属性说明

属性名可选值作用说明
AutoCompleteModeNone、Suggest、Append、SuggestAppendNone表示不启用,Suggest显示提示列表,Append自动补全文本,SuggestAppend同时生效
AutoCompleteSourceFileSystem、HistoryList、CustomSource等设置提示数据的来源类型,CustomSource表示使用自定义集合
AutoCompleteCustomSourceAutoCompleteStringCollection类型存储自定义提示字符串的集合,仅在AutoCompleteSource为CustomSource时生效

基础实现示例

以下代码演示如何为文本框配置固定的提示列表:

using System;
using System.Windows.Forms;

namespace AutoCompleteDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            InitAutoComplete();
        }

        private void InitAutoComplete()
        {
            // 创建自定义提示集合
            AutoCompleteStringCollection source = new AutoCompleteStringCollection();
            source.AddRange(new string[] { "苹果", "香蕉", "橘子", "梨子", "葡萄", "草莓" });

            // 配置文本框的AutoComplete属性
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.AutoCompleteCustomSource = source;
        }
    }
}

动态更新提示列表的实现

实际场景中提示数据往往是动态的,比如根据用户历史输入、数据库查询结果动态生成,这时候可以在文本框的TextChanged事件中更新提示集合。

using System;
using System.Windows.Forms;
using System.Collections.Generic;

namespace AutoCompleteDemo
{
    public partial class Form2 : Form
    {
        // 模拟数据库中的商品名称列表
        private List<string> allProducts = new List<string>
        {
            "华为手机", "华为平板", "华为笔记本",
            "小米手机", "小米电视", "小米手环",
            "苹果手机", "苹果平板", "苹果耳机"
        };

        public Form2()
        {
            InitializeComponent();
            // 初始不设置提示源,在文本变化时动态更新
            textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            textBox1.TextChanged += TextBox1_TextChanged;
        }

        private void TextBox1_TextChanged(object sender, EventArgs e)
        {
            string input = textBox1.Text.Trim();
            AutoCompleteStringCollection dynamicSource = new AutoCompleteStringCollection();

            if (!string.IsNullOrEmpty(input))
            {
                // 筛选包含输入内容的商品名称
                foreach (var product in allProducts)
                {
                    if (product.Contains(input))
                    {
                        dynamicSource.Add(product);
                    }
                }
            }

            // 更新提示集合
            textBox1.AutoCompleteCustomSource = dynamicSource;
        }
    }
}

自定义提示列表控件实现

如果内置的AutoComplete样式无法满足需求,比如需要显示多列信息、自定义列表项样式,可以结合ListBox控件自定义提示列表。核心思路是监听文本框的输入事件,将匹配的结果展示在ListBox中,点击ListBox项时回填到文本框。

using System;
using System.Windows.Forms;
using System.Collections.Generic;

namespace AutoCompleteDemo
{
    public partial class Form3 : Form
    {
        private ListBox suggestionList;
        private List<string> allData = new List<string>
        {
            "北京", "上海", "广州", "深圳",
            "杭州", "成都", "武汉", "西安"
        };

        public Form3()
        {
            InitializeComponent();
            InitCustomSuggestion();
        }

        private void InitCustomSuggestion()
        {
            // 初始化自定义提示列表控件
            suggestionList = new ListBox();
            suggestionList.Visible = false;
            suggestionList.Width = textBox1.Width;
            suggestionList.Height = 120;
            // 设置列表位置在文本框下方
            suggestionList.Top = textBox1.Bottom + 2;
            suggestionList.Left = textBox1.Left;
            suggestionList.Parent = this;

            // 绑定事件
            textBox1.TextChanged += TextBox1_TextChanged_Custom;
            textBox1.Leave += TextBox1_Leave;
            suggestionList.Click += SuggestionList_Click;
        }

        private void TextBox1_TextChanged_Custom(object sender, EventArgs e)
        {
            string input = textBox1.Text.Trim();
            suggestionList.Items.Clear();

            if (string.IsNullOrEmpty(input))
            {
                suggestionList.Visible = false;
                return;
            }

            // 匹配数据
            foreach (var item in allData)
            {
                if (item.Contains(input))
                {
                    suggestionList.Items.Add(item);
                }
            }

            suggestionList.Visible = suggestionList.Items.Count > 0;
        }

        private void TextBox1_Leave(object sender, EventArgs e)
        {
            // 文本框失去焦点时隐藏提示列表,延迟隐藏避免点击列表项时先触发失去焦点
            BeginInvoke(new Action(() => { suggestionList.Visible = false; }));
        }

        private void SuggestionList_Click(object sender, EventArgs e)
        {
            if (suggestionList.SelectedItem != null)
            {
                textBox1.Text = suggestionList.SelectedItem.ToString();
                suggestionList.Visible = false;
            }
        }
    }
}

注意事项

  • 使用内置AutoComplete时,如果数据源很大,频繁更新AutoCompleteCustomSource可能会有性能问题,建议做输入防抖处理,比如等待用户输入暂停300毫秒后再更新提示数据。
  • 自定义提示列表时,需要处理文本框获得焦点、其他控件点击等场景,确保提示列表能正确显示和隐藏,避免出现列表残留的问题。
  • 如果提示数据需要异步加载,比如从远程接口获取,要注意异步操作的时序问题,避免旧请求的结果覆盖新的输入匹配结果。

C#AutoCompleteWinForms提示列表修改时间:2026-07-17 16:09:31

免责声明:​ 已尽一切努力确保本网站所含信息的准确性。网站内容多为原创整理与精心编撰,观点力求客观中立。本站旨在免费分享,内容仅供个人学习、研究或参考使用。若引用了第三方作品,版权归原作者所有。如内容涉及您的权益,请联系我们处理。
内容垂直聚焦
专注技术核心技术栏目,确保每篇文章深度聚焦于实用技能。从代码技巧到架构设计,为用户提供无干扰的纯技术知识沉淀,精准满足专业提升需求。
知识结构清晰
覆盖从开发到部署的全链路。AI、前端、编程、数据库、服务器、建站、系统层层递进,构建清晰学习路径,帮助用户系统化掌握开发与运维所需的核心技术。
深度技术解析
拒绝泛泛而谈,深入技术细节与实践难点。无论是数据库优化还是服务器配置,均结合真实场景与代码示例进行剖析,致力于提供可直接应用于工作的解决方案。
专业领域覆盖
精准对应开发生命周期。从前端界面到后端编程,从数据库操作到服务器运维,形成完整闭环,一站式满足全栈工程师和运维人员的技术需求。
即学即用高效
内容强调实操性,步骤清晰、代码完整。用户可根据教程直接复现和应用于自身项目,显著缩短从学习到实践的距离,快速解决开发中的具体问题。
持续更新保障
专注既定技术方向进行长期、稳定的内容输出。确保各栏目技术文章持续更新迭代,紧跟主流技术发展趋势,为用户提供经久不衰的学习价值。