Note: All discussions about the code are conducted in. NET Framework 2.0.
Are you tired of the static style of ListBox? Maybe your program needs a ListBox with richer appearance, of course, we can rewrite the listtbox control or create a new custom control to achieve these requirements. This will undoubtedly add a lot of labor and job intensity. Is there a better way? Let's see if the following methods can meet your requirements.
Add code to the drawitem event of ListBox.
First, The ListBox control list box is drawn by the user.
Listbox1.drawmode = system. Windows. Forms. drawmode. ownerdrawfixed
Then add the listbox1.drawitem event.
The Code is as follows:
Listbox1.drawitem event
Private sub listboxincludrawitem (byval sender as system. Object, byval e as system. Windows. Forms. drawitemeventargs) handles listbox1.drawitem
Dim G as graphics = E. Graphics
'Get the text to be displayed (you can also directly retrieve data from the datatable according to E. Index)
Dim strvalue as string = system. Convert. tostring (listbox1.items. Item (E. Index ))
If not string. isnullorempty (strvalue) then
Dim strvalues () as string = strvalue. Split ("| ")
If strvalues. Length = 6 then
'When adding data to ListBox, each item is separated by '| '.
'Item code: strvalues (0)
'No: strvalues (1)
'..
'Message: strvalues (5)
'Item code (first column of the First line)
G. drawstring (strvalues (0), Font, brushes. Black, E. bounds. Left + col1pos, E. bounds. Top + row1pos)
'No (second column of the First row)
G. drawstring (strvalues (1), Font, brushes. Black, E. bounds. Left + col2pos, E. bounds. Top + row1pos)
'Item name (column 3 in the first row)
G. drawstring (strvalues (2), Font, brushes. Black, E. bounds. Left + col3pos, E. bounds. Top + row1pos)
'Consumption/production (first column of the Second row)
G. drawstring (strvalues (3), Font, brushes. Black, E. bounds. Left + col1pos, E. bounds. Top + row2pos)
'Linkno (second row and second column)
G. drawstring (strvalues (4), Font, brushes. Black, E. bounds. Left + col2pos, E. bounds. Top + row2pos)
'Message (first column of the third row)
G. drawstring (strvalues (5), Font, brushes. Black, E. bounds. Left + col1pos, E. bounds. Top + row3pos)
End if
End if
'Bottommost Line'
G. drawline (pens. Black, E. bounds. Left, E. bounds. Bottom-1, E. bounds. Right, E. bounds. Bottom-1)
End sub
The running effect is as follows:
Sample Code