簡體   English   中英

使用復選框C#添加具有不同顏色的列表框項目

[英]Add listbox items with different colors using a checkbox C#

我正在通過文本框將項目添加到列表框,並且我想知道如何在選中復選框時更改從文本框添加的列表框項目的顏色。 因此,當我選中復選框時,文本框中的顏色變為紅色,而當我單擊按鈕以將文本框發送到列表框時,列表框項變為紅色。 如果未選中該復選框,則文本顏色為黑色,因此如果添加的第一個為紅色,則添加為黑色,然后在列表框中需要看到一個紅色和一個黑色。

謝謝,

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();

處理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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM