• ComboBox自动补全小技巧


    网上看的自动补全实在是太麻烦,有时候还用到了第三方控件,但是现在我的需求是这样的

    有一个Person类,其中有Code、Name两个属性

    Person需要绑定到ComboBox中,在ComboBox中输入Code时,即自动补全相应的Name

    想了半天,采取了一种折中的办法,使ComboBox显示成Code-Name的样式,例如"001-张三"

    这样做的好处是,不必键入汉字“张”,只要加入"0"即可显示与Code匹配的Person

    设置ComboBox的属性

    AutoCompleteSource=ListItems

    AutoCompleteMode=Suggest

      public class Person
    {
    public string Code { get; set; }
    public string Name { get; set; }

    public Person(string code, string name)
    {
    this.Code = code;
    this.Name = name;
    }

    public override string ToString()
    {
    return Code + "-" + Name;
    }
    }
            List<Person> list = new List<Person>();
    list.Add(
    new Person("001", "张三"));
    list.Add(
    new Person("002", "李四"));
    list.Add(
    new Person("wangwu", "王五"));
    list.Add(
    new Person("chenliu", "陈六"));
    list.Add(
    new Person("wangermazi", "王二麻子"));

    comboBox1.DataSource
    = list;

    效果图

     

  • 相关阅读:
    CopyOnWriteArrayList
    Gradle version 2.2 is required. Current version is 2.10
    install mysql on ubuntu
    A<T extends B> and A <? extends B>
    java event listeners and dispatcher
    git
    linux patch usage
    Codeforces Round #404 (Div. 2) C 二分查找
    dijkstra算法模板及其用法
    Sublime Text 3 快捷键精华版
  • 原文地址:https://www.cnblogs.com/leiOOlei/p/1978474.html
Copyright © 2020-2023  润新知