首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

有ListBox.DrawItem事件,为什么ListBox不能选定,该如何处理

2012-05-23 
有ListBox.DrawItem事件,为什么ListBox不能选定我设置的:private void listBox1_DrawItem(object sender,

有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()就是把选定的哪个蓝条画出来,你也可以不使用它,自己画其它颜色

热点排行