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?