• 根据拼音首字母进行过滤的combobox


    keywords: 拼音 首字母 过滤

    在combobox中输入汉字拼音的首字母时,下面列出对应的可选项,就像下面这样

    1。 首先在数据库中需要设计一个表,专门用来存放药物及对应的拼音首字母,这样当用户输入拼音字母后就可以到表中查找匹配的药物,然后再显示

    2。 下面的委托方法负责将从数据库获得的查询结果集重新邦定到combobox并自动弹出下拉列表。下面的代码需要注意这几行

    // set the cursor at the end of the text
                    ctrl.Focus();
                    ctrl.Select(oldText.Length, oldText.Length);

    其功能就是保证用户能够连续输入字母,并使光标始终位于combobox最后,如果不加这两行,光标就会跑到第一个字母前面

    [c-sharp] view plain copy
     
    1. public delegate void ReBindDataSource(ComboBox ctrl, DataSet ds);  
    2.   
    3. public static void BindDataSource(ComboBox ctrl, DataSet ds)  
    4. {  
    5.     try  
    6.     {  
    7.         ctrl.BeginUpdate();  
    8.   
    9.         // make sure change it to false, or there will be exception if the droppedDownList is empty  
    10.         ctrl.DroppedDown = false;  
    11.   
    12.         string oldText = ctrl.Text;  
    13.   
    14.         ctrl.DataSource = ds.Tables[0];  
    15.         ctrl.DisplayMember = ds.Tables[0].Columns[0].ColumnName;  
    16.   
    17.         // set the text, so user can input continuely  
    18.         ctrl.Text = oldText;  
    19.   
    20.         // set the cursor at the end of the text  
    21.         ctrl.Focus();  
    22.         ctrl.Select(oldText.Length, oldText.Length);  
    23.   
    24.         // do not drop down if it is empty, or there will be exception  
    25.         if (ctrl.Items.Count > 0)  
    26.         {  
    27.             ctrl.DroppedDown = true;  
    28.         }  
    29.   
    30.         ctrl.Cursor = Cursors.Default;  
    31.     }  
    32.     catch (Exception ex)  
    33.     {  
    34.         //statusLabel.Text = ex.Message;  
    35.     }  
    36.     finally  
    37.     {  
    38.         ctrl.EndUpdate();  
    39.     }  
    40.   
    41. }  

    3。 下面的方法

    [c-sharp] view plain copy
     
    1. private void cbM1_TextUpdate(object sender, EventArgs e)  
    2.         {  
    3.            // 获得输入的拼音  
    4.             string abbr = cbM1.Text.Trim();  
    5.               
    6.            // 从数据库中查寻符合条件的药物集合  
    7.             DataSet ds = mPresenter.GetMedicineNamesByAbbr(abbr);  
    8.             // 重新邦定  
    9.             cbM1.BeginInvoke(new ReBindDataSource(BindDataSource), cbM1, ds);  
    10.         }  
     
    0
  • 相关阅读:
    第五节: EF高级属性(一) 之 本地缓存、立即加载、延迟加载(不含导航属性)
    第四节: EF调用存储过程的通用写法和DBFirst模式子类调用的特有写法
    第三节: EF调用普通SQL语句的两类封装(ExecuteSqlCommand和SqlQuery )
    Android ListView常见配置说明
    如何配置IIS使其支持APK文件的下载
    Android scrollview和GridView混合使用
    WCF配置后支持通过URL进行http方式调用
    转战博客园
    Android Intent参数传递
    Android 使用SQLite
  • 原文地址:https://www.cnblogs.com/yelanggu/p/6617581.html
Copyright © 2020-2023  润新知