CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Apr 2001
    Location
    Vadodara, Gujarat, India
    Posts
    52

    Question How to add formatted text in RichTextBox control?

    I have TabPages, Button and RichTextBox controls on my form. I like to format text and than add to RichTextBox which is placed on TabPage when I click on button.

    Button1_Click event

    RichTextBox1.Text(“This is my first line which I like to add as font size 24”)

    Button2_Click event

    RichTextBox1.AppendText(ControlChars.CrLf & “This is my second line which I like to add as font size 12”)

    Please help to format text and than add to RichTextBox.

    Ajay

  2. #2
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: How to add formatted text in RichTextBox control?

    Quote Originally Posted by Ajay Parmar
    Button1_Click event

    RichTextBox1.Text(“This is my first line which I like to add as font size 24”)

    Button2_Click event

    RichTextBox1.AppendText(ControlChars.CrLf & “This is my second line which I like to add as font size 12”)
    Hello! I'm not sure I understand you properly..
    Can't you do this instead ¿

    Code:
    RichTextBox1.SelectionFont = New Font("Tahoma", 24, FontStyle.Bold)
    You could also use the Font Dialog:
    Code:
    Dim dlgFont As New FontDialog
            If dlgFont.ShowDialog = DialogResult.OK Then Me.Font = dlgFont.Font
    Last edited by HanneSThEGreaT; October 10th, 2005 at 07:57 AM.

  3. #3
    Join Date
    Apr 2001
    Location
    Vadodara, Gujarat, India
    Posts
    52

    Exclamation Re: How to add formatted text in RichTextBox control?

    Thanks Hannes,

    i know command which your say but that is works on alrady entered text. that mean when you have text alrady in RichTextBox you can select it and format it. but i want that when i add my text first time at that time i format text first and than add.

    i mean when i click on button i like to add one line of text with font size 24 and when i click on other button i like to add another line to that same rich text box with font size 12.

    Thank you

    Ajay

  4. #4
    Join Date
    Jun 2005
    Posts
    34

    Re: How to add formatted text in RichTextBox control?

    I realize I'm not going to be of much help. But unless you know the RTF format code chances are you're going to have to format the text after you put it in the RichTextBox.

    One easy way to do this would be...

    Code:
    Dim MyStart as integer
    Dim MyLength as integer
    
    MyStart = RichTextBox.Text.Length 'Do this to get set the cursor location at the end of the RichTextBox.
    MyLength = AppendText.Length
    
    RichTextBox1.AppendText(ControlChars.CrLf & “This is my second line which I like to add as font size 12”)
    
    RichTextBox1.SelectionStart = MyStart
    RichTextBox1.SelectionLength = MyLength
    RichTextBox1.SelectionFont =  MyFont 'Whatever variable or font you want.
    I think that should do the trick. As I know of know way to get pre-formated text into an RTB unless you're using cut and paste.

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

Featured