我想改变一些行的背景颜色ListBox.我有两个列表,其中一个有名称,并显示在一个ListBox.第二个列表与第一个列表具有相似的值List.单击按钮时,我想搜索ListBox第二个List,然后更改ListBox出现在那些值中的颜色List.我的搜索结果ListBox如下:
for (int i = 0; i < listBox1.Items.Count; i++)
{
for (int j = 0; j < students.Count; j++)
{
if (listBox1.Items[i].ToString().Contains(students[j].ToString()))
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道使用哪种方法来改变ListBox行的外观.有谁能够帮我?
**编辑:**
我写了我的代码如下:
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
Brush myBrush = Brushes.Black;
Brush myBrush2 = Brushes.Red;
g.FillRectangle(new SolidBrush(Color.Silver), e.Bounds);
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), e.Font, myBrush, e.Bounds, StringFormat.GenericDefault);
for (int i = 0; i < listBox1.Items.Count; i++)
{
for (int j = 0; j < existingStudents.Count; j++)
{
if (listBox1.Items[i].ToString().Contains(existingStudents[j]))
{
e.Graphics.DrawString(listBox1.Items[i].ToString(),
e.Font, myBrush2, e.Bounds, StringFormat.GenericDefault);
}
}
}
e.DrawFocusRectangle();
}
Run Code Online (Sandbox Code Playgroud)
现在,它吸引我List的ListBox,但是当我第一次按一下按钮,它以红色显示只有那些在学生List,当我点击ListBox它绘制的所有元素.我希望它会显示所有元素,当我单击按钮时,它将显示所有元素和List红色中找到的元素.我的错误在哪里?
Erc*_*can 32
我发现解决方案,而不是使用ListBox我使用ListView.It允许更改列表项BackColor.
private void listView1_Refresh()
{
for (int i = 0; i < listView1.Items.Count; i++)
{
listView1.Items[i].BackColor = Color.Red;
for (int j = 0; j < existingStudents.Count; j++)
{
if (listView1.Items[i].ToString().Contains(existingStudents[j]))
{
listView1.Items[i].BackColor = Color.Green;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Jus*_*tin 22
您需要自己绘制项目.将DrawMode更改为OwnerDrawFixed并处理DrawItem事件.
/// <summary>
/// Handles the DrawItem event of the listBox1 control.
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="System.Windows.Forms.DrawItemEventArgs"/> instance containing the event data.</param>
private void listBox1_DrawItem( object sender, DrawItemEventArgs e )
{
e.DrawBackground();
Graphics g = e.Graphics;
// draw the background color you want
// mine is set to olive, change it to whatever you want
g.FillRectangle( new SolidBrush( Color.Olive), e.Bounds );
// draw the text of the list item, not doing this will only show
// the background color
// you will need to get the text of item to display
g.DrawString( THE_LIST_ITEM_TEXT , e.Font, new SolidBrush( e.ForeColor ), new PointF( e.Bounds.X, e.Bounds.Y) );
e.DrawFocusRectangle();
}
Run Code Online (Sandbox Code Playgroud)
小智 15
首先使用此命名空间:
using System.Drawing;
Run Code Online (Sandbox Code Playgroud)
在表单的任何位置添加:
listBox.DrawMode = DrawMode.OwnerDrawFixed;
listBox.DrawItem += listBox_DrawItem;
Run Code Online (Sandbox Code Playgroud)
这是事件处理程序:
private void listBox_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
Graphics g = e.Graphics;
g.FillRectangle(new SolidBrush(Color.White), e.Bounds);
ListBox lb = (ListBox)sender;
g.DrawString(lb.Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Black), new PointF(e.Bounds.X, e.Bounds.Y));
e.DrawFocusRectangle();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
113403 次 |
| 最近记录: |