Results 1 to 12 of 12

Thread: [RESOLVED] Keep Formatting From One RichTextbox To Another RichTextBox?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Resolved [RESOLVED] Keep Formatting From One RichTextbox To Another RichTextBox?

    Working on a chat/messaging program where I have a richtextbox and I have formatting options like bold, underline, italic, font colors and so on but I need to keep the formatting upon pressing the command button that sends the formatted text to the second richtextbox window.. it is not working the way I have it now:

    Code:
    RichTextBox1.AppendText(RichTextBox2.Text & vbCrLf)

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    vb.net Code:
    1. 'Position the caret at the end of the destination control.
    2. RichTextBox1.SelectionStart = RichTextBox1.TextLength
    3.  
    4. 'Get the formatted text from the source and insert it into the destination.
    5. RichTextBox1.SelectedRtf = RichTextBox2.Rtf
    6.  
    7. 'Append a line break.
    8. RichTextBox1.AppendText(ControlChars.Lf)
    You have to work with the formatted text, not the plain text, and you have to insert formatted text at the end rather than appending it.

    Also note that a RichTextBox uses line feeds alone as line breaks, rather than carriage return and line feed pairs. I've never looked into it but I assume that that is part of the RTF specification. The control does understand both but it's best to stick with the standard.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    No it is not working at all for some reason, no text is being transferred to the second richtextbox..

    You have to work with the formatted text, not the plain text, and you have to insert formatted text at the end rather than appending it.
    Also note that a RichTextBox uses line feeds alone as line breaks, rather than carriage return and line feed pairs. I've never looked into it but I assume that that is part of the RTF specification. The control does understand both but it's best to stick with the standard.
    Thanks ok I need to read up on all kinds of stuff just started buying some books.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    ok for some reason the text is not showing at all when I click the command button, it is only adding a blank space..

    This is what I am using in the button:

    Code:
    'Position the caret at the end of the destination control.
    ChatWindow.SelectionStart = ChatWindow.TextLength
    'Get the formatted text from the source and insert it into the destination.
    ChatEdit.SelectedRtf = ChatWindow.SelectedRtf
    'Append a line break.
    ChatWindow.AppendText(ControlChars.Lf)

  5. #5
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,140

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    Here's the documentation on the RichTextBox Class. Take a look through the methods and properties.

    https://docs.microsoft.com/en-us/dot...rk-4.8#methods

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    You're just supplying the text start. You need to supply the text length too, before using SelectedRtf

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    I am not exactly sure what you mean, the example says textlength before the selectedrtf. I just need it to use all the formatted text in the ChatEdit (RichTextBox) and send it formatted to the ChatWindow (RichTextBox)

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    Quote Originally Posted by DreamWarrior77 View Post
    ok for some reason the text is not showing at all when I click the command button, it is only adding a blank space..

    This is what I am using in the button:

    Code:
    'Position the caret at the end of the destination control.
    ChatWindow.SelectionStart = ChatWindow.TextLength
    'Get the formatted text from the source and insert it into the destination.
    ChatEdit.SelectedRtf = ChatWindow.SelectedRtf
    'Append a line break.
    ChatWindow.AppendText(ControlChars.Lf)
    Goodness me. Read my code properly. I even explain it in the comments! Here's my code:
    Code:
    'Position the caret at the end of the destination control.
    RichTextBox1.SelectionStart = RichTextBox1.TextLength
     
    'Get the formatted text from the source and insert it into the destination.
    RichTextBox1.SelectedRtf = RichTextBox2.Rtf
     
    'Append a line break.
    RichTextBox1.AppendText(ControlChars.Lf)
    and here's your code:
    Code:
    'Position the caret at the end of the destination control.
    ChatWindow.SelectionStart = ChatWindow.TextLength
    'Get the formatted text from the source and insert it into the destination.
    ChatEdit.SelectedRtf = ChatWindow.SelectedRtf
    'Append a line break.
    ChatWindow.AppendText(ControlChars.Lf)
    Spot the difference. In my code, RichTextBox1 is the destination and RichTextBox2 is the source. Which of your controls is the destination and which is the source? You're inserting text FROM ChatWindow TO ChatEdit, which I suspect is backwards. If it's not backwards then why are you using ChatWindow everywhere else, including appending the line break?

    Also, why are you using SelectedRtf twice when in the middle line? That is only going to work if you have first selected some text in the source and you will then insert only that selected part of the text into the destination. If you want to get all the text from the source then you should be using its Rtf property, as I do in my code, rather than the SelectedRtf property. That's what .paul. was referring to, I would think. If you really do only want to get part of the text from the source then you need to set SelectionStart AND SelectionLength in order to do so. I doubt that's what you're actually trying to achieve though.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: Keep Formatting From One RichTextbox To Another RichTextBox?

    I'm sorry I get confused so easily a lot of times. It seems that I got mixed up with my names of the richtextboxes that I had and then to make matters worse when that did not work since I mixed up the richtextbox names.. I accidentally changed the .rtf to.selectedrtf.. big mess on my part due to confusion.
    Anyways Thanks it was syntax errors on my part, it works just fine now.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: [RESOLVED] Keep Formatting From One RichTextbox To Another RichTextBox?

    All's well that ends well.

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2018
    Posts
    395

    Re: [RESOLVED] Keep Formatting From One RichTextbox To Another RichTextBox?

    Awesome Yes, I am very happy this works now.. I was hoping to get this working for 2 days. Just putting one foot in front of the other getting this project done one step at a time.
    Thanks for all your help. Got my book on network programming today.

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: [RESOLVED] Keep Formatting From One RichTextbox To Another RichTextBox?

    Quote Originally Posted by DreamWarrior77 View Post
    Got my book on network programming today.
    Get back to us when you get to the chapter about chanting in dead languages and sacrificing chickens. That may be more applicable to network engineering but anything to do with networks is always fun.

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