Question Formating Text Box

MJC22

Member
Joined
Apr 6, 2013
Messages
6
Programming Experience
Beginner
Dear all,

How do I reference to textbox from a Module?

I'm trying to write a simple Module that Underlines a text box.

In VBA (MS Access) my code looked something like this:

Routine: Run_Underline (SendFormName as string, SendFieldName as string)

FORM(SendFormName).Form(SendFieldName).Underline = True

End Sub


Many thanks in advance,
MJC
 
Last edited:
Where would that module method be called from? Presumably the form that contains the TextBox that you want to underline the text in. In that case, you would simply pass a reference to the TextBox itself, e.g.
Public Sub UnderlineTextBox(textBox As TextBox)
    textBox.Font = New Font(textBox.Font, textBox.Font.Style Or FontStyle.Underline)
End Sub
From the form you would call it like this:
UnderlineTextBox(Me.TextBox1)
You should also pay close attention to how the text is actually underlined. It's nothing to do with the TextBox itself, but rather the Font assigned to the TextBox's Font property. You need to change the Style property of that Font, but Font object's are immutable, i.e. cannot be changed once they are created. As such, you need to create a new Font object with the desired FontStyles and replace the existing Font with that new one. The Font class provides a constructor specifically for that purpose, which I've used above.

There's no point limiting that method to just TextBoxes though, because the Font property is a member of the Control class, so you may as well accept any control. Also, you may as well write a few similar methods that will allow you to turn all FontStyles (Bold, Italic, Underline and Strikethrough) on or off:
Public Module ControlExtensions

    ''' <summary>
    ''' Turns on the bold style for the font of a control.
    ''' </summary>
    ''' <param name="control">
    ''' The control whose font is to be modified.
    ''' </param>
    Public Sub EnableBold(control As Control)
        EnableFontStyle(control, FontStyle.Bold)
    End Sub

    ''' <summary>
    ''' Turns off the bold style for the font of a control.
    ''' </summary>
    ''' <param name="control">
    ''' The control whose font is to be modified.
    ''' </param>
    Public Sub DisableBold(control As Control)
        DisableFontStyle(control, FontStyle.Bold)
    End Sub

    ''' <summary>
    ''' Toggles the bold style for the font of a control.
    ''' </summary>
    ''' <param name="control">
    ''' The control whose font is to be modified.
    ''' </param>
    ''' <remarks>
    ''' The style is turned on if it is currently off and it is turned off if it is currently on.
    ''' </remarks>
    Public Sub ToggleBold(control As Control)
        ToggleFontStyle(control, FontStyle.Bold)
    End Sub

    ''' <summary>
    ''' Turns on a style for the font of a control.
    ''' </summary>
    ''' <param name="control">
    ''' The control whose font is to be modified.
    ''' </param>
    ''' <param name="style">
    ''' The style to enable.
    ''' </param>
    Public Sub EnableFontStyle(control As Control, style As FontStyle)
        control.Font = New Font(control.Font, control.Font.Style Or style)
    End Sub

    ''' <summary>
    ''' Turns off a style for the font of a control.
    ''' </summary>
    ''' <param name="control">
    ''' The control whose font is to be modified.
    ''' </param>
    ''' <param name="style">
    ''' The style to disable.
    ''' </param>
    Public Sub DisableFontStyle(control As Control, style As FontStyle)
        control.Font = New Font(control.Font, control.Font.Style And Not style)
    End Sub

    ''' <summary>
    ''' Toggles a style for the font of a control.
    ''' </summary>
    ''' <param name="control">
    ''' The control whose font is to be modified.
    ''' </param>
    ''' <param name="style">
    ''' The style to toggle.
    ''' </param>
    ''' <remarks>
    ''' The style is turned on if it is currently off and it is turned off if it is currently on.
    ''' </remarks>
    Public Sub ToggleFontStyle(control As Control, style As FontStyle)
        control.Font = New Font(control.Font, control.Font.Style Xor style)
    End Sub

End Module
You can now call EnableBold, DisableBold or ToggleBold and pass any control you like. You can add similar sets of three methods for FontStyle.Italic, .Underline and .Strikethrough. You can also call the last three methods, to enable and disable multiple styles, e.g.
EnableFontStyle(TextBox1, FontStyle.Bold Or FontStyle.Underline)
That will enable both bold and underline in the TextBox.
 
jmcilhinney,

Thank you for this!!!

Does this code limit me to a single form or can I place the code in a MODULE which can be accessible from any form?

Also, I would like to be able to use a SINGLE line of code for all text boxes etc... When I was programming in access, I would call a routine as follows: Call_Routine Me.Form.name, Me.ActiveControl.name.

Depending on the field that had the focus, I could underline (format) the desired field very simply.

How would I achieve this in VB.net?

Thank you in advance

MJC
 
The fact that the example I posted previously uses a module is string evidence that you can use a module, don't you think? All that code cares about is that it gets passed a control. Where that control comes from is irrelevant to that code, just as a machine that bakes cakes would expect eggs but has no care whatsoever where those eggs come from. You just call it as I showed and pass the control you want altered. In any form you can pass its ActiveControl property and you'll modify the font of the control that currently has focus.
 
Back
Top