Results 1 to 2 of 2

Thread: How do you programtically underline text in a normal textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2007
    Location
    Toronto, ON
    Posts
    117

    How do you programtically underline text in a normal textbox

    How would i, in runtime, set a textbox's underline font property to true? I tried:
    Code:
    txt.Font.Underline = True
    but this property seems to be readonly for some reason.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How do you programtically underline text in a normal textbox

    Font objects are immutable, i.e. once you create one you cannot change it. If you want to change a font you have to create a new one and assign it to the relevant property. Because changing a font style is a common operation there is a constructor provided just for the occasion:
    vb Code:
    1. 'Apply the underline style.
    2. Me.TextBox1.Font = New Font(Me.TextBox1.Font, _
    3.                             Me.TextBox1.Font.Style Or FontStyle.Underline)
    4.  
    5. 'Remove the underline style.
    6. Me.TextBox1.Font = New Font(Me.TextBox1.Font, _
    7.                             Me.TextBox1.Font.Style And Not FontStyle.Underline)
    8.  
    9. 'Toggle the underline style.
    10. Me.TextBox1.Font = New Font(Me.TextBox1.Font, _
    11.                             Me.TextBox1.Font.Style Xor FontStyle.Underline)
    Note that after the first expression the text will be underlined no matter what it was before, after the second it will not be underlined no matter what it was before, while after the third it will be the opposite of what it was before.

    Note also that this code affects the Underline style without affecting any other styles. That's why you use the existing Style property of the font.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width