public void AddToLbx(ListBox lbx, LbxMessageItem message) { ControlInvoke(lbx, new Action(() => { //lbx.Items.Insert(0, message); lbx.Items.Add(message); lbx.SelectedIndex = lbx.Items.Count - 1; })); }
protected void ListBox_DrawItem(object sender, DrawItemEventArgs e) { int idx = e.Index; if (idx != -1) { ListBox lbx = sender as ListBox; Graphics g = e.Graphics; LbxMessageItem lbxItem = lbx.Items[idx] as LbxMessageItem; SolidBrush backgroundBrush = new SolidBrush(lbxItem.BackColor != null ? lbxItem.BackColor : e.BackColor); SolidBrush foregroundBrush = new SolidBrush(lbxItem.ForeColor != null ? lbxItem.ForeColor : e.ForeColor); Font textFont = lbxItem.Font != null ? lbxItem.Font : e.Font; string text = lbxItem.Text != null ? lbxItem.Text : string.Empty; RectangleF rectangle = new RectangleF(new PointF(e.Bounds.X, e.Bounds.Y), new SizeF(e.Bounds.Width, g.MeasureString(text, textFont).Height)); g.FillRectangle(backgroundBrush, rectangle); g.DrawString(text, textFont, foregroundBrush, rectangle); backgroundBrush.Dispose(); foregroundBrush.Dispose(); g.Dispose(); } }