读书人

有ListBox.DrawItem事件为什么ListB

发布时间: 2012-05-23 13:44:13 作者: rapoo

有ListBox.DrawItem事件,为什么ListBox不能选定
我设置的:

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
string s = this.listBox1.Items[e.Index].ToString();
Brush b;
switch (s)
{
case "成功":
b = new SolidBrush(Color.Blue);
break;
case "失败":
b = new SolidBrush(Color.Red);
break;
default:
b = new SolidBrush(this.ForeColor);
break;
}
e.Graphics.DrawString(s, this.Font, b, e.Bounds);
}


为什么listBOx1不能选定项了?

[解决办法]
--可以的噢,前台加入一个TextBox(textBox1)和一个ListBox(listBox1),后台代码如下

C# code
public Form9()        {            InitializeComponent();        }        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)        {            string s = this.listBox1.Items[e.Index].ToString();            Brush b;            switch (s)            {                case "成功":                    b = new SolidBrush(Color.Blue);                    break;                case "失败":                    b = new SolidBrush(Color.Red);                    break;                default:                    b = new SolidBrush(this.ForeColor);                    break;            }            e.Graphics.DrawString(s, this.Font, b, e.Bounds);        }        private void Form9_Activated(object sender, EventArgs e)        {            listBox1.Items.Clear();            listBox1.Items.AddRange(new Object[] { "成功", "失败", "其他" });        }        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)        {            this.textBox1.Text = this.listBox1.SelectedItem.ToString();        }
[解决办法]
可以选定,只是没有画出来吧
e.DrawBackground()就是把选定的哪个蓝条画出来,你也可以不使用它,自己画其它颜色
[解决办法]
探讨
可以选定,只是没有画出来吧
e.DrawBackground()就是把选定的哪个蓝条画出来,你也可以不使用它,自己画其它颜色

读书人网 >C#

热点推荐