Hello,
I know this is probably simple but I have been racking my brains on it so here you go...
I want to click on a button and make the text in a label bold. How do I do it?
Thanks!
Printable View
Hello,
I know this is probably simple but I have been racking my brains on it so here you go...
I want to click on a button and make the text in a label bold. How do I do it?
Thanks!
Dim myFont As New Font(Label1.Font.Name, Label1.Font.Size, FontStyle.Bold)
Label1.Font = myFont
VB Code:
Label1.Font = New Font(Label1.Font, FontStyle.Bold)
Thanks!
Both of these work great. Can you tell me now how I can apply BOLD and UNDERLINE at the same time? As it is now, I have two buttons and when I click BOLD, it make the text BOLD. When I click UNDERLINE, the text becomes UNDERLINE but the BOLD goes away.
Thanks!
Dim myFont As New Font(Label1.Font.Name, Label1.Font.Size, FontStyle.Bold Or FontStyle.Underline)
Label1.Font = myFont
use this, to bring up the font dialog.
i know next u will ask me how i can change the colour
:D
or strike it
VB Code:
Dim myFontDialog As FontDialog myFontDialog = New FontDialog() If myFontDialog.ShowDialog() = DialogResult.OK Then ' Set the control's font. Label1.Font = myFontDialog.Font End If
Thank you, but I am not using the dialog box. I have created buttons for Bold, Italic, Underline, and Strikeout. The problem I am running into now is that I can only do one effect at a time.
For example, if I click on Bold then I get Bold. If I click on Underline then I get Underline, but the Bold goes away.
The way I want it to work is if I click on Bold, Italic, and Underline, I want the effects to stay. It is the same way the Microsoft Word handles it when you highlight text and then apply the Bold, Italic, etc.....to the text.
I know I can do this by writing a lot of "If's" and "Then's", but is there a somewhat simple solution to this?
Thanks!
try something like this ....
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fs As New FontStyle() fs = FontStyle.Regular Label1.Font = New Font(Label1.Font.Name, Label1.Font.Size, fs) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim fs As New FontStyle() fs = FontStyle.Italic Label1.Font = New Font(Label1.Font.Name, Label1.Font.Size, fs) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim fs As New FontStyle() fs = FontStyle.Bold Or FontStyle.Italic Label1.Font = New Font(Label1.Font.Name, Label1.Font.Size, fs) End Sub
Thank you, but that is pretty much what I have now.
If I code it that way, I have to account for every state of each button. For example, if I want the text Bold, then I click on Bold, but what if I want the text Bold and Italic? Do I have to create a button just for that or should I check the state of the Bold button before pushing the Italic button? Well....you see the problem? I have to check the state of ALL the buttons (Bold, Italic, Underline, Strikeout, Regular) and then make a determination to how the font will look. It can be done of course, but I thought there might be a simple solution.
Basically, I want it to work just like the buttons in Microsoft Word.
Thanks!
Hi.
Try this.
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim fs As FontStyle = Label1.Font.Style fs = fs Xor FontStyle.Bold Label1.Font = New Font(Label1.Font, fs) End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim fs As FontStyle = Label1.Font.Style fs = fs Xor FontStyle.Underline Label1.Font = New Font(Label1.Font, fs) End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim fs As FontStyle = Label1.Font.Style fs = fs Xor FontStyle.Italic Label1.Font = New Font(Label1.Font, fs) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim fs As FontStyle = Label1.Font.Style fs = fs Xor FontStyle.Strikeout Label1.Font = New Font(Label1.Font, fs) End Sub
This will toggle the appropiate effect every time you press a button.
Works like a champ!
Thanks folks!