Control derived from Listbox not drawing properly
-
I've got the following code running and the idea is that a row in the DB has a coloured letter in it to denote customer or supplier etc. The first row i highlight seems to vanish, the background for the row is blue and the selected index is set so that's all nice but the text disappears. If i click the row a second time the row unselects and the text comes back.
public partial class CompanyListControl : ListBox { int _previousIndex = 0; public CompanyListControl() { InitializeComponent(); this.DrawMode = DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(e); if (!DesignMode) { e.DrawBackground(); if (this.SelectedIndex == e.Index) { if (_previousIndex != e.Index) { InvalidateItem(_previousIndex); DrawSelected(e); _previousIndex = e.Index; } } else { DrawUnselected(e); } } } public void InvalidateItem(int index) { if ((index < 0) || (index >= this.Items.Count)) return; object InvalidatedObject = this.Items[index]; this.Items.RemoveAt(index); this.Items.Insert(index, InvalidatedObject); } private void DrawSelected(DrawItemEventArgs e) { if (e.Index == -1) return; int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.7f; float TextPos = PurchasePos * 2; int VerticalPos = e.Bounds.Top; e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, Brushes.White, TextPos, e.Bounds.Top); e.Graphics.DrawString("C", f, Brushes.Red, SalesPos, VerticalPos); } private void DrawUnselected(DrawItemEventArgs e) { int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.8f; floa
-
I've got the following code running and the idea is that a row in the DB has a coloured letter in it to denote customer or supplier etc. The first row i highlight seems to vanish, the background for the row is blue and the selected index is set so that's all nice but the text disappears. If i click the row a second time the row unselects and the text comes back.
public partial class CompanyListControl : ListBox { int _previousIndex = 0; public CompanyListControl() { InitializeComponent(); this.DrawMode = DrawMode.OwnerDrawFixed; } protected override void OnDrawItem(DrawItemEventArgs e) { base.OnDrawItem(e); if (!DesignMode) { e.DrawBackground(); if (this.SelectedIndex == e.Index) { if (_previousIndex != e.Index) { InvalidateItem(_previousIndex); DrawSelected(e); _previousIndex = e.Index; } } else { DrawUnselected(e); } } } public void InvalidateItem(int index) { if ((index < 0) || (index >= this.Items.Count)) return; object InvalidatedObject = this.Items[index]; this.Items.RemoveAt(index); this.Items.Insert(index, InvalidatedObject); } private void DrawSelected(DrawItemEventArgs e) { if (e.Index == -1) return; int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.7f; float TextPos = PurchasePos * 2; int VerticalPos = e.Bounds.Top; e.Graphics.DrawString(this.Items[e.Index].ToString(), e.Font, Brushes.White, TextPos, e.Bounds.Top); e.Graphics.DrawString("C", f, Brushes.Red, SalesPos, VerticalPos); } private void DrawUnselected(DrawItemEventArgs e) { int RowWidth = e.Bounds.Width; Font f = new Font("Arial Black", e.Font.Size); Size s = TextRenderer.MeasureText("C", f); int SalesPos = 0; float PurchasePos = s.Width * 0.8f; floa
It was wery hard to find your code problem, but i did find it. Use this:
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (!DesignMode)
{
e.DrawBackground();
if (this.SelectedIndex == e.Index)
DrawSelected(e);
else
DrawUnselected(e);
}
}Also InvalidateItem function removed item and inserted, witch coused to be OnDrawItem called. You have created infinitive loop. Edit: also I do not think
if (!DesignMode)
is nesesery, because you want to see items in designer mode if you add items using property windows. This is normal behaviour for ListView -
It was wery hard to find your code problem, but i did find it. Use this:
protected override void OnDrawItem(DrawItemEventArgs e)
{
base.OnDrawItem(e);
if (!DesignMode)
{
e.DrawBackground();
if (this.SelectedIndex == e.Index)
DrawSelected(e);
else
DrawUnselected(e);
}
}Also InvalidateItem function removed item and inserted, witch coused to be OnDrawItem called. You have created infinitive loop. Edit: also I do not think
if (!DesignMode)
is nesesery, because you want to see items in designer mode if you add items using property windows. This is normal behaviour for ListViewthank you, not at my desk at the moment but i'll try that fix in the morning. The invalidate thing was added because i thought maybe it needed to redraw to fix it, adding it seemed to make no difference at all, I should have taken it out before posting :-O Thanks again, Russ
-
thank you, not at my desk at the moment but i'll try that fix in the morning. The invalidate thing was added because i thought maybe it needed to redraw to fix it, adding it seemed to make no difference at all, I should have taken it out before posting :-O Thanks again, Russ
Russell Jones wrote:
i thought maybe it needed to redraw to fix it
Actualy you only draw an items. For that usaly happens in OnDraw function.