使用复选框C#

时间:2017-05-31 00:01:12

标签: c# colors listbox

我通过文本框将项目添加到列表框中,我想知道在选中复选框时如何更改从文本框添加的列表框项目的颜色。因此,当我选中复选框时,文本框中的颜色变为红色,当我单击按钮将文本框发送到列表框时,列表框项变为红色。如果我没有选中复选框,那么文本颜色是黑色的,所以如果第一个添加了红色,那么我添加到黑色,然后在列表框中我需要看到一个红色和一个黑色。

谢谢,

private void addEventButton_Click(object sender, EventArgs e)
    {


        // Adds events to listbox. 
       if (this.titleTextBox.Text != "")
        {
            listBox1.Items.Add(this.titleTextBox.Text); 
            listBox2.Items.Add(this.titleTextBox.Text);
            this.titleTextBox.Focus();
            this.titleTextBox.Clear();

1 个答案:

答案 0 :(得分:0)

处理ListBox的DrawItem事件

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
   var someObj = listBox1.Items[e.Index] as MyClass;
   Color backColor = Color.Green;
   if (someObj.ID == someID) {
      backColor = Color.Gray;
   }

   // draw back color and text 
   var g = e.Graphics;

   //background:
   SolidBrush backgroundBrush = new SolidColorBrush(backcolor);
   g.FillRectangle(backgroundBrush, e.Bounds);

   //text:
   SolidBrush foregroundBrush = (selected) ? reportsForegroundBrushSelected : reportsForegroundBrush;
   g.DrawString(text, e.Font, foregroundBrush, lbReports.GetItemRectangle(index).Location);
}