Question changing font on a text box

peskykid

Member
Joined
Jul 25, 2010
Messages
8
Programming Experience
1-3
hello all...

I am writing a program in vb.net 2008 ... I want the user to be able to change the font on a different form, however any code that I try and write comes up with the error code property is read only.. I hope I have given sufficient detail... oh one more thing I am trying to use fontdialog from the tool box. Any help would be greatly appreciated..

thanks in advance..
 
Hi again... Thanks for the code snipet... I am having trouble implementing this code...

Here is what I have...

Private Sub ChangeFontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeFontToolStripMenuItem.Click
FontDialog1.ShowApply = True
FontDialog1.ShowColor = True
FontDialog1.ShowEffects = True
FontDialog1.ShowDialog()
End Sub

This allows the user to pull up the font dialog from the menu on the program... and allow them to choose to change the font size, color, style... etc. This will also display an apply button.. I like the apply button because the user can see what the changes will look like before the font dialog closes so if they are not satisfied they can keep changing until they are happy..

This is what I would like to do..

form2.textbox1.fontsize = fontdialog.fontsize
form2.textbox1.forecolor = fontdialog.color
form2.textbox1. ****etc etc etc *****
form2.textbox2.fontsize = fontdialog.fontsize
form2.textbox2.forecolor = fontdialog.color
form2.textbox2. ****etc etc etc *****

There are 10 forms which I would like to be able to apply these changes... thats no problem for me... I dont mind sitting in front of the computer for a few hours..
 
Half way there!!!

OK i got it... now the question is... how to change the color and bold, underline yada yada yada.... Sorry it took so long to get back to you..



VB.NET:
        If dr = Windows.Forms.DialogResult.OK Then
            Dim pos As Integer
            For pos = 0 To Me.Controls.Count - 1

                If TypeOf (Me.Controls(pos)) Is TextBox Then
                    Dim ctrl As TextBox = Me.Controls(pos)
                    ctrl.Font = New Font(FontDialog1.Font.Name, FontDialog1.Font.Size)
                End If
            Next

            For pos = 0 To Form5.Controls.Count - 1
                If TypeOf (Form5.Controls(pos)) Is TextBox Then
                    Dim ctrl As TextBox = Form5.Controls(pos)
                    ctrl.Font = New Font(FontDialog1.Font.Name, FontDialog1.Font.Size)
                End If

            Next

            For pos = 0 To Form6.Controls.Count - 1
                If TypeOf (Form6.Controls(pos)) Is TextBox Then
                    Dim ctrl As TextBox = Form6.Controls(pos)
                    ctrl.Font = New Font(FontDialog1.Font.Name, FontDialog1.Font.Size)
                End If

            Next
            For pos = 0 To Form7.Controls.Count - 1
                If TypeOf (Form7.Controls(pos)) Is TextBox Then
                    Dim ctrl As TextBox = Form7.Controls(pos)
                    ctrl.Font = New Font(FontDialog1.Font.Name, FontDialog1.Font.Size)
                End If

            Next

            For pos = 0 To Form8.Controls.Count - 1
                If TypeOf (Form8.Controls(pos)) Is TextBox Then
                    Dim ctrl As TextBox = Form8.Controls(pos)
                    ctrl.Font = New Font(FontDialog1.Font.Name, FontDialog1.Font.Size)
                End If

            Next

        End If
 
OH BOY!!! I posted this before i checked your code... Yours looks much better... Im trying to check it out now... You can email me at peskykid eighty two at gmail dot com.. make the eighty two 82 and the at an @ and the dot a . Sorry for the crazy way but spam bots i do not like...
 
Thanks for the help!!! YOU ARE AWESOME!!!!!!!

I modified your code a touch to achieve my goal ... here it is...

VB.NET:
 Private Sub ChangeFontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChangeFontToolStripMenuItem.Click
        FontDialog1.ShowColor = True
        FontDialog1.ShowEffects = True
        If FontDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            For Each ctrl As Control In Me.Controls
                If TypeOf (ctrl) Is TextBox Then
                    DirectCast(ctrl, TextBox).Font = FontDialog1.Font
                    DirectCast(ctrl, TextBox).ForeColor = FontDialog1.Color
                End If
            Next
            For Each ctrl As Control In Form5.Controls
                If TypeOf (ctrl) Is TextBox Then
                    DirectCast(ctrl, TextBox).Font = FontDialog1.Font
                    DirectCast(ctrl, TextBox).ForeColor = FontDialog1.Color
                End If
            Next
            For Each ctrl As Control In Form6.Controls
                If TypeOf (ctrl) Is TextBox Then
                    DirectCast(ctrl, TextBox).Font = FontDialog1.Font
                    DirectCast(ctrl, TextBox).ForeColor = FontDialog1.Color
                End If
            Next
            For Each ctrl As Control In Form7.Controls
                If TypeOf (ctrl) Is TextBox Then
                    DirectCast(ctrl, TextBox).Font = FontDialog1.Font
                    DirectCast(ctrl, TextBox).ForeColor = FontDialog1.Color
                End If
            Next
            For Each ctrl As Control In Form8.Controls
                If TypeOf (ctrl) Is TextBox Then
                    DirectCast(ctrl, TextBox).Font = FontDialog1.Font
                    DirectCast(ctrl, TextBox).ForeColor = FontDialog1.Color
                End If
            Next
        End If
 
Your profile is .Net 3.5 and it looks as you are referencing the default form instances, so with a bit of reflection and using the OfType filter code can be simplified:
VB.NET:
For Each formprop In My.Forms.GetType.GetProperties
    Dim form = CType(formprop.GetValue(My.Forms, Nothing), Form)
    For Each tbox In form.Controls.OfType(Of TextBox)()
        tbox.Font = FontDialog1.Font
        tbox.ForeColor = FontDialog1.Color
    Next
Next
If you have multiple form instances and/or only want to apply to open forms you can instead loop My.Application.OpenForms collection.
 

Latest posts

Back
Top