I am trying to build an application that has some of the functionality of Word: the ability to change font color, font size, etc. of certain lines of text while leaving others alone. Any ideas of what control would be good for this? Thanks.
Jay
Printable View
I am trying to build an application that has some of the functionality of Word: the ability to change font color, font size, etc. of certain lines of text while leaving others alone. Any ideas of what control would be good for this? Thanks.
Jay
It's very simple to do using the Rich Text Box control and the Common Dialog. Looks a little something like this...
Private Sub mnuFont_Click()
On Error GoTo cancelerror
'Setup and show font dialog
CommonDialog.FontName = currentRTF.SelFontName
CommonDialog.FontSize = currentRTF.SelFontSize
CommonDialog.FontBold = currentRTF.SelBold
CommonDialog.FontItalic = currentRTF.SelItalic
CommonDialog.FontUnderline = currentRTF.SelUnderline
CommonDialog.FontStrikethru = currentRTF.SelStrikeThru
CommonDialog.Color = currentRTF.SelColor
CommonDialog.Flags = cdlCFScreenFonts Or cdlCFPrinterFonts Or cdlCFEffects
CommonDialog.ShowFont
'Apply new font to text
currentRTF.SelFontName = CommonDialog.FontName
currentRTF.SelFontSize = CommonDialog.FontSize
currentRTF.SelBold = CommonDialog.FontBold
currentRTF.SelItalic = CommonDialog.FontItalic
currentRTF.SelUnderline = CommonDialog.FontUnderline
currentRTF.SelStrikeThru = CommonDialog.FontStrikethru
currentRTF.SelColor = CommonDialog.Color
Exit Sub
cancelerror:
'User canceled common dialog. Do nothing.
End Sub
And of course you can add fonts to a combo box and put it on the toolbar, and a color dropdown, and toolbar buttons for bold, italic etc.
Private Sub tlbFont_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Key
Case "BOLD" 'Toggle bold on text
If Button.Value = tbrPressed Then
currentRTF.SelBold = True
Else
currentRTF.SelBold = False
End If
Case "UNDERLINE" 'Toggle underline on text
If Button.Value = tbrPressed Then
currentRTF.SelUnderline = True
Else
currentRTF.SelUnderline = False
End If
Case "ITALIC" 'Toggle italic on text
If Button.Value = tbrPressed Then
currentRTF.SelItalic = True
Else
currentRTF.SelItalic = False
End If
'etc etc etc
End Sub
And then in the Rich Text Box's Change, Click, and KeyPress,events, call some function to update your controls.
Private Sub UpdateRTFControls()
With Toolbar1
If currentRTF.SelBold = True Then .Buttons("BOLD").Value = tbrPressed
If currentRTF.SelBold = False Then .Buttons("BOLD").Value = tbrUnpressed
If currentRTF.SelUnderline = True Then .Buttons("UNDERLINE").Value = tbrPressed
If currentRTF.SelUnderline = False Then .Buttons("UNDERLINE").Value = tbrUnpressed
If currentRTF.SelItalic = True Then .Buttons("ITALIC").Value = tbrPressed
If currentRTF.SelItalic = False Then .Buttons("ITALIC").Value = tbrUnpressed
If currentRTF.SelAlignment = 0 Then .Buttons("LEFT").Value = tbrPressed
If currentRTF.SelAlignment = 1 Then .Buttons("RIGHT").Value = tbrPressed
If currentRTF.SelAlignment = 2 Then .Buttons("CENTER").Value = tbrPressed
'etc etc etc
End With
End Sub
Good luck :)