最近在对一个之前开发的应用系统作优化,遇到这样一个需求:
能否输入一些字段作类似于百度或Google这样的输入提示? (这是一个winform程序)
经过一些研究和网上一些朋友的意见,利用ComboBox其实很容易就可以实现一个简单的输入提示功能。
下面是一些关键的代码:
- private void LoadData()
- {
- comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; //suggest模糊查询,Append自动填充
- comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
- List<Team> teams = new List<Team>();
- Team team = new Team();
- team.ID = 1;
- team.Name = "Platform";
- teams.Add(team);
- team = new Team();
- team.ID = 2;
- team.Name = "Integrator";
- teams.Add(team);
- team = new Team();
- team.ID = 3;
- team.Name = "MOCA";
- teams.Add(team);
- comboBox1.DataSource = teams;
- comboBox1.DisplayMember = "Name";
- comboBox1.ValueMember = "ID";
- }
- /// <summary>
- /// Team
- /// </summary>
- public class Team
- {
- /// <summary>
- /// Name
- /// </summary>
- public String Name { get; set; }
- /// <summary>
- /// ID
- /// </summary>
- public Double ID { get; set; }
- }
这样以来我们在输入数据时,程序会自动弹出类似的数据项给我们
当然,还有其它方法(如, TextBox + ListView)可以实现输入提示,但这个应该是最快捷的一种。
转载于:https://blog.51cto.com/eagles/664132