|
-
Sep 21st, 2007, 01:54 PM
#1
Thread Starter
Frenzied Member
[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.
-
Sep 21st, 2007, 08:34 PM
#2
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.
-
Sep 25th, 2007, 08:20 AM
#3
Thread Starter
Frenzied Member
Re: [2005] Document Layout, RichTextBox
 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:
Dim tempRTB As New RichTextBox
tempRTB.Text = "This is a Brand New Document Title" & ControlChars.NewLine
tempRTB.SelectAll()
tempRTB.SelectionAlignment = HorizontalAlignment.Center
tempRTB.SelectionFont = New Font("Tahoma", 10, FontStyle.Bold)
' Add tempRTB to mainRTB
Me.mainRTB.Rtf = tempRTB.Rtf
The first block of code works perfectly. However, I then try adding the next block like this:
vb Code:
' Clear tempRTB
tempRTB.Clear()
' Add more info
tempRTB.Text = "This is the first block of document text."
tempRTB.SelectAll()
tempRTB.SelectionAlignment = HorizontalAlignment.Left
tempRTB.SelectionFont = New Font("Tahoma", 10, FontStyle.Regular)
' Add tempRTB to mainRTB
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.
-
Sep 25th, 2007, 09:46 AM
#4
Thread Starter
Frenzied Member
Re: [2005] Document Layout, RichTextBox
I have tried a lot of stuff including this:
vb Code:
' Add tempRTB to mainRTB
Me.mainRTB.Rtf += tempRTB.Rtf
But nothing seems to work. I must be missing something here.....
-
Sep 25th, 2007, 11:11 AM
#5
Thread Starter
Frenzied Member
Re: [2005] Document Layout, RichTextBox
Last edited by circuits2; Sep 25th, 2007 at 11:17 AM.
-
Sep 25th, 2007, 07:09 PM
#6
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.
-
Sep 26th, 2007, 08:22 AM
#7
Thread Starter
Frenzied Member
Re: [2005] Document Layout, RichTextBox
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|