FIXED A bug in FontSize function in Visual Basic while developing an office suite

一笑奈何 提交于 2021-02-10 06:12:51

问题


After faffing around with the FontSize function for hours, I am stuck upon a piece code. The font list, whenever I want to define that each font has the same size, it automatically scales it up to 72.

I have browsed the Internet to find out how to get a font list, that worked, but when I added a fontSize to sizecombo combobox it went all berserk.

Imports System.Drawing.Text

Public Class wordking
    Dim fontSize As Integer = 8
    Private Sub wordking_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim myFont As New InstalledFontCollection

        For Each FontName As FontFamily In myFont.Families
            fontcombo.Items.Add(FontName.Name)
        Next
        For fontSize = 8 To 72 Step 2
            sizecombo.Items.Add(fontSize)
        Next
    End Sub
    Private Sub fontcombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles fontcombo.SelectedIndexChanged
        wordbox.SelectionFont = New Font(fontcombo.Text, fontSize, FontStyle.Regular)
    End Sub

    Private Sub sizecombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles sizecombo.SelectedIndexChanged
        Dim currentfontSize As Single = Convert.ToSingle(sizecombo.Text)
        wordbox.SelectionFont = New Font(wordbox.SelectionFont.FontFamily, currentfontSize)
    End Sub

    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        mainmenu.Show()
        Me.Close()
    End Sub
End Class

I expected the function to show a list of fonts, which it did, but it also resizes the font to my maximum value of 72. This bug is ticking me off.


回答1:


Here's some points that might help:

  • To populate the combo boxes:
cmbFont.DataSource = New InstalledFontCollection().Families
cmbFont.DisplayMember = "Name"

For i As Integer = 8 To 72 Step 2
    cmbFontSize.Items.Add(i)
Next

'Assuming the target control is a TextBox:
cmbFont.SelectedIndex = cmbFont.Items.IndexOf(TextBox1.Font.FontFamily)
cmbFontSize.Text = TextBox1.Font.Size.ToString

  • Create a sub to change the font which will be called from the raised events of the combo boxes:
Private Sub SetFont()
    If cmbFont.SelectedItem Is Nothing OrElse String.IsNullOrEmpty(cmbFontSize.Text) Then
        Return
    End If

    Dim fnt As FontFamily = DirectCast(cmbFont.SelectedItem, FontFamily)
    Dim fntSize As Single = Convert.ToSingle(cmbFontSize.Text)
    Dim fntStyle As FontStyle

    For Each fs As FontStyle In [Enum].GetValues(GetType(FontStyle))
        If fnt.IsStyleAvailable(fs) Then
            fntStyle = fs
            Exit For
        End If
    Next

    TextBox1.Font = New Font(fnt, fntSize, fntStyle)
End Sub

Note that, you also need to check the FontStyle of the newly selected font whether the regular style (I see in your code that you prefer the regular style) is available. If not, the first available style will be selected.

  • Handle the fonts SelectedIndexChanged event:
Private Sub cmbFont_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbFont.SelectedIndexChanged
    SetFont()
End Sub
  • I think it's better to handle the TextChanged event of the sizes combo box, so the user can enter a non listed value like 11, 13 ..etc:
Private Sub cmbFontSize_TextChanged(sender As Object, e As EventArgs) Handles cmbFontSize.TextChanged
    SetFont()
End Sub

If you also need to link the combo boxes with the text editor to display the selection font whenever the selection is changed, then you could do:

  • Add a class variable and name it, say;
Private IsSelectionChanged As Boolean = False

This will be used to avoid the redundancies.

  • Add sub to get the font from the selection:
Private Sub GetFont()
    IsSelectionChanged = True

    Dim fnt As Font = If(txtEditor.SelectionFont, txtEditor.Font)

    cmbFont.SelectedIndex = cmbFont.Items.IndexOf(fnt.FontFamily)
    cmbFontSize.Text = fnt.Size.ToString

    IsSelectionChanged = False
End Sub
  • Change the first line in the SetFont sub as follow:
Private Sub SetFont()
    If IsSelectionChanged OrElse cmbFont.SelectedItem Is Nothing OrElse String.IsNullOrEmpty(cmbFontSize.Text) Then
        Return
    End If
'
'
'The rest as the above mentioned...
End Sub
  • In the selection changed event of your editor:
Private Sub txtEditor_SelectionChanged(sender As Object, e As EventArgs) Handles txtEditor.SelectionChanged
    GetFont()
End Sub

That's all. Just tweak it to meet your requirements.

Here's a quick demo:

Good luck.



来源:https://stackoverflow.com/questions/58789041/fixed-a-bug-in-fontsize-function-in-visual-basic-while-developing-an-office-suit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!