473,486 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problem with ListView. OwnerDraw=true. Checkboxes display problem

28 New Member
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 8848
nukefusion
221 Recognized Expert New Member
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
prpradip
28 New Member
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 Recognized Expert New Member
@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
prpradip
28 New Member
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 Recognized Expert New Member
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
prpradip
28 New Member
Yes I need checkbox. BTW what is the solution u do? can u supply it?
Dec 12 '08 #7
nukefusion
221 Recognized Expert New Member
@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
1459
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
1342
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
2784
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
289
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
1795
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
1512
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
5048
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
2642
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
2310
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
7123
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,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.