Results 1 to 11 of 11

Thread: [2008] RichTextBox Replace Programmatically

  1. #1

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Angry [2008] RichTextBox Replace Programmatically

    I have a RTB that loads data from a file like this in the Load event of the Form2

    Code:
    Me.rtb_cerere.LoadFile(Application.StartupPath & "\ceelceur.rtf")
    After i load this file i want to replace some key words with a specified text but here i'm stuck...

    I use this...

    Code:
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    
            Dim PozitieCurenta As Integer = Me.rtb_cerere.SelectionStart
            Dim SelectieCurenta As Integer = Me.rtb_cerere.SelectionLength
            Dim CuvantCheie As String = "{1}"
    
            Me.rtb_cerere.Select(StartPosition - 1, CuvantCheie.Length)
            Me.rtb_cerere.ScrollToCaret()
            Me.Focus()
    
            Me.rtb_cerere.Rtf = Replace(Me.rtb_cerere.Rtf, Trim(CuvantCheie), Trim("TEST"))
            Me.rtb_cerere.SelectionStart = PozitieCurenta
            Me.rtb_cerere.SelectionLength = SelectieCurenta
            Me.Focus()
    
        End Sub
    How can i replace this {1} with "TEST" ? I have spend 2 days and i'm lost in the space!

    Thanks...
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] RichTextBox Replace Programmatically

    Code:
            Dim testRTB As New RichTextBox
            testRTB.Text = "1234{1}56789abcdefghi{1}jklmnopqrstuvwxyz"
            Debug.WriteLine(testRTB.Text)
            testRTB.Text = testRTB.Text.Replace("{1}", "Test")
            Debug.WriteLine(testRTB.Text)
    debug output

    1234{1}56789abcdefghi{1}jklmnopqrstuvwxyz
    1234Test56789abcdefghiTestjklmnopqrstuvwxyz
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] RichTextBox Replace Programmatically

    Thanks alot!
    It's working but i have another problem now...

    The RTB text loaded from the file is formated and if i use that code the hole text will not keep his properties.

    Any ideea about this ?!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] RichTextBox Replace Programmatically

    try this:

    vb Code:
    1. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    2.     Dim PozitieCurenta As Integer = Me.rtb_cerere.SelectionStart
    3.     Dim SelectieCurenta As Integer = Me.rtb_cerere.SelectionLength
    4.     Dim CuvantCheie As String = "{1}"
    5.  
    6.     Dim startAt As Integer = 0
    7.  
    8.     Do While Me.rtb_cerere.Text.IndexOf(CuvantCheie, startAt) <> -1
    9.          Me.rtb_cerere.SelectionStart = Me.rtb_cerere.Text.IndexOf(CuvantCheie, startAt)
    10.          Me.rtb_cerere.SelectionLength = CuvantCheie.Length
    11.          startAt = Me.rtb_cerere.Text.IndexOf(CuvantCheie, startAt) + CuvantCheie.Length
    12.          Me.rtb_cerere.SelectedText = "New Text"
    13.     Loop
    14.  
    15.     Me.rtb_cerere.SelectionStart = PozitieCurenta
    16.     Me.rtb_cerere.SelectionLength = SelectieCurenta
    17.     Me.Focus()
    18.  
    19. End Sub

  5. #5

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] RichTextBox Replace Programmatically

    Brilliant .paul.!

    Thanks alot for your big help!
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] RichTextBox Replace Programmatically

    Quote Originally Posted by Alexandru_mbm
    Thanks alot!
    It's working but i have another problem now...

    The RTB text loaded from the file is formated and if i use that code the hole text will not keep his properties.

    Any ideea about this ?!
    what do you mean? if you can post what you tried.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] RichTextBox Replace Programmatically

    using replace on the rtb.text removes all the text formatting

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [2008] RichTextBox Replace Programmatically

    if all else fails i should, read the destructions, hmm, i mean instructions
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] RichTextBox Replace Programmatically

    I have tried to take values from another form (Form1 wich is a MDI Child) and use it into another form (Form2 - that uses the RTB code) but it seems it's not working like in VB6 ?

    Code:
    Me.rtb_cerere.SelectedText = frm_eliberare_cerere_certificat_urbanism.txt_nr_cerere.Text
    Or what i am doing wrong ?
    Last edited by Alexandru_mbm; Sep 28th, 2008 at 04:20 PM.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  10. #10

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] RichTextBox Replace Programmatically

    I don't know if it's good or if it's wrong but i used a Public Shared function!

    Anyone can tell me if it's OK with that ?
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

  11. #11

    Thread Starter
    Addicted Member Alexandru_mbm's Avatar
    Join Date
    Jul 2007
    Location
    VBForums.com
    Posts
    157

    Re: [2008] RichTextBox Replace Programmatically

    I have another problem with this code...
    I take values from the Form1 by using

    Code:
     Public Shared SHR_SatSolicitant as String
    
               SHR_SatSolicitant = Me.txt_box1.text
    The problem is when i use the code (down) to replace the string "{13}" if the Form1 SHR_SatSolicitant is EMPTY is not replacing me with "" (empty).

    Code:
            Dim STR_SatSolicitant As String = "{13}"
    
    
                If frm_eliberare_cerere_certificat_urbanism.SHR_SatSolicitant = "" Then
    
                    Do While Me.rtb_cerere.Text.IndexOf(STR_SatSolicitant, startAt) <> -1
                        Me.rtb_cerere.SelectionStart = Me.rtb_cerere.Text.IndexOf(STR_SatSolicitant, startAt)
                        Me.rtb_cerere.SelectionLength = STR_SatSolicitant.Length
                        startAt = Me.rtb_cerere.Text.IndexOf(STR_SatSolicitant, startAt) + STR_SatSolicitant.Length
                        Me.rtb_cerere.SelectedText = Me.rtb_cerere.SelectedText.Replace(Me.rtb_cerere.SelectedText, "")
    
                    Loop
    
                Else
    
                    Do While Me.rtb_cerere.Text.IndexOf(STR_SatSolicitant, startAt) <> -1
                        Me.rtb_cerere.SelectionStart = Me.rtb_cerere.Text.IndexOf(STR_SatSolicitant, startAt)
                        Me.rtb_cerere.SelectionLength = STR_SatSolicitant.Length
                        startAt = Me.rtb_cerere.Text.IndexOf(STR_SatSolicitant, startAt) + STR_SatSolicitant.Length
                        Me.rtb_cerere.SelectedText = ", satul " & frm_eliberare_cerere_certificat_urbanism.SHR_SatSolicitant
                    Loop
    
                End If
    What shold i do ? Thanks again and i'll wait again your big help!
    Last edited by Alexandru_mbm; Sep 29th, 2008 at 03:13 PM.
    I'm still learning VB.NET
    Sorry for my bad english
    Thanks for your help

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