Listbox multiline colored items

Scanzee

Member
Joined
May 26, 2007
Messages
23
Programming Experience
1-3
Hello all of you

My question:
Can you make something like a listbox with multiline colored items.
Like this:

----------------------------------------
Name
Age
Adress
Email

-----------------------------------------
 
Dim sql As String = ""
Dim i As Integer


ListBox1.Items.Clear()


If RadioButton1.Checked = True Then
sql = "SELECT * FROM Bestelling Where datumlev =#" & DateTimePicker1.Value.Date & "#"
End If

Dim con As New OleDb.OleDbConnection
con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = Deliche.mdb"

Dim dc As New OleDb.OleDbCommand(sql, con)
Dim dr As OleDb.OleDbDataReader

con.Open()

dr = dc.ExecuteReader

While dr.Read()

idk(i) = dr.Item(2)
ido(i) = dr.Item(0)

ListBox1.Items.Add("BestellingID: " & dr.Item(0) & vbCrLf & "KlantID: " & dr.Item(2) & vbCrLf & "Bestelling: " & vbCrLf & dr.Item(1) & _
vbCrLf & "Datum van bestelling: " & dr.Item(3) & " Datum van levering:" & dr.Item(4))

i += 1
End While
ListBox1.Refresh()
con.Close()

End Sub

Private Sub listbox1_DrawItem(ByVal sender As Object, _
ByVal e As DrawItemEventArgs) Handles ListBox1.DrawItem

' If the item is the selected item, then draw the rectangle filled in
' blue. The item is selected when a bitwise And of the State property
' and the DrawItemState.Selected property is true.
If (e.State And DrawItemState.Selected = DrawItemState.Selected) Then
e.Graphics.FillRectangle(Brushes.CornflowerBlue, e.Bounds)
Else
' Otherwise, draw the rectangle filled in beige.
e.Graphics.FillRectangle(Brushes.Beige, e.Bounds)
End If

' Draw a rectangle in blue around each item.
e.Graphics.DrawRectangle(Pens.Blue, e.Bounds)

'Set Backcolor
e.Graphics.FillRectangle(Brushes.Green, e.Bounds.X, e.Bounds.Y, 10, 10)
' Draw the text in the item.
e.Graphics.DrawString(Me.ListBox1.Items(e.Index), Me.Font, _
Brushes.Black, e.Bounds.X, e.Bounds.Y)

' Draw the focus rectangle around the selected item.
e.DrawFocusRectangle()
Exit Sub
End Sub
-----------------------------------------------------------------------
This code works fine and thanks for the hint. There is just one problem.
When the code is executed, all listbox items are blue, even they are not selected. What can I do about it?
 
Notice the difference in paranthesis:
VB.NET:
If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then
 
It was too good to be true...
The maximum height of a listbox item is 255 pixels...
Is there any work around to make them larger??
 
You can't be serious!? 255px is practically one quarter of a high-res screen... Maybe Listbox is not the right control for your layout?
 
I'm am serious...
You don't wanna know what I'm making...
It's quite complicated...
Which control do you suggest...
Is it a good idea to make to colums??
 
Columns-based DataGridView and Listview could be options, also a Details bound DataTable where you navigate with the BindingNavigator. Other option could be creating a display/input schema with a UserControl and add instances of this for each record to a FlowLayoutPanel.
 
It was too good to be true...
The maximum height of a listbox item is 255 pixels...
Is there any work around to make them larger??

Set your screen to a res of 320x200 = then they will look nearly as large as the screen
 
Back
Top