Question ToolStripCombo SelectedItem issue with fonts

gchq

Well-known member
Joined
Dec 14, 2007
Messages
168
Programming Experience
10+
Hi there

I have a ToolStripCombBox that loads a font selection. For whatever reason I can't get it to load the SelectedItem (even though I know it's there).

It's probably something very simple, but I have been over this for days now. Other Comboxes work without any issue whatsoever, so I'm guessing it relates to fonts!

Thanks

Here is the ComboBox

VB.NET:
 'Fonts drop down box
        Dim FontCombo As New ToolStripComboBox
        With FontCombo
            .Name = "FontCombo"
            .DropDownStyle = ComboBoxStyle.DropDownList
            .ToolTipText = "Select the desired font"
        End With
        vLowerToolStrip.Items.Add(FontCombo)
        Dim FontComboContainer As ComboBox = CType(FontCombo.Control, ComboBox)
        With FontComboContainer
            .DropDownWidth = 240
            .Width = 200
            .ItemHeight = 20
            .BindingContext = EditorForm.BindingContext
            .DrawMode = DrawMode.OwnerDrawFixed
        End With
        AddHandler FontComboContainer.DrawItem, AddressOf ComboBox_DrawFont
        AddHandler FontCombo.SelectedIndexChanged, AddressOf SwitchFontFamily
        Dim FF() As FontFamily = FontFamily.Families
        For i As Integer = 0 To FF.Length - 1
            Dim vFont As Drawing.Font = Nothing
            If FF(i).IsStyleAvailable(FontStyle.Regular) Then
                vFont = New System.Drawing.Font(FF(i).Name, FontComboContainer.Font.Size)
            ElseIf FF(i).IsStyleAvailable(FontStyle.Bold) Then
                vFont = New Drawing.Font(FF(i).Name, FontComboContainer.Font.Size, FontStyle.Bold)
            ElseIf FF(i).IsStyleAvailable(FontStyle.Italic) Then
                vFont = New Drawing.Font(FF(i).Name, FontComboContainer.Font.Size, FontStyle.Italic)
            ElseIf FF(i).IsStyleAvailable(FontStyle.Strikeout) Then
                vFont = New Drawing.Font(FF(i).Name, FontComboContainer.Font.Size, FontStyle.Strikeout)
            ElseIf FF(i).IsStyleAvailable(FontStyle.Underline) Then
                vFont = New Drawing.Font(FF(i).Name, FontComboContainer.Font.Size, FontStyle.Underline)
            End If
            If Not vFont Is Nothing Then
                FontComboContainer.Items.Add(vFont)
            End If
        Next
        FontComboContainer.SelectedItem = EditorForm.Font.FontFamily.Name

..and here is the paint event


VB.NET:
Private Sub ComboBox_DrawFont(ByVal sender As Object, ByVal e As DrawItemEventArgs)
        Dim CB As ComboBox = CType(sender, ComboBox)
        If e.Index = -1 OrElse e.Index >= CB.Items.Count Then
            Exit Sub
        End If
        e.DrawBackground()
        If (e.State And DrawItemState.Focus) <> 0 Then
            e.DrawFocusRectangle()
        End If
        Dim vBrush As Brush = New SolidBrush(e.ForeColor)
        e.Graphics.DrawString((CType(CB.Items(e.Index), Font)).Name, (CType(CB.Items(e.Index), Font)), vBrush, e.Bounds)
        If Not vBrush Is Nothing Then
            vBrush.Dispose()
        End If

    End Sub
 
You mean these?
FontComboContainer.Items.Add(vFont)
...
FontComboContainer.SelectedItem = EditorForm.Font.FontFamily.Name
 
Resolved

Hi John

Sleepy head strikes again! I'll blame it on Friday nights

Here is the (dah - obvious) solution

VB.NET:
Dim vCurrentFF As FontFamily = EditorForm.Font.FontFamily
        Dim vNewFont As Drawing.Font = Nothing
        If vCurrentFF.IsStyleAvailable(FontStyle.Regular) Then
            vNewFont = New Drawing.Font(vCurrentFF, FontComboContainer.Font.Size)
        End If
        FontComboContainer.SelectedItem = vNewFont

Thanks for pointing that out!
 
Back
Top