By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,384 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Problem with ListView. OwnerDraw=true. Checkboxes display problem

P: 28
I have used ListView in my Form. I have set OwnerDraw = true. And I have the DrawItem Function as

Expand|Select|Wrap|Line Numbers
  1. private void listView1_DrawItem(object sender,
  2.             DrawListViewItemEventArgs e)
  3.         {
  4.             if (e.Item.Checked)
  5.                 ControlPaint.DrawCheckBox(e.Graphics, 0, e.Bounds.Top + 1, 15, 15,
  6.                                           ButtonState.Flat | ButtonState.Checked);
  7.             else
  8.                 ControlPaint.DrawCheckBox(e.Graphics, 0, e.Bounds.Top + 1, 15, 15, ButtonState.Flat);
  10.             Rectangle rect = new Rectangle(e.Bounds.X + 23, e.Bounds.Y + 1, e.Bounds.Width, e.Bounds.Height);
  11.             if ((e.State & ListViewItemStates.Selected) != 0)
  12.             {
  13.                 // Draw the background and focus rectangle for a selected item.
  14.                 e.Graphics.FillRectangle(Brushes.AliceBlue, rect);
  15.                 e.DrawFocusRectangle();
  16.             }
  17.             else
  18.             {
  19.                 // Draw the background for an unselected item.
  20.                 using (LinearGradientBrush brush =
  21.                     new LinearGradientBrush(rect, Color.White,
  22.                     Color.White, LinearGradientMode.Horizontal))
  23.                 {
  24.                     e.Graphics.FillRectangle(brush, rect);
  25.                 }
  26.             }
  28.             // Draw the item text for views other than the Details view.
  29.             if (listView1.View != View.Details)
  30.             {
  31.                 e.DrawText();
  32.             }
  33.         }

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.
Dec 12 '08 #1
Share this Question
Share on Google+
7 Replies

Expert 100+
P: 221
Is it your intention that the checkbox scrolls off screen to the left, or that it remains visible when using the horizontal scrollbar and that the text just disappears underneath it?
Dec 12 '08 #2

P: 28
When I scroll the horizontal scroll bar.....It draw the Checkboxes improperly.
Sometimes full chekbox sometime half......Sometime location change.
Dec 12 '08 #3

Expert 100+
P: 221
Yes, I tested your code and I can see what you mean. What you need to change depends on what you want the checkbox to do.
Do you want it scroll cleanly, or remain fixed at the edge of the listview?

If you want it to scroll with the text change the first couple of lines of code to:

Expand|Select|Wrap|Line Numbers
  1. if (e.Item.Checked) 
  2.     ControlPaint.DrawCheckBox(e.Graphics, e.Bounds.X, e.Bounds.Top + 1, 15, 15, ButtonState.Flat | ButtonState.Checked); 
  3. else 
  4.     ControlPaint.DrawCheckBox(e.Graphics, e.Bounds.X, e.Bounds.Top + 1, 15, 15, ButtonState.Flat); 
Dec 12 '08 #4

P: 28
No I want it to stay where it is i.e, when I scroll horizontal scroll bar to Right it should remains where it is and when I scroll horizontal scroll bar to left, checkboxes should display properly.
Dec 12 '08 #5

Expert 100+
P: 221
Ok, I've spent a good half-hour looking at this but it looks like one of those things that isn't going to be easy. I remember trying to do something fairly similar myself with the listview a few months back.

If I recall correctly I couldn't get it working in the DrawItem event unless I used CreateGraphics() to get a new graphics object and painted onto that instead. I didn't like that solution, so I started overriding WndProc, which almost yielded the results I wanted, but became far to much work to maintain for what I was trying to do. I'm sure I've still got that code around somewhere but cannot locate it at the moment.

In short it was taking more time than it was worth to get it the way I wanted it. In the end I gave up and found an alternative solution. Sorry I can't be more help.

Maybe there is an alternative way to acheive the results you want. Is a CheckListBox control no use to you?
Dec 12 '08 #6

P: 28
Yes I need checkbox. BTW what is the solution u do? can u supply it?
Dec 12 '08 #7

Expert 100+
P: 221
I'd use a checked list box.
Edit: If you're using Visual Studio you should find it it your control toolbox.
Dec 12 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.