473,585 Members | 2,739 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 3116

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
4830
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...
2
5032
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...
7
3641
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...
3
4001
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....
2
1737
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
2553
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:...
7
18286
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
1981
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
3426
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...
10
4589
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
7900
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8192
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. ...
1
7943
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...
0
8204
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6592
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...
1
5705
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3828
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2338
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
0
1167
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...

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.