Results 1 to 7 of 7

Thread: [RESOLVED] [2005] Document Layout, RichTextBox

  1. #1

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Resolved [RESOLVED] [2005] Document Layout, RichTextBox

    My exposure to the System.Windows.Forms.RichTextBox has been very minimal, which is to say I have never had a need for it.

    An application I am currently designing requires that I create and display a document to the user. I think the RichTextBox might be the best control to use since I will be extracting data from a DataRow and building the document on the fly.

    I understand that like a TextBox, a RTB has a Text property and an AppendText method. What I don't understand, is how I format the text that I want to add to the RTB. For instance, if I start with a blank RTB, how do I add a Title that is centered? Remember that I am using the RTB to display data as a document (Kind of like MS Word). I read the documentation on MSDN but the only examples I could find simply loaded an existing text file into the RTB.

    Any guidance is appreciated.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

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

    Re: [2005] Document Layout, RichTextBox

    The Text property contains the string you see displayed in the control. The Rtf property contains that plus all the RTF markup code, much like a HTML source file.

    To apply formatting to part of the text in a RichTextBox you first set the SelectionStart and SelectionLength properties such that the text of interest is selected in the control. You then set one or more of the properties whose names start with "Selection", e.g. SelectionAlignment, to apply the formatting you desire.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Document Layout, RichTextBox

    Quote Originally Posted by jmcilhinney
    The Text property contains the string you see displayed in the control. The Rtf property contains that plus all the RTF markup code, much like a HTML source file.

    To apply formatting to part of the text in a RichTextBox you first set the SelectionStart and SelectionLength properties such that the text of interest is selected in the control. You then set one or more of the properties whose names start with "Selection", e.g. SelectionAlignment, to apply the formatting you desire.

    Ok, I started playing around with this a little bit. Since I am adding blocks of text based on the values in a DataTable, then I thought it might be best to format the text before adding it to the main RTB. I tried doing something like this:

    vb Code:
    1. Dim tempRTB As New RichTextBox
    2. tempRTB.Text = "This is a Brand New Document Title" & ControlChars.NewLine
    3. tempRTB.SelectAll()
    4. tempRTB.SelectionAlignment = HorizontalAlignment.Center
    5. tempRTB.SelectionFont = New Font("Tahoma", 10, FontStyle.Bold)
    6.  
    7. ' Add tempRTB to mainRTB
    8. Me.mainRTB.Rtf = tempRTB.Rtf

    The first block of code works perfectly. However, I then try adding the next block like this:

    vb Code:
    1. ' Clear tempRTB
    2. tempRTB.Clear()
    3.  
    4. ' Add more info
    5. tempRTB.Text = "This is the first block of document text."
    6. tempRTB.SelectAll()
    7. tempRTB.SelectionAlignment = HorizontalAlignment.Left
    8. tempRTB.SelectionFont = New Font("Tahoma", 10, FontStyle.Regular)
    9.  
    10. ' Add tempRTB to mainRTB
    11. Me.mainRTB.Rtf = Me.mainRTB.Rtf & tempRTB.Rtf

    I never seem to be able to add to the contents of the mainRTB. Is there something I am missing?
    Last edited by circuits2; Sep 25th, 2007 at 09:47 AM.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  4. #4

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Document Layout, RichTextBox

    I have tried a lot of stuff including this:

    vb Code:
    1. ' Add tempRTB to mainRTB
    2. Me.mainRTB.Rtf += tempRTB.Rtf

    But nothing seems to work. I must be missing something here.....
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  5. #5

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Document Layout, RichTextBox

    Ok, I have a hard time believing that RTB's are this ridiculous by design. I found a work-around to my issue. I can use the copy and paste methods to transfer Rtf data from one RTB to another, but ONLY if they have not been set to ReadOnly.

    vb Code:
    1. tempRTB.SelectAll()
    2. tempRTB.Copy()
    3. mainRTB.DeselectAll()
    4. mainRTB.Paste()

    This can't be the only way to do it.


    Last edited by circuits2; Sep 25th, 2007 at 11:17 AM.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2005] Document Layout, RichTextBox

    I would guess that the RTF code of the first RTB has opening and closing tags, so anything you add to the end of that will be outside the closing tag and therefore not displayed. Like I said, an RTF file is somewhat like an HTML file. If you view an HTML file with two BODY tags then I would guess that only the first one will be rendered. RTF is likely to be the same. I don't know all this for a fact but it's the sort of thing you should be considering. Have you actually looked at the strings you're moving around, or are you just making assumptions about what they contain? If you are using strings and it's not working then if you haven't actually looked at the contents of the strings then you haven't really debugged.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Frenzied Member circuits2's Avatar
    Join Date
    Sep 2006
    Location
    Kansas City, MO
    Posts
    1,027

    Re: [2005] Document Layout, RichTextBox

    Quote Originally Posted by jmcilhinney
    I would guess that the RTF code of the first RTB has opening and closing tags, so anything you add to the end of that will be outside the closing tag and therefore not displayed. Like I said, an RTF file is somewhat like an HTML file. If you view an HTML file with two BODY tags then I would guess that only the first one will be rendered. RTF is likely to be the same. I don't know all this for a fact but it's the sort of thing you should be considering. Have you actually looked at the strings you're moving around, or are you just making assumptions about what they contain? If you are using strings and it's not working then if you haven't actually looked at the contents of the strings then you haven't really debugged.
    The opening and closing tags sound like they could be the culprit. I did check the RTF code for the contents that I moved around (I try not to assume anything). However, when I checked the RTF code, I didn't think about the opening and closing tags being an issue. That would explain why a copy and paste works.

    To test this theory, I should be able to insert any new information before the closing ControlWord but after any other ControlWords for the previous block. I'll try that to see if it works. I'm surprised that such a simple action wasn't built into the RTB. I understand that the AppendText was carried over from the TextBox inheritance, but they should have created an AppendRtf for similar functionality. Maybe a custom RTB would be a good project for later.
    Show the love! Click (rate this post) under my name if I was helpful.

    My CodeBank Submissions: How to create a User Control | Move a form between Multiple Monitors (Screens) | Remove the MDI Client Border | Using Report Viewer with Visual Studio 2012 Express

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