![](/img/trans.png)
[英]Adding items to a listbox on a different class in C# using a second form
[英]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.