Hi:)
I want to change the formatting of a rich text box's selected text with lines of different sizes. For example if I have written
" I code in VB " (any font, size 72, bold)
on the first line and
" VB is easy " (any font, size 48, Italic)
on the second.
I want to change it when I click a button to make both like:
" I code in VB " (any font, size 72, bold, underline)
on the first line and
" VB is easy " (any font, size 48, Italic, underline)
on the second.

Note that the lines are just examples and the user of my program types them to change them. I am making a different kind of program like MS Word.
PLEASE HELP:)

Edited 4 Years Ago by daniwaber: n/a

.a bit.confusing, though somewhat I have an idea.
To clear this up for me and possibly others, do you want to type in rtb(RichTextBox) and Then change font after?.orElse do you want to always have line.1 w/(any font, size 72, bold) and line.2 w/(any font, size 48, Italic)? orElse btn.Click to change font for those 2lines only?

I want to first type and then adjust the different font sizes for different lines and then format it. Above are just examples.

See if this helps.

Public Class Form1
    Private xnL As String = vbNewLine
    Private myCoolFont1 As New Font("verdana", 23, FontStyle.Bold), myCoolFont2 As New Font("georgia", 33, FontStyle.Italic)

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With RichTextBox1 : .Text &= "line 1" & xnL & "line 2" & xnL & "line 3" : .HideSelection = False : End With
    End Sub
  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With RichTextBox1 : setFont(RichTextBox1, 0, myCoolFont1) : setFont(RichTextBox1, 1, myCoolFont2) : End With
    End Sub
    Private Sub setFont(ByVal selRtb As RichTextBox, ByVal selLine As Integer, ByVal selFont As Font)
        With selRtb
            If .Lines.Length >= selLine Then : Dim iStartIndex As Integer = 0
                For i As Integer = 0 To selLine - 1 : iStartIndex += .Lines(i).Length + 1 : Next
                .Select(iStartIndex, .Lines(selLine).Length) : .SelectionFont = selFont
            End If
        End With
    End Sub
End Class

p.s.am a lil' too busy to add a few comments, though this helped to possibly help you.
http://www.vbforums.com/showpost.php?p=3355847&postcount=3

Edited 4 Years Ago by codeorder: Dim Not Static

i think i mentioned that i am making a program like word. the formatting is done by user to selected text as he wants.

.a bit.confusing, though, see if this helps?.

Public Class Form1
    Private myCoolFont1 As New Font("verdana", 23, FontStyle.Bold), myCoolFont2 As New Font("georgia", 33, FontStyle.Italic)
    Private isFont1 As Boolean = False '// Toggles Font.

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        With RichTextBox1
            If isFont1 Then
                .SelectionFont = myCoolFont2
                isFont1 = False
            ElseIf Not isFont1 Then
                .SelectionFont = myCoolFont1
                isFont1 = True
            End If
        End With
    End Sub

    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        With RichTextBox1 : .HideSelection = False : End With
    End Sub
End Class
This article has been dead for over six months. Start a new discussion instead.