C# 根据条件为组合框中的文本添加颜色

C# 根据条件为组合框中的文本添加颜色,c#,combobox,C#,Combobox,我有以下填充组合框的代码。来自数据库的项将布尔值设置为true或false。如果布尔值设置为true,我想将这些项目涂成红色。我绑定组合框的代码如下所示 if (HasCoded) { listAnestheticMeds= new List<AnesthMedsInventory>(); listAnestheticMeds = AnesthMeds.CreateObject(); fo

我有以下填充组合框的代码。来自数据库的项将布尔值设置为true或false。如果布尔值设置为true,我想将这些项目涂成红色。我绑定组合框的代码如下所示

            if (HasCoded) {
            listAnestheticMeds= new List<AnesthMedsInventory>();
            listAnestheticMeds = AnesthMeds.CreateObject();
            for (int i = 0; i < listAnestheticMeds.Count; i++) {
                if (listAnestheticMeds[i].Hidden == false && listAnestheticMeds[i].IsEmergencyMed) {
                    this.comboAnesthMed.Items.Add(listAnestheticMeds[i].AnesthMedName.ToString());

                }
            }
        }
if(hascooded){
ListMeds=新列表();
ListAnystemeds=AnesthMeds.CreateObject();
for(int i=0;i
使用另一个循环,如果条件为false,则向组合框中添加项(为简洁起见省略)。根据我所读的内容,我需要将DrawMode设置为OwnerDrawVariable,但不确定如何在_DrawItemEvent处理程序中编写代码以使用我的代码块。

确定

以下是完整的解决方案:

        private void comboAnesthMed_MeasureItem(object sender,
        System.Windows.Forms.MeasureItemEventArgs e) {
        e.ItemHeight = 12;
        e.ItemWidth = 160;
    }

    private void comboAnesthMed_DrawItem(object sender, DrawItemEventArgs e) {
        // Draw the background 
        e.DrawBackground();
        // Determine the forecolor based on whether or not the item is selected    
        HasCoded = Convert.ToBoolean(AnestheticRecords.GetCodeStatus(anestheticRecordNum));
        if (HasCoded) {
            listAnestheticMeds = new List<AnesthMedsInventory>();
            listAnestheticMeds = AnesthMeds.CreateObject();
            for (int i = 0; i < listAnestheticMeds.Count; i++) {
                IsEmergencyMed = new string[listAnestheticMeds.Count];
                if (listAnestheticMeds[i].Hidden == false && listAnestheticMeds[i].IsEmergencyMed) {
                    if (comboAnesthMed.Items[e.Index].ToString() == listAnestheticMeds[i].AnesthMedName.ToString()) {
                        brush = Brushes.Red;
                    }
                }
                if (listAnestheticMeds[i].Hidden == false &! listAnestheticMeds[i].IsEmergencyMed) {
                    if (comboAnesthMed.Items[e.Index].ToString() == listAnestheticMeds[i].AnesthMedName.ToString()) {
                        brush = Brushes.Black;
                    }
                }

            }
        }
        // Draw the text    
        e.Graphics.DrawString(comboAnesthMed.Items[e.Index].ToString(), ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y) ;
    }
private void comboAnesthMed\u MeasureItem(对象发送方,
System.Windows.Forms.MeasureItemEventArgs(e){
e、 项目高度=12;
e、 项目宽度=160;
}
private void comboAnesthMed_DrawItem(对象发送方,DrawItemEventArgs e){
//画背景
e、 牵引杆接地();
//根据是否选择项目确定前景色
hascooded=Convert.ToBoolean(麻醉剂记录.GetCodeStatus(麻醉剂记录编号));
如果(已编码){
ListMeds=新列表();
ListAnystemeds=AnesthMeds.CreateObject();
for(int i=0;i
问题已解决

以下是完整的解决方案:

        private void comboAnesthMed_MeasureItem(object sender,
        System.Windows.Forms.MeasureItemEventArgs e) {
        e.ItemHeight = 12;
        e.ItemWidth = 160;
    }

    private void comboAnesthMed_DrawItem(object sender, DrawItemEventArgs e) {
        // Draw the background 
        e.DrawBackground();
        // Determine the forecolor based on whether or not the item is selected    
        HasCoded = Convert.ToBoolean(AnestheticRecords.GetCodeStatus(anestheticRecordNum));
        if (HasCoded) {
            listAnestheticMeds = new List<AnesthMedsInventory>();
            listAnestheticMeds = AnesthMeds.CreateObject();
            for (int i = 0; i < listAnestheticMeds.Count; i++) {
                IsEmergencyMed = new string[listAnestheticMeds.Count];
                if (listAnestheticMeds[i].Hidden == false && listAnestheticMeds[i].IsEmergencyMed) {
                    if (comboAnesthMed.Items[e.Index].ToString() == listAnestheticMeds[i].AnesthMedName.ToString()) {
                        brush = Brushes.Red;
                    }
                }
                if (listAnestheticMeds[i].Hidden == false &! listAnestheticMeds[i].IsEmergencyMed) {
                    if (comboAnesthMed.Items[e.Index].ToString() == listAnestheticMeds[i].AnesthMedName.ToString()) {
                        brush = Brushes.Black;
                    }
                }

            }
        }
        // Draw the text    
        e.Graphics.DrawString(comboAnesthMed.Items[e.Index].ToString(), ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y) ;
    }
private void comboAnesthMed\u MeasureItem(对象发送方,
System.Windows.Forms.MeasureItemEventArgs(e){
e、 项目高度=12;
e、 项目宽度=160;
}
private void comboAnesthMed_DrawItem(对象发送方,DrawItemEventArgs e){
//画背景
e、 牵引杆接地();
//根据是否选择项目确定前景色
hascooded=Convert.ToBoolean(麻醉剂记录.GetCodeStatus(麻醉剂记录编号));
如果(已编码){
ListMeds=新列表();
ListAnystemeds=AnesthMeds.CreateObject();
for(int i=0;i