滚动可以通过TopIndex来设置:
private void AddMsg(string msg)
{
bool scroll = false;
if (this.listBox1.TopIndex == this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight))
scroll = true;
this.listBox1.Items.Add(msg);
if (scroll)
this.listBox1.TopIndex = this.listBox1.Items.Count - (int)(this.listBox1.Height / this.listBox1.ItemHeight);
this.listBox1.SelectedIndex = this.listBox1.Items.Count - 1;
}
颜色主要通过DrawItem方法进行行级重绘
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
if (e.Index < 0)
{
e.DrawFocusRectangle();
return;
}
if (listBox1.Items[e.Index].ToString().Contains("成功"))
{
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, new SolidBrush(Color.Lime), e.Bounds);
}
else
{
e.Graphics.DrawString(((ListBox)sender).Items[e.Index].ToString(), e.Font, new SolidBrush(e.ForeColor), e.Bounds);
}
e.DrawFocusRectangle();
}