Problem with ListView. OwnerDraw=true. Checkboxes display problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prpradip
    New Member
    • Nov 2008
    • 28

    Problem with ListView. OwnerDraw=true. Checkboxes display problem

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

    Code:
    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.
  • nukefusion
    Recognized Expert New Member
    • Mar 2008
    • 221

    #2
    Hi,
    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?

    Comment

    • prpradip
      New Member
      • Nov 2008
      • 28

      #3
      When I scroll the horizontal scroll bar.....It draw the Checkboxes improperly.
      Sometimes full chekbox sometime half......Somet ime location change.

      Comment

      • nukefusion
        Recognized Expert New Member
        • Mar 2008
        • 221

        #4
        Originally posted by prpradip
        When I scroll the horizontal scroll bar.....It draw the Checkboxes improperly.
        Sometimes full chekbox sometime half......Somet ime location change.
        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:

        Code:
        if (e.Item.Checked) 
            ControlPaint.DrawCheckBox(e.Graphics, e.Bounds.X, e.Bounds.Top + 1, 15, 15, ButtonState.Flat | ButtonState.Checked); 
        else 
            ControlPaint.DrawCheckBox(e.Graphics, e.Bounds.X, e.Bounds.Top + 1, 15, 15, ButtonState.Flat);

        Comment

        • prpradip
          New Member
          • Nov 2008
          • 28

          #5
          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.

          Comment

          • nukefusion
            Recognized Expert New Member
            • Mar 2008
            • 221

            #6
            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?

            Comment

            • prpradip
              New Member
              • Nov 2008
              • 28

              #7
              Yes I need checkbox. BTW what is the solution u do? can u supply it?

              Comment

              • nukefusion
                Recognized Expert New Member
                • Mar 2008
                • 221

                #8
                Originally posted by prpradip
                Yes I need checkbox. BTW what is the solution u do? can u supply it?
                I'd use a checked list box.
                Edit: If you're using Visual Studio you should find it it your control toolbox.

                Comment

                Working...