Newbie, about DrawItem when using OwnerDraw!

T

Try Guy

I'm trying to learn this ownerdrawing thing and I have tested a few things
with a listbox and drawing the items myself..

Here' my small Form Load
'-----------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

Dim Counter As Integer

For Counter = 1 To 5

Dim myItem As New ListViewItem("Item")

myItem.SubItems.Add("text")

ListBox1.Items.Add(myItem)

Next

End Sub

'-----------------------------------------


I have this in my DrawItem Sub

'------------------------------------------------
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem

Dim MyCaption As String = "Item " & e.Index

Dim MyBrush As System.Drawing.Brush = System.Drawing.Brushes.Black

Dim MyFont As New Font("Arial", 10, FontStyle.Regular, GraphicsUnit.Pixel)

Dim MySizeF As SizeF = e.Graphics.MeasureString(MyCaption, MyFont)

e.Graphics.DrawString(MyCaption, MyFont, MyBrush, e.Bounds.X + 20,
e.Bounds.Y)

End Sub

'--------------------------------------------------------------------------

Since I want to add the text "text" in form load.. wouldn't it be good if
that text actually did show up in the Itembox and not the text "Item" that I
use in theDrawItem... now.. I understand of course that it's the DrawItem
that does all this stuff.. but... what if I, in DrawItem, does'nt have
access to the text that I want to add with an item... How does all this
work?

In the e areguments I have'nt found anything that will give me the "text"
text... from what triggered the DrawItem..

And also.. what about subitems hwo do they get in there... if I woudl to
draw them myself.. ?

/Try Guy
 
C

Cablewizard

First a comment. You mention sub items and such and you are creating a
"ListViewItem", however you are adding the item to a ListBox, not a ListView.
This may be your first bit of confusion.

As far as the other stuff...
There are any number of ways to get the information you want.
The "best" way would depend on just what you really want to achieve.
Given the ListBox you are actually using, to get the text string you could do
this:

'------------------------------------------------
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As
System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim thisListbox As ListBox = DirectCast(sender, ListBox)
Dim MyCaption As String = thisListbox.Items(e.Index).ToString
Dim MyBrush As Brush = New SolidBrush(e.ForeColor)

e.DrawBackground()

With thisListbox
e.Graphics.DrawString(MyCaption, .Font, MyBrush, e.Bounds.X + 20,
e.Bounds.Y)
End With

End Sub
'--------------------------------------------------------------------------

But like I said, there are a number of ways to do things in here, it just
depends on what you really want to achieve.

Gerald
 
T

Try Guy

Thanx, that exaclt what I needed to know before I went on.. and yes I guess
I kind confused the ListBox and ListView.. Oops.. Now things makes sence to
me a lot more!

/Try Guy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top