Analysis on the display of lines in the drop-down list using VB. NET

Source: Internet
Author: User

VB. NET has a lot to learn about. Here we mainly introduce the VB. NET implementation drop-down list, including the control improvements.
. NET is a tool provided by Microsoft to address future computing needs. In. NET Framework provides many controls to design and implement the user interface in programming. However, in practical applications, you may need to improve the controls provided by the system, the following list cannot be displayed in lines. This article describes how to use VB. NET to display the drop-down list in rows.
Design a drop-down list that can automatically fold rows
VB. NET implementation drop-down list. Each item occupies one row in the ComboBox control. If the content length of the selected item exceeds the width of the drop-down list, the excess part is not displayed, this may result in incomplete and unselectable content. This control is improved to display rows when a line is not completely displayed. To Prevent Users From mistaken the line items for two options, different options are distinguished by separated colors. The class code is as follows:
1. Public Class myComboBox
2. Inherits System. Windows. Forms. ComboBox
3.
4. # Region "code generated by Windows Form Designer"
5 ....
6. # End Region
7. the following code displays options in different colors.
8. Private Sub myComboBox_DrawItem (ByVal sender As Object,
ByVal e As _ System. Windows. Forms. DrawItemEventArgs) Handles MyBase. DrawItem
9. If e. Index <0 Then Exit Sub
10. Dim txtColor As SolidBrush
11. Dim bgColor As SolidBrush
12. Dim txtfnt As Font
13. txtColor = New SolidBrush (Color. Black)
14. If e. Index/2 = CInt (e. Index/2) Then
15. bgColor = New SolidBrush (Color. White)
16. Else
17. bgColor = New SolidBrush (Color. LightYellow)
18. End If
19. If e. State And DrawItemState. Selected Then
20. txtColor = New SolidBrush (Color. Blue)
21. End If
22. e. Graphics. FillRectangle (bgColor, e. Bounds)
23. e. Graphics. DrawRectangle (Pens. Black, e. Bounds)
24. Dim r As New RectangleF (e. Bounds. X, e. Bounds. Y, e. Bounds. Width, e. Bounds. Height)
25. e. Graphics. DrawString (Items (e. Index). ToString, Me. Font, txtColor, r)
26. End Sub
27. The following code calculates the size required for each row of options
28. Private Sub myComboBox_MeasureItem (ByVal sender As Object,
ByVal e As _ System. Windows. Forms. MeasureItemEventArgs) Handles MyBase. MeasureItem
29. Dim lsize As SizeF
30. lsize = e. Graphics. MeasureString (Items (e. Index). ToString, Me. Font, New SizeF (Me. Width, 200 ))
31. e. ItemHeight = lsize. Height
32. e. ItemWidth = lsize. Width
33. End Sub
34. End Class
The preceding section introduces VB. NET to display the drop-down list in rows.

Related Article

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.