講解VB.NET COMBOBOX控件
Visual Basic.NET是基于微軟.NET Framework之上的面向?qū)ο蟮闹虚g解釋性語言,可以看作是Visual Basic在.Net Framework平臺上的升級版本,增強了對面向?qū)ο蟮闹С?。但由于改動太大,導致VB.net對VB的向后兼容性不好,在業(yè)界引起不小的爭議。
大多的VB.NET程序員使用Visual Studio .Net作為IDE(integrated development environment). SharpDevelop是另一種可用的開源的IDE。VB.NET需要在.Net Framework平臺上才能執(zhí)行。
前不久,有位朋友發(fā)帖,尋求顏色選擇的VB.NET COMBOBOX控件的制作方法,經(jīng)過試驗,整理了出來,僅供參考。
- Private Sub filllistboxwithcolors()
- Me.ComboBox1.DrawMode = DrawMode.OwnerDrawFixed
- Me.ComboBox1.DropDownStyle = ComboBoxStyle.DropDownList
- Me.ComboBox1.ItemHeight = 15
- '避免閃爍beginupdate
- Me.ComboBox1.BeginUpdate()
- ComboBox1.Items.Clear()
- Dim pi As Reflection.PropertyInfo
- For Each pi In GetType(Color).GetProperties(Reflection.BindingFlags.
Public Or Reflection.BindingFlags.Static)- Me.ComboBox1.Items.Add(pi.Name)
- Next
- ComboBox1.EndUpdate()
- End Sub
- Private Sub ComboBox1_DrawItem(ByVal sender As Object,
ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem- If e.Index < 0 Then Exit Sub
- Dim rect As Rectangle = e.Bounds '每一項的邊框
- '繪制項如果被選中則顯示高亮顯示背景,否則用白色
- If e.State And DrawItemState.Selected Then
- e.Graphics.FillRectangle(SystemBrushes.Highlight, rect)
- Else
- e.Graphics.FillRectangle(SystemBrushes.Window, rect)
- End If
- Dim colorname As String = ComboBox1.Items(e.Index)
- Dim b As New SolidBrush(Color.FromName(colorname))
- '縮小選定項區(qū)域()
- rect.Inflate(-16, -2)
- '填充顏色(文字對應的顏色)
- e.Graphics.FillRectangle(b, rect)
- '繪制邊框()
- e.Graphics.DrawRectangle(Pens.Black, rect)
- Dim b2 As Brush
- '確定顯示的文字的顏色()
- If CInt(b.Color.R) + CInt(b.Color.G) + CInt(b.Color.B) > 128 * 3 Then
- b2 = Brushes.Black
- Else
- b2 = Brushes.White
- End If
- e.Graphics.DrawString(colorname, Me.ComboBox1.Font, b2, rect.X, rect.Y)
- End Sub
- Private Sub Form1_Load
(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load- filllistboxwithcolors()
- End Sub
【編輯推薦】