473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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);
  9.  
  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.             }
  27.  
  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
7 8838
nukefusion
221 Expert 100+
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?
Dec 12 '08 #2
When I scroll the horizontal scroll bar.....It draw the Checkboxes improperly.
Sometimes full chekbox sometime half......Sometime location change.
Dec 12 '08 #3
nukefusion
221 Expert 100+
@prpradip
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); 
  5.  
  6.  
Dec 12 '08 #4
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
nukefusion
221 Expert 100+
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
Yes I need checkbox. BTW what is the solution u do? can u supply it?
Dec 12 '08 #7
nukefusion
221 Expert 100+
@prpradip
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

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

Similar topics

0
by: Richard | last post by:
Hello. In VB .NET I have a class derived from ListView like this: Public Class ListViewSorted Inherits System.Windows.Forms.ListView 'code to make this ListViewSorted class sortable... End...
1
by: Andre | last post by:
Hi, I have a strange problem when i try to display my data on a web page. Everything's fine until i try to display a Date Value. If i try to display this, no problem : obj("data1")...
3
by: andrewcw | last post by:
I have a simple winform with the following code. But although I can read back the info, the display fails to provide the text or the cell background color changes. private void ListViewBroke()...
0
by: Jonas Knaus | last post by:
Hello Is it not possible to have more than one checked item on one line on a ListView? I only figured out how to have one checkBox but i would like have a checkBox on every Column. Is this...
1
by: tim cummings via .NET 247 | last post by:
I am writing a program that interfaces with a cars pcm via theserial port and recieves data (such as rpm) at an interval, oflets say 1 second, and then formats it. The problem is, I wouldlike to...
2
by: tlyczko | last post by:
I have an odd display problem with Access 2003 on XP SP2, latest patches, etc. I am working on a not large DB with several forms and <100 records per table, it is still in development. ...
1
by: tmda | last post by:
I'm using OwnerDraw for some customization in my ListView - adding color to item backgrounds. I'm using the ListView specifically for it's OwnerDraw capability. I originally was using the...
2
by: pssraju | last post by:
Hi, I am having applet display problem on my PC and the same thing working fine on some of my colleagues PC's. When I cross checked content through view source its exactly same on both the PC's....
2
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i know how to work with the owner draw mode in the ListView-Class, but how do i draw the last three dots in a column if the text exceeds the columns width? To make it clear, i am talking...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.