The user draws the item appearance of The ListBox.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.