ReadLine in rich text box?

LBGSHI

Member
Joined
May 9, 2007
Messages
21
Programming Experience
Beginner
Is there an equivalent to ReadLine for rich text boxes? I'm trying to take an input like this:

12345678 9123
45678912 0349
43536363 8383
26736457 9039
37474589 8484

...then add a numeric offset to each line, ignoring everything but the first eight characters of each line, then output each line to a second rich text box, readding those last five characters to each line (" 9123", for example). For example, if the input was what I have above, and the offset was 1, the output would be

12345679 9123
45678913 0349
43536364 8383
26736458 9039
37474590 8484

Does anyone know how I might accomplish this? To compound the matter, I'm going to be doing all of this in hexadecimal, but I think I can figure the conversions out myself, given a good method to read line by line...
 
richTextBox has a .Lines property returning an array of strings representing.. well.. the lines in the textbox ;)

youll have to parse the first block into a number, add one than tostring it back. You mentioned hex but the additions you performed:

37474589

37474590

are definitely base 10 math
 
Right; I used decimal numbers to explain the concept, and keep things simple.

So, something like:

VB.NET:
dim OriginalLine1 As String
dim ResultantLine1 As String 
OriginalLine1 = RichTextBox1.Lines(0)
ResultantLine1 = OriginalLine1.Substring(0,8)
'(and so on...)
...would grab the first eight characters of each line, and throw them into the string ResultantLineX?
 
Last edited by a moderator:
Youre starting to not make sense

I was thinking more

VB.NET:
For Each line as String in myRTB.Lines
  Integer i = Integer.Parse(line.Substring(0,8))
  i += 1
  line = i.ToString("D8") & line.Substring(10)
Next Line
 
That aside, with a button to control the operation, and a box to enter an offset in, I used this:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
Dim Offset As Integer = Int32.Parse(TextBox1.Text)
 
 
For Each line As String In RichTextBox1.Lines
Dim i As Integer
i = Integer.Parse(line.Substring(0, 8))
i += Offset
line = i.ToString("D8") & line.Substring(10)
Dim k As Integer
k = Integer.Parse(line.Substring(7, 4))
 
RichTextBox2.Text = i & " " & k
Next line
 
End Sub
...which worked almost perfectly as a test.

The only problem I encountered was that it seems to mangle the first of those last four characters off and on.

Could I use a similar system to output to each corresponding line of my second rtb?
 
Last edited by a moderator:
Hmm...I don't understand this line:

line = i.ToString("D8") & line.Substring(10)

From whence is D8 derived?

Look at the help for .ToString() and you will find out.

D8 is the format specifier for 8 digits

123.ToString("D8")
--> "00000123"
 
The only problem I encountered was that it seems to mangle the first of those last four characters off and on.

Define "mangle"

Note, that I'm not here to do your work for you.. Code I post usually contains a small bug somewhere that is deliberate, to encourage you to think about what youre doing and what the code does

Could I use a similar system to output to each corresponding line of my second rtb?
certainly
 
cjard said:
Look at the help for .ToString() and you will find out.

D8 is the format specifier for 8 digits

123.ToString("D8")
--> "00000123"

I figured it'd be something along those lines; thanks plenty.

As far as "mangling" the last four characters, it's actually only changing that first character, in a strange way I haven't picked up on yet.

cjard said:
Note, that I'm not here to do your work for you.. Code I post usually contains a small bug somewhere that is deliberate, to encourage you to think about what youre doing and what the code does

Heh, I didn't expect you to do the work for me; I was just curious concerning this one subject, which I couldn't find much info on around the 'net. Your help is greatly appreciated...I suppose I'll go look for the possibly intentional bug ;)
 
This worked a little better:

VB.NET:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
 
Dim Offset As Integer = Int32.Parse(TextBox1.Text)
 
 
For Each line As String In RichTextBox1.Lines
Dim i As Integer
i = Integer.Parse(line.Substring(0, 8))
i += Offset
Dim k As Integer
k = Integer.Parse(line.Substring(8, 5))
 
RichTextBox2.Text = i & " " & k
Next line
 
End Sub
Though, I know it's a slightly more lazy way of going about things...and, I can't grab these variables outside of this. I'll have to think of a way around that.
 
Last edited by a moderator:
I'd like to use something like this:

For Each line As String In RichTextBox2.Lines
line = i & " " & k


...inside of the other For loop, but I just get this error: Variable 'line' hides a variable in an enclosing block.

Apparently, I can't reuse "line" as such (the error references "For Each line")...lame.
 
Last edited:
I believe its a limitation of VB, that within a method you cannot have two ForEach with the same variable:
VB.NET:
Sub Whatever()
 
For Each s AS String
 ...
Next s
 
For Each s As String
 ...
Next s
 
End Sub

It's lame, but just change the name of the var


VB.NET:
Sub Whatever()
 
For Each s AS String
 ...
Next s
 
For Each t As String
 ...
Next s
 
End Sub

Note, you dont do anything with K, so why bother parsing it to an integer?
 
As far as parsing k to an integer, I assumed VB would want the entire contents of an rtb to be in the same format...now that I think about it, that makes no sense, since I also used " ", heh.

Sub Whatever()

For Each s AS String
...
Next s

For Each t As String
...
Next s

End Sub

You mean "Next t", right?

I tried this, but it failed to output to rtb2...

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim Offset As Integer = Int32.Parse(TextBox1.Text)
Dim i As Integer
Dim k As Integer

For Each line1 As String In RichTextBox1.Lines

i = Integer.Parse(line1.Substring(0, 8))
i += Offset
k = line1.Substring(8, 5)

Next line1

For Each line2 As String In RichTextBox2.Lines
line2 = i & " " & k

Next line2


It also seems logical that the second For...Next loop would go inside the first one, but that failed as well...
 
Last edited:
You mean "Next t", right?
I did.. sorry. Shot by my own cut and paste!

I tried this, but it failed to output to rtb2...
k is an integer and you assign it to a string? You really should program with Option Strict On

It also seems logical that the second For...Next loop would go inside the first one, but that failed as well...
It actually seems more logical (to me) that if youre scanning TB1 and putting into TB2, then you should clear TB2 before you start, then as you scan TB1, append lines of output to TB2
 
Back
Top