[C#] 纯文本查看 复制代码 using System;
using System.Drawing;
using System.Windows.Forms;
partial class ComboBoxExample : Form
{
[STAThreadAttribute]
public static void Main()
{
Application.Run(new ComboBoxExample());
}
}
partial class ComboBoxExample
{
private ComboBox GradeCbox;
private Label TipsLbl;
public ComboBoxExample()
{
this.Size = new Size(500, 380);
this.StartPosition = FormStartPosition.CenterScreen;
GradeCbox = new ComboBox();
GradeCbox.Size = new Size(400, 60);
GradeCbox.Location = new Point((500-400) / 2, (380-60) / 2);
GradeCbox.Font = new Font("楷体", 12, FontStyle.Bold);
GradeCbox.DataSource = new string[]
{
"AutoCompleteMode.Append",
"AutoCompleteMode.None",
"AutoCompleteMode.Suggest",
"AutoCompleteMode.SuggestAppend",
"AutoCompleteSource.AllSystemSource",
"AutoCompleteSource.AllUrl",
"AutoCompleteSource.CustomSource",
"AutoCompleteSource.FileSystem",
"AutoCompleteSource.FileSystemDirectories",
"AutoCompleteSource.HistoryList",
"AutoCompleteSource.ListItems",
"AutoCompleteSource.None",
"AutoCompleteSource.RecentlyUsedList"
};
// 指定自动匹配数据源
// GradeCbox.AutoCompleteSource = AutoCompleteSource.CustomSource;
// GradeCbox.AutoCompleteCustomSource.AddRange(new string[] {"AAA", "BBB", "ABC", "BCA"});
// 指定自己的数据项为自动匹配的数据源
GradeCbox.AutoCompleteSource = AutoCompleteSource.ListItems;
GradeCbox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
GradeCbox.SelectedIndexChanged += new EventHandler(GradeCboxSelectIndexChanged);
// GradeCbox.DropDownStyle = ComboBoxStyle.DropDownList;
this.Controls.Add(GradeCbox);
TipsLbl = new Label();
TipsLbl.Text = "未选中";
TipsLbl.Size = new Size(200, 60);
TipsLbl.Location = new Point((500-200) / 2, ((380-60) / 2 - 60) / 2);
TipsLbl.Font = new Font("楷体", 12, FontStyle.Bold);
this.Controls.Add(TipsLbl);
}
private void GradeCboxSelectIndexChanged(object sender, EventArgs e)
{
if (GradeCbox.SelectedIndex != -1)
{
TipsLbl.Text = GradeCbox.Items[GradeCbox.SelectedIndex].ToString();
}
}
} |