'*************************************************************************** '* ClassSosPhoneList * '* * '* This class expands the native ListBox with some funcionality as the * '* capability to draw the text in various colors etc wich is used to * '* represent the different states of the sos phones. * '* * '* EQUITEL C.V. February 2007 ALMDI* '*************************************************************************** Public Class ClassSosPhoneList Inherits System.Windows.Forms.ListBox Delegate Sub RefreshCallBack() Private Sub myClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click Refresh() End Sub Private Sub myDrawItem(ByVal sender As Object, ByVal e As DrawItemEventArgs) Handles MyBase.DrawItem Dim g As Graphics = Graphics.FromHwnd(MyBase.Handle) Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic) Dim P As ClassSosPhone Dim TextPen As Brush Dim S As String Dim F As Font If MyBase.Items.Count >= (e.Index + 1) Then P = MyBase.Items(e.Index) S = P.Name e.DrawBackground() e.DrawFocusRectangle() 'Choose the appropiate drawing color depending on the selection 'and te status of the sos phone If e.Index = MyBase.SelectedIndex Then If P.Connected Then TextPen = New SolidBrush(Color.LightGreen) Else TextPen = New SolidBrush(Color.Gray) End If Else If P.Connected Then TextPen = New SolidBrush(Color.Green) Else TextPen = New SolidBrush(Color.Gray) End If End If 'Draws a rectangle to enclose each item of the list e.Graphics.DrawRectangle(Pens.Black, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height) 'Draw the text If P.Connected Then F = New Font(MyBase.Font, FontStyle.Bold) Else F = MyBase.Font End If e.Graphics.DrawString(P.Name, F, TextPen, e.Bounds.X, e.Bounds.Y) End If End Sub Private Sub myMeasureItem(ByVal sender As Object, ByVal e As MeasureItemEventArgs) Handles MyBase.MeasureItem Dim g As Graphics = Graphics.FromHwnd(MyBase.Handle) Dim sf As StringFormat = New StringFormat(StringFormat.GenericTypographic) Dim size As SizeF Dim height As Single = 0 Dim oFont As Font Dim P As ClassSosPhone 'measure the height of what you are about to draw 'and let the listbox know this If MyBase.Items.Count >= (e.Index + 1) Then oFont = MyBase.Font P = MyBase.Items(e.Index) size = g.MeasureString(P.Name, oFont, 500, sf) height = size.Height + 5 e.ItemHeight = height End If End Sub Public Sub MyRefresh() If InvokeRequired Then Dim d As New RefreshCallBack(AddressOf MyRefresh) Me.Invoke(d) Else Me.Refresh() End If End Sub End Class