winfrom中ListBox自动滚动到底部,并设置单行文字颜色

    xiaoxiao2021-12-14  17

    滚动可以通过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(); }

    转载请注明原文地址: https://ju.6miu.com/read-965512.html

    最新回复(0)