c# - 组合框 ownerdrawfixed 删除项目 c#
问题描述
我在 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);
}
解决方案
ComboBox
如果您指的是刚刚输入的文本框部分,那么效果确实会发生,并且与所有者绘制控件或剩余多少项无关。
该if
块仅在存在列表元素并且不存在清除文本区域的行时才执行。
这将始终清除文本部分:
public void RemoveCurrentItem()
{
if (comboBox1.SelectedIndex != -1)
{
comboBox1.Items.RemoveAt(comboBox1.SelectedIndex);
}
comboBox1.Text = null;
}