combobox ownerdrawfixed删除项目c#

时间:2019-08-09 08:16:56

标签: c# winforms combobox

我在OwnerDrawFixed中有一个带有DrawMode的组合框,当我删除元素时,最后一个门仍然写在文本框中,如何删除它?目前,我使用此代码删除元素,而不显示最后一个元素

public void RemoveCurrentItem()
{
   if (comboBox1.SelectedIndex != -1)
   {                
      comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
      comboBox1.Text = null;
   }       
}

代码抽奖:

if (((System.Windows.Forms.ComboBox)sender).Items.Count != 0)
        {
            e.DrawBackground();
            string text = ((System.Windows.Forms.ComboBox)sender).Items[e.Index].ToString();
            Brush brush;
            if (e.Index == 0)
                brush = Brushes.Red;
            else
                brush = Brushes.Black;
            e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
        }

1 个答案:

答案 0 :(得分:1)

如果您是指ComboBox的文本框部分和仅在此处输入的文本,则该效果确实发生,与所有者绘制控件或控件的数量无关剩下的东西。

if块仅在存在列表元素并且没有清除文本区域的行时才执行。

这将始终清除文本部分:

public void RemoveCurrentItem()
{
    if (comboBox1.SelectedIndex != -1)
    {
        comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
    }
    comboBox1.Text = null;
}