Results 1 to 6 of 6

Thread: [RESOLVED] ToUpper destroying Richtextbox format.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    16

    Resolved [RESOLVED] ToUpper destroying Richtextbox format.

    I am using a ToUpper statement on a RichTextBox and I have found it is destroying the color formatting.

    This is the output of the RTB before the ToUpper statement

    Code:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    {\colortbl ;\red255\green255\blue255;\red0\green255\blue0;}
    \viewkind4\uc1\pard\cf1\f0\fs192 There are a great number of things you  \cf2\bullet\cf1  cannot do with a Declare statement. \par
    }
    This is the statement I am using

    Code:
    RichTextBox1.Text = RichTextBox1.Text.ToUpper()
    This is the output after the ToUpper statement

    Code:
    {\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    {\colortbl ;\red255\green255\blue255;}
    \viewkind4\uc1\pard\cf1\f0\fs192 THERE ARE A GREAT NUMBER OF THINGS YOU  \bullet  CANNOT DO WITH A DECLARE STATEMENT. \par
    }
    Does anyone have any thoughts on why this happens and how to solve it?

    Thanks

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    16

    Re: ToUpper destroying Richtextbox format.

    Seems any manipulation of RichTextBax1.Text harms the formatting. Typing in the control works fine but replacing or inserting a character to the RichTextBox1.Text causes formatting to be lost. This makes me wonder if I need to manipulate the RichTextBox1.Rtf somehow. Looking at the value of RichtextBox1.Rtf I see most of the beginning formatting values are within {}. All the other formatting items within the actual text are preceded with a \. If there is a \ in the actual text then the RichTextBox1.Rtf contains \\. If a { is used as part of the actual text the RichTextBox1.Rtf contains a \{. A space seems to indicate the end of a formatting command in the RichTextBox1.Rtf.

    Has anyone tried manipulating the RichTextBox1.Rtf? I hesitate to start down that path if its a dead end.

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

    Re: ToUpper destroying Richtextbox format.

    The RichTextBox does have its limitations as an RTF editor. I suspect that what you might have to do is select the text by portions based on formatting like colour, etc, and then set the SelectedText with the result of ToUpper so as not to disturb the other formatting. I've seen other solutions to RTF editing but none free I'm afraid.
    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

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

    Re: ToUpper destroying Richtextbox format.

    what if you manipulate the .SelectionText rather than the .Text? Would that make a difference? Set the .SelectionStart = 0, .SelectionLength = rtf.Text.Length, .SelectionText = .SelectionText.ToUpper()

    Not sure if it'll work, and don't have a way to test it at the moment. Would be easier than trying to manipulate it from the RTF side though.


    -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??? *

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    16

    Re: ToUpper destroying Richtextbox format.

    I did try using selected text one character at a time. I walked the rtb.text until I found the first lower case character. Selected that character but as soon as I did toupper on selectedtext I lost my font color assignments to other text in the rtb.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Oct 2018
    Posts
    16

    Re: ToUpper destroying Richtextbox format.

    Well strange enough this works. It changes the case to upper in the Richtextbox without destroying the formatting. I have no clue why it works.

    Code:
        Private Sub chkCaps_CheckedChanged(sender As Object, e As EventArgs) Handles chkCaps.CheckedChanged
    
            'ToDo Uppercase all text without harming the formatting.
            If chkCaps.Checked Then
                Dim sTemp As String = RichTextBox1.Rtf
                sTemp = Strings.Right(sTemp, sTemp.Length - 7)
                RichTextBox1.Rtf = "{\rtf1\" & sTemp.ToUpper
            End If
    
        End Sub

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