473,729 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Owner-Draw ListBox problems

I am owner-drawing a listbox, in an attempt to create a nice list with some
custom "fields" and text layout.

Essentially it works, but I must be missing something big, conceptually,
because I get all kinds of screen artifacts and weirdness.

My general goal is: list item with a few areas for text, every other item
shaded a light color for readability, font color changes with selection. The
listbox is populated with custom structurs loaded with data. Here is my
prototype code so far:

Private Sub lstAccounts_Dra wItem(ByVal sender As Object, ByVal e As
System.Windows. Forms.DrawItemE ventArgs) Handles lstAccounts.Dra wItem'

' Set fonts and colors
Dim cLine As New System.Drawing. Pen(System.Draw ing.SystemColor s.ControlDark)
Dim fLarge As New Font("Verdana", 12, FontStyle.Bold, GraphicsUnit.Pi xel)
Dim fNorm As New Font("Verdana", 10, FontStyle.Regul ar, GraphicsUnit.Pi xel)
Dim fMed As New Font("Verdana", 11, FontStyle.Regul ar, GraphicsUnit.Pi xel)
Dim bNorm As System.Drawing. Brush

' Make Variable Defaults
Dim mX As Single = e.Bounds.Width
Dim mT As Single = e.Bounds.Top
Dim mY As Single = e.Bounds.Height
Dim m75 As Single = mX * 0.75
Dim m50 As Single = mX * 0.5
Dim objA As Account
Dim objS As System.Drawing. SizeF

' Get Object
objA = lstAccounts.Ite ms(e.Index)

' Draw background
e.DrawBackgroun d()
e.DrawFocusRect angle()

' Assign Pen Color
If (e.State = DrawItemState.S elected) Or (e.State = DrawItemState.S elected +
DrawItemState.F ocus) Then
bNorm = System.Drawing. Brushes.White
Else
bNorm = System.Drawing. Brushes.Black
End If

' Draw Background if not selected and odd
If e.Index Mod 2 = 1 Then
If (e.State <> DrawItemState.S elected) And (e.State <>
DrawItemState.S elected + DrawItemState.F ocus) Then
e.Graphics.Fill Rectangle(Syste m.Drawing.Syste mBrushes.Contro lLight,
0, mT, mX, mT + mY)
End If
End If

' Draw Bottom Line
e.Graphics.Draw Line(cLine, 0, mT + mY, mX, mT + mY)

' Draw 75% Line, 50% line
e.Graphics.Draw Line(cLine, m75, mT, m75, mT + mY)
e.Graphics.Draw Line(cLine, m50, mT, m50, mT + mY)
e.Graphics.Draw Line(cLine, m50, mT + (mY / 2), m75, mT + (mY / 2))

' Draw Name of Item, Description
e.Graphics.Draw String(objA.Acc ount_Name, fLarge, bNorm, 5, mT + 5)
e.Graphics.Draw String(objA.Acc ount_Memo, fNorm, bNorm, 5, mT + 20)

' Draw Balance Information (right aligned)
objS = e.Graphics.Meas ureString(objA. Account_Balance .ToString("$0.0 0"), fMed)
e.Graphics.Draw String(objA.Acc ount_Balance.To String("$0.00") , fMed, bNorm,
mX - objS.Width - 5, mT + 15)

End Sub

Like I said, this code essentially works. Start it up, add some items to
the list box, it shows correctly. However, if you resize the form, or give
focus, or remove focus, or generally interact with it the rendering goes
bannas. What am I missing? Anyone out there with more experience than me?
(Read: any experience in this regard!)

Many thanks!
Nov 21 '05 #1
2 3135

dan heskett wrote:
I am owner-drawing a listbox, in an attempt to create a nice list with some
custom "fields" and text layout.

Essentially it works, but I must be missing something big, conceptually,
because I get all kinds of screen artifacts and weirdness.

My general goal is: list item with a few areas for text, every other item
shaded a light color for readability, font color changes with selection. The
listbox is populated with custom structurs loaded with data. Here is my
prototype code so far:
[code snipped. code is all fine IMO, except that you run without Option
Strict On, but that's not important right now. Also, all DrawItem
should have

If e.Index = -1 Then Exit Sub

at the start, because you DO get called by empty listboxes. Anyway]
Like I said, this code essentially works. Start it up, add some items to
the list box, it shows correctly. However, if you resize the form, or give
focus, or remove focus, or generally interact with it the rendering goes
bannas. What am I missing? Anyone out there with more experience than me?
(Read: any experience in this regard!)


An interesting problem. I have implemented owner drawn listboxes in the
past, and found things largely went as planned in DrawItem. So I set
about creating a minimal example of your problem. This is what I came
up with:

Create a Windows Forms app, drop a list box and a button on the form,
anchor the listbox to all four sides of the form (so that we can induce
it to redraw by resizing the form). Set the listbox DrawMode to
OwnerDrawFixed. Then here's some code:

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
System.EventArg s) Handles Button1.Click
With ListBox1.Items
.Clear()
.Add("apple apple apple apple apple apple apple apple apple
")
.Add("banana banana banana banana banana banana banana
banana banana ")
.Add("grape grape grape grape grape grape grape grape grape
")
End With
End Sub

Private Sub ListBox1_DrawIt em(ByVal sender As Object, ByVal e As
System.Windows. Forms.DrawItemE ventArgs) Handles ListBox1.DrawIt em
With e
If .Index = -1 Then Exit Sub

.DrawBackground ()

.Graphics.DrawS tring(ListBox1. Items(.Index).T oString,
Me.Font, Brushes.Black, _
New PointF(.Bounds. Left, .Bounds.Top)) ' Line XXX

.DrawFocusRecta ngle()
End With
End Sub
Now, in this example as it stands, all the DrawItem does is duplicate
the behaviour of a normal listbox. When you resize the form, everything
WORKS.

Now, change Line XXX to

New PointF(.Bounds. Right - 10, .Bounds.Top)) ' Line XXX-2

We are now asking that each listbox item gets drawn starting 10 pixels
from the right hand edge of the list box (This means of course we don't
get to see much text, but this is just an example).

Now try it. On my machine, as I drag the form wider, *only the first
item in the listbox (apple...) gets redrawn correctly* - the second and
third lines show severe graphics redraw issues.

Trying .Right\2 gives similar results. However, something like .Left+10
is *fine*.

So where does that leave us? I call bug, but that isn't immediately
useful. The key seems to be:

****
When owner-drawing, make all your drawn stuff 'anchored' to the TOP
LEFT of e.Bounds
****

Thoughts and experiments from others welcome!

--
Larry Lard
Replies to group please

Nov 21 '05 #2
Larry,

Thanks for your help! I notice all kinds of odd behaviour, but I figured
out my original problem by accident.

For one thing, I need to invalidate the control on refresh to cause the
existing items to be redrawn.

Second thing, I was using the Top + Height as the parameter for where to
draw my bottom line on the item. The problem is that this position is
actually the first pixel of the *next* item, so item #1 is drawn, and it's
good, but when item #2 is drawn it overwrites the line drawn on the previous
item.

Thanks for your help!
Nov 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4834
by: Ange T | last post by:
Hi, I have a secured Access 2000 database, and have encountered a strange error that I can't find any similar postings on. I'd really appreciate your help! In my database, I have a number of local tables as well as a number of linked tables. The User that I have logged in as ("Developer1") is the owner of all objects and is Administrator. In the User and Group Permissions box, under Change Owner, Developer1 is the current owner
2
5046
by: John Regan | last post by:
Hello All I am trying to find the owner of a file or folder on our network (Windows 2000 Server) using VB.Net and/or API. so I can search for Folders that don't follow our company's specified folder structure and naming conventions and then send a Net send message to those users telling them to rectify. The information I want to get is when you select the file/folder and then: Properties -> Security Tab -> Advanced Button -> Owner Tab ->...
7
3651
by: Dave Coate | last post by:
Hi everyone, I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I have administrative rights to all the computers in the company. Some of my user base has full control rights to their files and have elected to remove my access to some files. It is possible for an administrator to regain access, but it is a messy process and can be time consuming....
3
4012
by: Dave Coate | last post by:
Hello again, I am going to re-post a question. I got some excellent suggestions from Rob and Mattias on this but their ideas did not solve the problem. Here is the original post: ***************************************************** I am looking for a way to 'override' file security and read the Owner of a file to which I have no access. I am a system administrator, as such I have administrative rights to all the computers in the...
2
1743
by: Bruce D | last post by:
I have a form that opens up with my application. It acts as the main menu. Let's just say it looks like this: Public Class frmStartup ' some other settings Public UserID As Integer = 0 ' a bunch of functions and subs End Class Now let's say I have a button click on this form that calls another page
1
2569
by: Mike Mascari | last post by:
While migrating to 7.4, which performs quite nicely btw, I must have performed some sequence of the migration incorrectly. Now, when I use pg_dump on a database for backup, I get: pg_dump: WARNING: owner of data type "plr_environ_type" appears to be invalid pg_dump: WARNING: owner of data type "r_typename" appears to be invalid pg_dump: WARNING: owner of function "plr_call_handler" appears to be invalid pg_dump: WARNING: owner of...
7
18307
by: Laurence | last post by:
Hi folks, Who knows how to retrieve the owner of SQL objects, such as SCHEMA, TABLE etc.? Is GRANTOR within catalog view owner? Thanks, Laurence
7
1985
by: MarkJ | last post by:
him im kinda new to c-sharp... to reference up the class chain, how do i reference the super class (parent) example class mybase { protected int abc=0 } classs myclass:mybase {
2
3438
by: levimc | last post by:
I know that that topic may be old to you but I looked at other more- than-two-year-old topics related to mine. However, I didn't find them working for my project at all because its errors return back to me everytime. The error I have on that project said: "An unhandled exception of type 'System.EntryPointNotFoundException' occurred in TestSysaudit.exe
10
4600
by: SimeonD | last post by:
Hi Is there a way I can fnd the Owner of a folder, using vb.net? I know how to find the permissions, but I can't figure how to find the owner. Thanks SimeonD
0
8764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9428
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9289
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9210
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8159
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6026
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3242
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 we have to send another system
2
2694
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2166
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.