我目前有一个自定义的ListBox控件我喜欢它看起来的样子但它缺少SelectedIndexChanged事件。我试图自己添加但是我失败了:/所以总之有人可以帮助我将SelectedIndexChanged事件添加到提供的列表框类中吗?
Public Class Theme_ListBox
Inherits Control
'''I added this bit but it didnt work
'Friend Event SelectedIndexChanged As EventHandler
'Protected Overridable Sub OnSelectedIndexChanged(ByVal e As EventArgs)
' RaiseEvent SelectedIndexChanged(Me, e)
'End Sub
#Region "Variables"
Private WithEvents ListB As New ListBox
Private _Items As String() = {""}
Private _BaseColour As Color = Color.FromArgb(42, 42, 42)
Private _SelectedColour As Color = Color.FromArgb(55, 55, 55)
Private _ListBaseColour As Color = Color.FromArgb(47, 47, 47)
Private _TextColour As Color = Color.FromArgb(255, 255, 255)
Private _BorderColour As Color = Color.FromArgb(35, 35, 35)
#End Region
#Region "Properties"
<Category("Control")>
Public Property Items As String()
Get
Return _Items
End Get
Set(value As String())
_Items = value
ListB.Items.Clear()
ListB.Items.AddRange(value)
Invalidate()
End Set
End Property
<Category("Colours")>
Public Property BorderColour As Color
Get
Return _BorderColour
End Get
Set(value As Color)
_BorderColour = value
End Set
End Property
<Category("Colours")>
Public Property SelectedColour As Color
Get
Return _SelectedColour
End Get
Set(value As Color)
_SelectedColour = value
End Set
End Property
<Category("Colours")>
Public Property BaseColour As Color
Get
Return _BaseColour
End Get
Set(value As Color)
_BaseColour = value
End Set
End Property
<Category("Colours")>
Public Property ListBaseColour As Color
Get
Return _ListBaseColour
End Get
Set(value As Color)
_ListBaseColour = value
End Set
End Property
<Category("Colours")>
Public Property TextColour As Color
Get
Return _TextColour
End Get
Set(value As Color)
_TextColour = value
End Set
End Property
Public ReadOnly Property SelectedItem() As String
Get
Return ListB.SelectedItem
End Get
End Property
Public ReadOnly Property SelectedIndex() As Integer
Get
Return ListB.SelectedIndex
If ListB.SelectedIndex < 0 Then Exit Property
End Get
End Property
Public Sub Clear()
ListB.Items.Clear()
End Sub
Public Sub ClearSelected()
For i As Integer = (ListB.SelectedItems.Count - 1) To 0 Step -1
ListB.Items.Remove(ListB.SelectedItems(i))
Next
End Sub
Protected Overrides Sub OnCreateControl()
MyBase.OnCreateControl()
If Not Controls.Contains(ListB) Then
Controls.Add(ListB)
End If
End Sub
Sub AddRange(ByVal items As Object())
ListB.Items.Remove("")
ListB.Items.AddRange(items)
End Sub
Sub AddItem(ByVal item As Object)
ListB.Items.Remove("")
ListB.Items.Add(item)
End Sub
#End Region
#Region "Draw Control"
Sub Drawitem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles ListB.DrawItem
If e.Index < 0 Then Exit Sub
e.DrawBackground()
e.DrawFocusRectangle()
With e.Graphics
.SmoothingMode = SmoothingMode.HighQuality
.PixelOffsetMode = PixelOffsetMode.HighQuality
.InterpolationMode = InterpolationMode.HighQualityBicubic
.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
If InStr(e.State.ToString, "Selected,") > 0 Then
.FillRectangle(New SolidBrush(_SelectedColour), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height - 1))
.DrawString(" " & ListB.Items(e.Index).ToString(), New Font("Segoe UI", 9, FontStyle.Bold), New SolidBrush(_TextColour), e.Bounds.X, e.Bounds.Y + 2)
Else
.FillRectangle(New SolidBrush(_ListBaseColour), New Rectangle(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
.DrawString(" " & ListB.Items(e.Index).ToString(), New Font("Segoe UI", 8), New SolidBrush(_TextColour), e.Bounds.X, e.Bounds.Y + 2)
End If
.Dispose()
End With
End Sub
Sub New()
SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.UserPaint Or
ControlStyles.ResizeRedraw Or ControlStyles.OptimizedDoubleBuffer, True)
DoubleBuffered = True
ListB.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed
ListB.ScrollAlwaysVisible = False
ListB.HorizontalScrollbar = False
ListB.BorderStyle = BorderStyle.None
ListB.BackColor = _BaseColour
ListB.Location = New Point(3, 3)
ListB.Font = New Font("Segoe UI", 8)
ListB.ItemHeight = 20
ListB.Items.Clear()
ListB.IntegralHeight = False
Size = New Size(130, 100)
End Sub
Protected Overrides Sub OnPaint(e As PaintEventArgs)
Dim B As New Bitmap(Width, Height)
Dim G = Graphics.FromImage(B)
Dim Base As New Rectangle(0, 0, Width, Height)
With G
.SmoothingMode = SmoothingMode.HighQuality
.PixelOffsetMode = PixelOffsetMode.HighQuality
.TextRenderingHint = TextRenderingHint.ClearTypeGridFit
.Clear(BackColor)
ListB.Size = New Size(Width - 6, Height - 5)
.FillRectangle(New SolidBrush(_BaseColour), Base)
.DrawRectangle(New Pen(_BorderColour, 3), New Rectangle(0, 0, Width, Height - 1))
End With
MyBase.OnPaint(e)
G.Dispose()
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic
e.Graphics.DrawImageUnscaled(B, 0, 0)
B.Dispose()
End Sub
#End Region
End Class