0

I can grab every 2 chars from sum2.text in order (102030) i get 10,20,30 but my issue is, selecting exactly those numbers 10,20,30 in order my current code below outputs: msgbox(10) msgbox(20) msgbox(30) but wont select and replace those exact numbers in order one by one

My code:

            For i = 0 To sum2.Text.Length - 1 Step 2 'grabs every 2 chars
            Dim result = (sum2.Text.Substring(i, 2)) 'this holds the 2 chars
            MsgBox(result) 'this shows the 2 chars
            sum2.SelectionStart = i 'this starts the selection at i                                                                            
            sum2.SelectionLength = 2 'this sets the length of selection (i)                                     
            If sum2.SelectedText.Contains("10") Then 
                sum2.SelectedText = sum2.SelectedText.Replace("10", "a")
            End If
            If sum2.SelectedText.Contains("20") Then
                sum2.SelectedText = sum2.SelectedText.Replace("20", "b")
            End If
            If sum2.SelectedText.Contains("30") Then
                sum2.SelectedText = sum2.SelectedText.Replace("30", "c")
            End If

my probolem is that it will show the numbers in sum2 one by one correctly, but it would select and replace at all or one by one. I believe the issue is with the selection length

alan7811
  • 65
  • 1
  • 8
  • Please explain what is the expected output given the string "10203077328732" – Steve Jun 26 '17 at 17:52
  • dont worry about the line: sum2.SelectedText = sum2.SelectedText.Replace("10", "a"), i just need help with selecting every two characters in 10203077328732 and then doing something with those two characters until the end of the string – alan7811 Jun 26 '17 at 18:00
  • After Edit: No, as I stated in my answer, it's because you are altering the length of text in the sum2 (textbox) while the loop is running. Big no no as you are altering the size of a collection while iterating through it. Did my answer not work for you? – Charles May Jun 26 '17 at 19:19
  • OK, I edited my answer to use your controls. This should now update your textbox when the loop is finished. – Charles May Jun 26 '17 at 19:24

2 Answers2

0

Here's some code to get you started:

      For i = 0 To sum2.SelectedText.Length - 1 Step 2
         MessageBox.Show(sum2.SelectedText.Substring(i, 2))
      Next
Brian M Stafford
  • 8,483
  • 2
  • 16
  • 25
  • For i = 0 To sum2.Text.Length - 1 Step 2 'loop every 2 chars Dim r = (sum2.Text.Substring(i, 2)) 'get result of 2 chars sum2.Select(r, 2) 'select those 2 chars in r *i cant seem to select the 2 chars from r – alan7811 Jun 26 '17 at 18:17
  • are you wanting the result of `10203077328732` to become `a203077328732` – Charles May Jun 26 '17 at 18:30
  • yes, but also replace 20 with b 30 with c etc, so i need help SELECTING every 2 characters. I use sum2.select(i, 2) but it wont work. – alan7811 Jun 26 '17 at 18:35
0

OK, here's my attempt from what I'm understanding you are wanting to do. The problem is, you are trying to alter the string that the loop is using when you replace "10" with "a" so you need to create a variable to hold your newly built string.

    Dim part As String = ""
    Dim fixed As String = ""
    For i = 0 To Sum2.SelectedText.Length - 1 Step 2
        part = Sum2.SelectedText.Substring(i, 2)
        Select Case part
            Case "10"
                part = part.Replace("10", "a")
            Case "20"
                part = part.Replace("20", "b")
            Case "30"
                part = part.Replace("30", "c")
        End Select
        fixed &= part
    Next

    Sum2.SelectedText = fixed

Of course, this is only to show the workings of moving through the string and changing it. You would need to replace your selected text with the newly formatted result (fixed in this case)

Result: ab3077328732

Also, just so you know, if this format was such that no 2 digits would interfere, you could simply do a

sub2.selectedtext.replace("10", "a").Replace("20", "b").Replace...

However if you had 2 digits like 11 next to 05 it would fail to give desired results because if would change 1105 to 1a5. Just something to think about.

Charles May
  • 1,725
  • 1
  • 11
  • 18
  • how do i make a response like yours where i can clearly write the code in code form – alan7811 Jun 26 '17 at 18:58
  • You can edit your question with context as to what the new code represents. Comments are not made to format code but questions and answers are. Just don't use answers on your own questions to relay more information. Edit your original question. – Charles May Jun 26 '17 at 19:00
  • ok im going to show you what i have in my code so far and what needs tweaked – alan7811 Jun 26 '17 at 19:04
  • ok so 1st the probolem you mentioned with 1105 to 1a5 thats what im trying to avoid, so instead of loop through all "10" in textbox, i only want to do one by one in order not all found instances. Also your updated code is good, but theres still an issue. its not replacing any numbers to letters because i havent selected any text, which is what idk how to do. – alan7811 Jun 26 '17 at 19:50
  • ok, then just change all occurrances of .SelectedText to .Text if you're wanting the contents of the textbox. I was under the impression that you were selecting an area of text and processing it. – Charles May Jun 26 '17 at 19:53
  • thank you, sorry it was a hassle. Im going to post another question right now about how to insert a space into a specific position in a string like insert a space at the 5th position in 123456789 = 1234 56789, so if you want a easy cred im posting now. – alan7811 Jun 26 '17 at 20:14