|
-
Jan 15th, 2016, 10:49 AM
#1
Thread Starter
New Member
Help formatting content of RichTextBox
Hi,
I have a RichTextBox in my form and I would like the text to be automatically formatted, so that for example the first line is bigger and in a different font. I would also like to add an image if possible.
At the moment this is what I have:

How do I get it to look less crap?
My current code:
Code:
Private Sub ToolStripButtonPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButtonReceipt.Click
Dim headLine As String = "Pete's Awesome Plumbing Company"
RichTextBox1.Text = ""
RichTextBox1.AppendText(vbTab & headLine & vbNewLine)
RichTextBox1.AppendText(vbTab & "Labour: " & Labour_CostTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "Travel: " & Travel_CostTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "Plastic Pipes: " & Plastic_Pipes_CostTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "Copper Pipes: " & Copper_Pipes_CostTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "Chrome: " & Chrome_Pipes_CostTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "Subtotal: " & SubtotalTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "VAT: " & VATTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "Discount: " & DiscountTextBox.Text & vbNewLine)
RichTextBox1.AppendText(vbTab & "Total: " & TotalTextBox.Text & vbNewLine)
End Sub
-
Jan 15th, 2016, 11:25 AM
#2
Re: Help formatting content of RichTextBox
RichTextBox control:
https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx
The documentation indicates how to select and format text.
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jan 15th, 2016, 12:51 PM
#3
Re: Help formatting content of RichTextBox
The trick is to format it as you go.
A while back, I wrote a tool to document table definitions in our system.
Here's some of the methods I wrote to add text and format them...
As I look it over, I realize I probably could refactor that a bit more... but the keys are in grabbing the current position before adding text, adding the text, selecting the text, then applying the formating to the selection... and as a final step, setting the current position to the very end of the RTBox.
Code:
Private Sub AddAssemblyHeader(text As String)
Dim StartPos As Integer = 0
StartPos = RichTextBox1.SelectionStart
RichTextBox1.AppendText(text)
RichTextBox1.SelectionStart = StartPos
RichTextBox1.SelectionLength = text.Length
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font.FontFamily.Name, 10, FontStyle.Bold Or FontStyle.Underline Or FontStyle.Italic)
RichTextBox1.SelectionStart = RichTextBox1.TextLength
End Sub
Private Sub AddTableHeader(tablename As String)
Dim StartPos As Integer = 0
StartPos = RichTextBox1.SelectionStart
RichTextBox1.AppendText(tablename)
RichTextBox1.SelectionStart = StartPos
RichTextBox1.SelectionLength = tablename.Length
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Bold)
RichTextBox1.SelectionStart = RichTextBox1.TextLength
End Sub
Private Sub AddTableDesc(tabledesc As String)
Dim StartPos As Integer = 0
StartPos = RichTextBox1.SelectionStart
RichTextBox1.AppendText(tabledesc)
RichTextBox1.SelectionStart = StartPos
RichTextBox1.SelectionLength = tabledesc.Length
RichTextBox1.SelectionFont = New Font(RichTextBox1.Font, FontStyle.Italic)
RichTextBox1.SelectionStart = RichTextBox1.TextLength
End Sub
Private Sub AddTableField(fielddef As String)
Dim StartPos As Integer = 0
StartPos = RichTextBox1.SelectionStart
RichTextBox1.AppendText(fielddef)
RichTextBox1.SelectionStart = StartPos
RichTextBox1.SelectionLength = fielddef.Length
RichTextBox1.SelectionBullet = True
RichTextBox1.SelectionIndent = 25
RichTextBox1.SelectionStart = RichTextBox1.TextLength
End Sub
Private Sub AddTableFieldValues(valueText As String)
RichTextBox1.AppendText(valueText)
End Sub
Private Sub AddText(textToAdd As String)
RichTextBox1.AppendText(textToAdd)
End Sub
Example of how I call the methods:
Code:
AddTableHeader(t.ToString & Environment.NewLine)
AddTableDesc(t.Description & Environment.NewLine)
AddTableField(F.ToString & Environment.NewLine)
AddTableFieldValues(F.Values)
AddText(TEXTLINE)
AddText(Environment.NewLine)
AddText(Environment.NewLine)
I've taken some IP stuff out... but hopefully you get the idea.
-tg
Tags for this Thread
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
|