ListBox.DrawItem事件:见MSDN
ListBox.BeginUpdate 方法:见MSDNBeginUpdate 方法的作用是防止控件自绘,那下面的代码,我就不懂了private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
string s = listBox1.Items[e.Index].ToString();
e.Graphics.DrawString(s, this.Font, new SolidBrush(Color.Red), e.Bounds); //红色字体
}
private void button4_Click(object sender, EventArgs e)
{
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.BeginUpdate();
}
private void button5_Click(object sender, EventArgs e)
{
listBox1.EndUpdate();
}button4点击事件中,我是首先设置DrawMode属性为OwnerDrawFixed,此时就应该引发DrawItem事件了,listBox1应该绘制了,每一项都应该是红色字体了吧。而事实上显示的结果是listBox1并没有自绘,全是空白,这是为什么呢?BeginUpdate方法是在后面的啊?
我又实验了一下,如果注释掉listBox1.BeginUpdate();显示结果则是listBox1的项都是红色字体BeginUpdate方法应该是阻止自绘吧,难道是把已经绘制了的抹掉?有谁能解释一下吗?谢谢!!
ListBox.BeginUpdate 方法:见MSDNBeginUpdate 方法的作用是防止控件自绘,那下面的代码,我就不懂了private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
string s = listBox1.Items[e.Index].ToString();
e.Graphics.DrawString(s, this.Font, new SolidBrush(Color.Red), e.Bounds); //红色字体
}
private void button4_Click(object sender, EventArgs e)
{
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.BeginUpdate();
}
private void button5_Click(object sender, EventArgs e)
{
listBox1.EndUpdate();
}button4点击事件中,我是首先设置DrawMode属性为OwnerDrawFixed,此时就应该引发DrawItem事件了,listBox1应该绘制了,每一项都应该是红色字体了吧。而事实上显示的结果是listBox1并没有自绘,全是空白,这是为什么呢?BeginUpdate方法是在后面的啊?
我又实验了一下,如果注释掉listBox1.BeginUpdate();显示结果则是listBox1的项都是红色字体BeginUpdate方法应该是阻止自绘吧,难道是把已经绘制了的抹掉?有谁能解释一下吗?谢谢!!
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货