为WinForm combox控件增加自动完成功能

http://hi.baidu.com/softwyy/blog/item/07ca0af679b7822a730eecbc.html

 

private void toolStripButton3_Click(object sender, EventArgs e)
        {
            
if (CanHistoryKey == true)              //让用户自己设置是否愿意使用自动完成的功能
            {
                 AutoCompleteStringCollection auto 
= new AutoCompleteStringCollection();//定义并实例化AutoCompleteStringCollection类
                if (MyLib.Properties.Settings.Default.HistoryKey != null)
                     auto 
= MyLib.Properties.Settings.Default.HistoryKey;//设置时HistoryKey的值是为null的,所以不为空时我们需要加载旧的历史关键字
                 auto.Add(toolStripTextBox1.Text.Trim());                //添加新的历史关键字
                 MyLib.Properties.Settings.Default.HistoryKey = auto;    //将历史关键字进行更新(不过并没有保存)
                 toolStripTextBox1.AutoCompleteCustomSource = auto;   //立刻为控件添加新的历史关键字
             }
         }


//--------------------------------------------
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
             MyLib.Properties.Settings.Default.Save(); 
         }

//--------------------------------------------
private void 清空历史记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
             MyLib.Properties.Settings.Default.HistoryKey 
= null;
             toolStripTextBox1.AutoCompleteCustomSource 
= null;
         }

 

 

posted @ 2010-07-05 15:33  庚武  Views(1164)  Comments(0Edit  收藏  举报