Results 1 to 3 of 3

Thread: Help formatting content of RichTextBox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2016
    Posts
    4

    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:

    Name:  Capture5.jpg
Views: 513
Size:  9.2 KB

    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

  2. #2
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    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."

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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
  •  



Click Here to Expand Forum to Full Width