font style question

steediddydid

New member
Joined
Feb 16, 2006
Messages
2
Programming Experience
Beginner
Anyone have an example of code or could help me in having multiple styles for text in a text box? i need the text to be bold, italic or underlined or any combo of the three. Do i have to use if statements? Thanks.
 
Yes, you have to use If statements.

Use a checkbox for each style you want supported. In each checkbox click event you create a new FontStyle with the combination of the checkboxes that are checked. This FontStyle you then assign the font used by TextBox.

Add a textbox txtInput plus the three checkboxes chkFsItalic, chkFsBold and chkFsUnderLine to your form and use this code example:
VB.NET:
Private Sub chkFsBold_Click(sender, e) Handles chkFsBold.Click
  setfontstyle()
End Sub
 
Private Sub chkFsItalic_Click(sender, e) Handles chkFsItalic.Click
  setfontstyle()
End Sub
 
Private Sub chkFsUnderLine_Click(sender, e) Handles chkFsUnderLine.Click
  setfontstyle()
End Sub
 
Private Sub setfontstyle()
  Dim fs As FontStyle = FontStyle.Regular
  Dim ff As FontFamily = txtInput.Font.FontFamily
  If chkFsBold.Checked = True And ff.IsStyleAvailable(FontStyle.Bold) Then
    fs = fs Or FontStyle.Bold
  End If
  If chkFsItalic.Checked = True And ff.IsStyleAvailable(FontStyle.Italic) Then
    fs = fs Or FontStyle.Italic
  End If
  If chkFsUnderLine.Checked = True And ff.IsStyleAvailable(FontStyle.Underline) Then
    fs = fs Or FontStyle.Underline
  End If
  txtInput.Font = New Font(txtInput.Font, fs)
End Sub
 
Back
Top