Hello everybody. I am struggling with this issue, which is probably something simple. How do you change the font style and font size while a VB application is running? I want to to be able to choose a different font and size based on choices from the ToolStripComboBoxe(s). Any help would be greatly appreciated. Thanks

Craig

Recommended Answers

All 10 Replies

Dim FontName As String = "Arial"
Dim FontSize As Integer = 14
Dim FS As New Font(FontName, FontSize, FontStyle.Regular)
Me.Font = FS

After a little monkeying around with what you provided and what I had, I got the form to change font. Thank you very much. Can I assume that your code (modified a little) can be used to change the font size also? Thanks again. That little snippet of code was 4 hours of internet searching!

Sure. If you have a combobox full of different size-values, you can use those to set the size of the font. Dim FontSize As Integer = Cint(ComboSizes.Text)

One last question. Is there a way to have these modifications apply to an entire application and not just to the active form? Thanks again for ALL the help.

I'm thinking that's it's possible.
And I'm sure there is a very clever way of managing that.
That said, this is how I would do it:

Dim fontName As String = "Arial"
        Dim fontSize As Integer = 14
        Dim fontStyle As System.Drawing.FontStyle = Drawing.FontStyle.Regular
        Dim FS As New Font(fontName, fontSize, fontStyle)

        For Each frm As Form In Application.OpenForms
            frm.Font = FS
            frm.Invalidate()  '' Causes a repaint of the form to make sure that the change goes through.
        Next

Wow, you have been a lifesaver. Is there a way to change the background color just like the font was changed? I do not see any choices to change to except one color at a time. Better put, is there a color family to choose from just like there was a font family to choose from?

No, there's no Color family.
But there is a possibility for choosing colors, one at a time.
You can use a ColorDialog.

If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    Dim c As Color = ColorDialog1.Color
    form.BackColor = c
End If

Or to put it the codesample I provided earilier:

Dim c As Color = System.Drawing.SystemColors.Control
        If ColorDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            c = ColorDialog1.Color
        End If

        Dim fontName As String = "Arial"
        Dim fontSize As Integer = 14
        Dim fontStyle As System.Drawing.FontStyle = Drawing.FontStyle.Regular
        Dim FS As New Font(fontName, fontSize, fontStyle)

        For Each frm As Form In Application.OpenForms
            frm.BackColor = c
            frm.Font = FS
            frm.Invalidate()  '' Causes a repaint of the form to make sure that the change goes through.
        Next

This just keeps going! Ok, now that I have the color chosen from the ColorDialog and it has been applied to the forms, how do I get a color that is slightly darker (if a lighter color was chosen) or slightly lighter (if a darker color was chosen) for the various backcolors? i.e. if somebody chose blue, I want to automatically change the various backcolors to light blue so that the textboxes, listboxes, etc stand out more. This is a tall order and I greatly appreciate any help on this!

Or better yet, just change the brightness, if that's possible?!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.