[英]How to disable an item in combbox on popup show
Is there any way to disable specific item inside combobox based on condition.有什么方法可以根据条件禁用组合框中的特定项目。 which need to display on click of combobox ie on popup show
需要在单击组合框时显示,即弹出显示
Step 1. Set the property DrawMode
of the ComboBox
to OwnerDrawFixed
步骤 1. 将
ComboBox
的属性DrawMode
设置为OwnerDrawFixed
Step 2. Change the Item color using Index value步骤 2. 使用索引值更改项目颜色
Font fontValue = new Font("calibri", 12, FontStyle.Regular);
//Form Load
private void form_Load(object sender, EventArgs e)
{
List<string> lstCombxValue = new List<string>();
lstCombxValue.Add("Item A1");
//Item to Disable
lstCombxValue.Add("Item A2");
lstCombxValue.Add("Item A3");
lstCombxValue.Add("Item A4");
lstCombxValue.Add("Item A5");
lstCombxValue.Add("Item A6");
comboBox1.DataSource = lstCombxValue;
}
private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//Check the Condition get the Item Index Value to Disable
//and follow this step to disable the item
if (e.Index == 1)
{
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), fontValue, Brushes.Gray, e.Bounds);
}
else
{
e.DrawBackground();
e.Graphics.DrawString(comboBox1.Items[e.Index].ToString(), fontValue, Brushes.Black, e.Bounds);
e.DrawFocusRectangle();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.