none
Changing text in richtextbox without affecting formatting/objects RRS feed

  • Question

  • Hello,

    I'm trying to convert selected text in a richtextbox to proper case using the following code, but when I do this, all formatting (incl. different fonts, sizes, etc.) and selected objects are deleted.

    richtextbox.SelectedText = StrConv(richtextbox.SelectedText, VbStrConv.ProperCase)


    Is there a way of preserving objects and formatting while converting to proper case?

    Thanks in advance.


    ---- JDS404 ---- Check out my blog at www.interlinkjds.wordpress.com!

    Thursday, June 20, 2019 6:20 PM

Answers

  • Maybe try changing the text letter-by-letter:

     

    Dim start = richtextbox.SelectionStart
    Dim length = richtextbox.SelectionLength
    Dim new_text = StrConv(richtextbox.SelectedText, VbStrConv.ProperCase)
     
    For i = start To start + length - 1
        richtextbox.SelectionStart = i
        richtextbox.SelectionLength = 1
        richtextbox.SelectedText = new_text.Substring(i - start, 1)
    Next
     
    richtextbox.SelectionStart = start
    richtextbox.SelectionLength = length
     

    • Proposed as answer by Alex Li-MSFT Friday, June 21, 2019 5:00 AM
    • Marked as answer by JDS404 Friday, June 21, 2019 8:57 AM
    Friday, June 21, 2019 4:51 AM

All replies

  • Hello,

    I'm trying to convert selected text in a richtextbox to proper case using the following code, but when I do this, all formatting (incl. different fonts, sizes, etc.) and selected objects are deleted.

    richtextbox.SelectedText = StrConv(richtextbox.SelectedText, VbStrConv.ProperCase)


    Is there a way of preserving objects and formatting while converting to proper case?

    Thanks in advance.


    ---- JDS404 ---- Check out my blog at www.interlinkjds.wordpress.com!

    Hi

    I can't reproduce the effects you are having. I tested using the code below (explicitly exaggerated styles for test) and on a Button click, applied your Proper Case line. Result was as expected and no style elements were altered/lost.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		With RichTextBox1
    			.Text = "This is right for you if you want a short term price fix, and you are happy to pay by Direct Debit or Receipt of Bill.
    
    Full information
    Estimated monthly cost £70.91 That's £850.90 per year - Close
    Standing charge: Gas 25.8195p per day | Elec 23.331p per day
    Unit Rate: Gas 3.62565p per kWh | Elec 16.149p per kWh
    
      1900  03  22   0.0020.64-00.79000.00
      1900  03  23   0.0016.64-00.48000.00
      1900  03  24   0.0007.8100.00004.480
      1900  03  25   0.0011.80000.48004.26
    "
    			.ForeColor = Color.Maroon
    			.BackColor = Color.LightPink
    			.Font = New Font("Arial", 22, FontStyle.Bold)
    		End With
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		With RichTextBox1
    			.SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thursday, June 20, 2019 7:30 PM
  • Hello,

    I'm trying to convert selected text in a richtextbox to proper case using the following code, but when I do this, all formatting (incl. different fonts, sizes, etc.) and selected objects are deleted.

    richtextbox.SelectedText = StrConv(richtextbox.SelectedText, VbStrConv.ProperCase)


    Is there a way of preserving objects and formatting while converting to proper case?

    Thanks in advance.


    ---- JDS404 ---- Check out my blog at www.interlinkjds.wordpress.com!

    Hi

    I can't reproduce the effects you are having. I tested using the code below (explicitly exaggerated styles for test) and on a Button click, applied your Proper Case line. Result was as expected and no style elements were altered/lost.

    Option Strict On
    Option Explicit On
    Public Class Form1
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    		With RichTextBox1
    			.Text = "This is right for you if you want a short term price fix, and you are happy to pay by Direct Debit or Receipt of Bill.
    
    Full information
    Estimated monthly cost £70.91 That's £850.90 per year - Close
    Standing charge: Gas 25.8195p per day | Elec 23.331p per day
    Unit Rate: Gas 3.62565p per kWh | Elec 16.149p per kWh
    
      1900  03  22   0.0020.64-00.79000.00
      1900  03  23   0.0016.64-00.48000.00
      1900  03  24   0.0007.8100.00004.480
      1900  03  25   0.0011.80000.48004.26
    "
    			.ForeColor = Color.Maroon
    			.BackColor = Color.LightPink
    			.Font = New Font("Arial", 22, FontStyle.Bold)
    		End With
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		With RichTextBox1
    			.SelectedText = StrConv(.SelectedText, VbStrConv.ProperCase)
    		End With
    	End Sub
    End Class


    Regards Les, Livingston, Scotland

    Thanks for replying. 

    The problem occurs when the selected text contains different formatting options. For example, if half of the selection was in Arial font, and the other half was in Calibri, the returned proper case text would only be in Arial. 

    How could I prevent this from happening? 


    JDS404

    Thursday, June 20, 2019 8:57 PM
  • Hi

    Sorry, I tried a few things, but they all resulted in same effect.

    Bound to be someone elxe here that knows better!


    Regards Les, Livingston, Scotland

    Thursday, June 20, 2019 9:27 PM
  • Maybe try changing the text letter-by-letter:

     

    Dim start = richtextbox.SelectionStart
    Dim length = richtextbox.SelectionLength
    Dim new_text = StrConv(richtextbox.SelectedText, VbStrConv.ProperCase)
     
    For i = start To start + length - 1
        richtextbox.SelectionStart = i
        richtextbox.SelectionLength = 1
        richtextbox.SelectedText = new_text.Substring(i - start, 1)
    Next
     
    richtextbox.SelectionStart = start
    richtextbox.SelectionLength = length
     

    • Proposed as answer by Alex Li-MSFT Friday, June 21, 2019 5:00 AM
    • Marked as answer by JDS404 Friday, June 21, 2019 8:57 AM
    Friday, June 21, 2019 4:51 AM