DevExpress.XtraEditors.ComboBoxEdit禁用文本编辑
DevExpress是一套非常丰富的第三方控件,而DevExpress.XtraEditors.ComboBoxEdit又是最基本的控件,默认情况下,ComboBoxEdit的值是即允许选择又允许编辑的,如下图所示:
关键代码
private void Form1_Load(object sender, EventArgs e)
{
List<Student> lst = new List<Student>()
{
new Student() { Id = 0, Name = String.Empty },
new Student() { Id = 1, Name = "Lisa" },
new Student() { Id = 2, Name = "Tom" }
};
this.comboBoxEdit1.Properties.Items.AddRange(lst);
}
但大部分应用场景中,我们希望ComboBoxEdit只允许选择,不需要编辑,这样可以确保输入数据的合法性。
我们只需要设置以下属性就可以了。
CombBoxEdit.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor;
补全代码如下:
private void Form1_Load(object sender, EventArgs e)
{
List<Student> lst = new List<Student>()
{
new Student() { Id = 0, Name = String.Empty },
new Student() { Id = 1, Name = "Lisa" },
new Student() { Id = 2, Name = "Tom" }
};
this.comboBoxEdit1.Properties.Items.AddRange(lst);
this.comboBoxEdit1.Properties.TextEditStyle = DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor; //禁用文本编辑
}
效果如下图: