[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...
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
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 ?!
Re: [2008] RichTextBox Replace Programmatically
try this:
vb 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}"
Dim startAt As Integer = 0
Do While Me.rtb_cerere.Text.IndexOf(CuvantCheie, startAt) <> -1
Me.rtb_cerere.SelectionStart = Me.rtb_cerere.Text.IndexOf(CuvantCheie, startAt)
Me.rtb_cerere.SelectionLength = CuvantCheie.Length
startAt = Me.rtb_cerere.Text.IndexOf(CuvantCheie, startAt) + CuvantCheie.Length
Me.rtb_cerere.SelectedText = "New Text"
Loop
Me.rtb_cerere.SelectionStart = PozitieCurenta
Me.rtb_cerere.SelectionLength = SelectieCurenta
Me.Focus()
End Sub
Re: [2008] RichTextBox Replace Programmatically
Brilliant .paul.!
Thanks alot for your big help!
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.
Re: [2008] RichTextBox Replace Programmatically
using replace on the rtb.text removes all the text formatting
Re: [2008] RichTextBox Replace Programmatically
if all else fails i should, read the destructions, hmm, i mean instructions :)
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 ?
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 ?
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!