progpradip
Programmer
- Nov 26, 2008
- 23
I have used ListView in my Form. I have set OwnerDraw = true. And I have the DrawItem Function as
private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
if (e.Item.Checked)
ControlPaint.DrawCheckBox(e.Graphics, 0, e.Bounds.Top + 1, 15, 15,
ButtonState.Flat / ButtonState.Checked);
else
ControlPaint.DrawCheckBox(e.Graphics, 0, e.Bounds.Top + 1, 15, 15, ButtonState.Flat);
Rectangle rect = new Rectangle(e.Bounds.X + 23, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height);
if ((e.State & ListViewItemStates.Selected) != 0)
{
// Draw the background and focus rectangle for a selected item.
e.Graphics.FillRectangle(Brushes.AliceBlue, rect);
e.DrawFocusRectangle();
}
else
{
// Draw the background for an unselected item.
using (LinearGradientBrush brush =
new LinearGradientBrush(rect, Color.White,
Color.White, LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(brush, rect);
}
}
// Draw the item text for views other than the Details view.
if (listView1.View != View.Details)
{
e.DrawText();
}
}
My problem is when I scroll the horizontal scrollbar (NOT with vertical scrollbar) checkboxes are not displaying correctly or drawing improperly (sometimes hidden and appears after selecting listitem). Why is this happening? Is it because of Graphics use? How can I get rid of this problem? Please help me.
private void listView1_DrawItem(object sender,
DrawListViewItemEventArgs e)
{
if (e.Item.Checked)
ControlPaint.DrawCheckBox(e.Graphics, 0, e.Bounds.Top + 1, 15, 15,
ButtonState.Flat / ButtonState.Checked);
else
ControlPaint.DrawCheckBox(e.Graphics, 0, e.Bounds.Top + 1, 15, 15, ButtonState.Flat);
Rectangle rect = new Rectangle(e.Bounds.X + 23, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height);
if ((e.State & ListViewItemStates.Selected) != 0)
{
// Draw the background and focus rectangle for a selected item.
e.Graphics.FillRectangle(Brushes.AliceBlue, rect);
e.DrawFocusRectangle();
}
else
{
// Draw the background for an unselected item.
using (LinearGradientBrush brush =
new LinearGradientBrush(rect, Color.White,
Color.White, LinearGradientMode.Horizontal))
{
e.Graphics.FillRectangle(brush, rect);
}
}
// Draw the item text for views other than the Details view.
if (listView1.View != View.Details)
{
e.DrawText();
}
}
My problem is when I scroll the horizontal scrollbar (NOT with vertical scrollbar) checkboxes are not displaying correctly or drawing improperly (sometimes hidden and appears after selecting listitem). Why is this happening? Is it because of Graphics use? How can I get rid of this problem? Please help me.