[SOLVED] [2005] ToolStripComboBox with Font Styles?
I have most of the basic ideas. I'm just having problems with defining the EventHandler for ComboBox_DrawItem... Any ideas?
Code:
Public Class ctlTSFontCombo
Inherits System.Windows.Forms.ToolStripComboBox
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
Me.DropDownStyle = ComboBoxStyle.DropDownList
Me.DropDownWidth = 150
Me.ComboBox.DrawMode = DrawMode.OwnerDrawVariable
AddHandler ComboBox.DrawItem, AddressOf MyComboBox_DrawItem
'Actually shows 8 items because ItemHeight is increade
Me.MaxDropDownItems = 14
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
'
'ctlTSFontCombo
'
Me.Font = New System.Drawing.Font("Arial", 8.25!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
End Sub
#End Region
Private Sub MyComboBox_DrawItem(ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles MyComboBox.DrawItem
If e.Index >= 0 Then
Dim txt$ = Me.Items(e.Index).ToString
Dim fnt As Font = New Font(e.Font.Name, 14, FontStyle.Bold) 'Default = legible text
'Selected line will draw in the default font
'All others will draw in the individual line's named font
If e.State And DrawItemState.ComboBoxEdit Then
fnt = e.Font 'Combobox's edit text always draws in the box's own font
ElseIf (e.State And DrawItemState.Selected) = 0 Then
'If not the selected line, create a custom font
'Try all permutations of regular/bold/italic/underline/strikeout
'If it can't do any of them, you still have the default
Try
Dim ff As FontFamily = New FontFamily(txt)
For tStyle As FontStyle = 0 To 15
If (ff.IsStyleAvailable(tStyle)) Then
fnt = New Font(ff.Name, 14, tStyle)
Exit For
End If
Next
Catch ex As Exception
'Gets here if font has been deleted while app is running, or other error
End Try
End If
'Draw the drop-down item - you could easily adapt this to display a font icon, etc
e.DrawBackground()
e.DrawFocusRectangle()
e.Graphics.DrawString(txt, fnt, New SolidBrush(e.ForeColor), e.Bounds.X, e.Bounds.Y)
End If
End Sub
'Go through installed font list and add to ComboBox
Public Sub ReloadFonts()
Me.Items.Clear()
For Each ff As FontFamily In System.Drawing.FontFamily.Families
Me.Items.Add(ff.Name)
Next
End Sub
End Class