日期:2014-05-17 浏览次数:20756 次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private Dictionary<string, string> Cities = new Dictionary<string, string>()
{
{ "北京", "BJ" }, { "上海", "SH" }, { "苏州", "SZ" }, { "包头", "BT" }, { "北京南", "BJN" }
};
private void Form1_Load(object sender, EventArgs e)
{
foreach (var item in Cities.Select(x => x.Key))
{
comboBox1.Items.Add(item);
}
var result = Cities.Select(x => x.Value);
comboBox1.AutoCompleteCustomSource.Clear();
foreach (var item in result)
comboBox1.AutoCompleteCustomSource.Add(item);
comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
private void comboBox1_TextChanged(object sender, EventArgs e)
{
var result = Cities.Where(x => x.Value == comboBox1.Text);
if (result.Count() > 0)
comboBox1.Text = result.First().Key;
}
&n