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...
 
k is an integer and you assign it to a string? You really should program with Option Strict On

Well, I was able to use this (strictly as a test of output), and it worked fine:


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

Dim Offset As Integer = Integer.Parse(TextBox1.Text)
Dim Address As Integer
Dim Value As Integer

For Each line1 As String In RichTextBox1.Lines

Address = Integer.Parse(line1.Substring(0, 8))
Address += Offset
Value = line1.Substring(8, 5)

RichTextBox2.Text = Address & " " & Value

Next line1

End Sub


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

I'd love to do that! I've been hunting for the ability to write line-by-line to an rtb for a while now, to no avail. If you have a clue as to how to do this, please, let me know.
 
Last edited:
Well, I was able to use this (strictly as a test of output), and it worked fine
It's close to what I would do.. You ought to call your variables these:

address
offset
value

Not:

Address
Offset
Value



I'd love to do that! I've been hunting for the ability to write line-by-line to an rtb for a while now, to no avail. If you have a clue as to how to do this, please, let me know.
You'd be better off doing this:

VB.NET:
Dim sb as New StringBuilder(1000)

For ...
  sb.AppendLine(text)
Next

RichTextBox.Text = sb.ToString()
 
I had to use

Dim sb As New System.Text.StringBuilder(1000)

...as otherwise, VS told me that type StringBuilder wasn't defined.

I'll toy with that later today, if I can squeeze it in, heh. Thanks.
 
System.Text is imported by default for me, I'm sure.. But the Error Correction Helper will solve this one easily..
 
Sweet; I figured it out. This works:

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


Dim offset As Integer = Integer.Parse(TextBox1.Text)
Dim address As Integer
Dim value As String


Dim sb As New System.Text.StringBuilder(1000)

For Each line1 As String In RichTextBox1.Lines

address = Integer.Parse(line1.Substring(0, 8))
address += offset
value = line1.Substring(8, 5)

Dim completestring As String = address & " " & value

sb.AppendLine(completestring)

Next line1

RichTextBox2.Text = sb.ToString

End Sub



Now, to convert those integers to hex integers before incrementation.
 
Last edited:
Now, to convert those integers to hex integers before incrementation.

Well, there isnt really such a thing as a hex integer.. You will have to parse the sequence of numbers as though it were in base 16 (look at the Integer.Parse options for that)

And you will have to write it back formatted as a hex number, which is myInteger.ToString("X8") for a padded-out-to-8-wide hex number
 
Ack, I tried doing this:

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

Dim sb As New System.Text.StringBuilder(1000)

For Each line1 As String In RichTextBox1.Lines

Dim offset = Long.Parse(TextBox1.Text, Globalization.NumberStyles.HexNumber)
Dim address = Long.Parse(line1.Substring(0, 8), Globalization.NumberStyles.HexNumber)
Dim value As String

address += offset
value = line1.Substring(8, 5)

sb.AppendLine(address & " " & value)

Next line1

RichTextBox2.Text = sb.ToString

End Sub

But it gave some pretty hilarious results when executed...

Such as 80090001 incremented by 100 = 2148073729...
 
Last edited:
0x80090001 (hex) is 2148073473 (decimal)

Add 100 (dec) to this and you get 2148073573
Add 256 (dec) to this (0x100 hex) and you get your result

What's the problem?
 
Heh, I can't believe I didn't notice that :)

I tried this:

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

Dim sb As New System.Text.StringBuilder(1000)

For Each line1 As String In RichTextBox1.Lines

Dim offset = Integer.Parse(TextBox1.Text, Globalization.NumberStyles.HexNumber)
Dim address = Long.Parse(line1.Substring(0, 8), Globalization.NumberStyles.HexNumber)
Dim value As String

address += offset
address.ToString("X8")
value = line1.Substring(8, 5)

sb.AppendLine(address & " " & value)

Next line1

RichTextBox2.Text = sb.ToString

End Sub

But VS replied with
- Too many arguments to 'Public Overridable Function ToString() As String'.

...in reference to "X8".
 
Turn Option Strict On. Fix all the errors and warnings in your code that arise as a result.

Never program with this off
 
Heh, I did, and I got no errors. Though, it could be because I changed

address.ToString("X8")

...to

address = address.ToString("X8")

Bleh, I still haven't found a way to display that resultant decimal integer in hex...
 
Last edited:
W00t! This worked:

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

Dim sb As New System.Text.StringBuilder(1000)

For Each line1 As String In RichTextBox1.Lines

Dim offset = Integer.Parse(TextBox1.Text, Globalization.NumberStyles.HexNumber)
Dim address = Long.Parse(line1.Substring(0, 8), Globalization.NumberStyles.HexNumber)
Dim value As String

address += offset
value = line1.Substring(8, 5)

sb.AppendLine((Hex$(address)) & " " & value)

Next line1

RichTextBox2.Text = sb.ToString

End Sub

The only problem remaining is that when offset = a negative number, upon executon, I get "input string was not in a correct format". Sometimes, the offset will need to be a negative number (ie, "subtract this much").
 
I'll mess with it a bit later (I'm at work), but I think I'll do something like,

"If

the first character of textbox1.text = "-"

then

make a substring with everything but the first character of textbox1.text, convert it to hex, and subtract that from address

else if

the first character of textbox1.text <> "-"

then take all of textbox1.text, convert it to hex, and add it to address"
 
OK, I figured out a decent way of doing that. The finished product (for this part of the app) is:

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

Dim sb As New System.Text.StringBuilder(1000)

For Each line1 As String In RichTextBox1.Lines

Dim offset As String = TextBox1.Text

If offset.Chars(0) <> "-" Then

offset = Integer.Parse(TextBox1.Text, Globalization.NumberStyles.HexNumber)
Dim address = Long.Parse(line1.Substring(0, 8), Globalization.NumberStyles.HexNumber)
Dim value As String


address += offset
value = line1.Substring(8, 5)

sb.AppendLine((Hex$(address)) & " " & value)



ElseIf offset.Chars(0) = "-" Then

offset = Integer.Parse(TextBox1.Text.Substring(1, (offset.length - 1)), Globalization.NumberStyles.HexNumber)
Dim address = Long.Parse(line1.Substring(0, 8), Globalization.NumberStyles.HexNumber)
Dim value As String




address -= offset
value = line1.Substring(8, 5)

sb.AppendLine((Hex$(address)) & " " & value)

End If


Next line1

RichTextBox2.Text = sb.ToString

End Sub

Thanks a million for the help :)
 

Latest posts

Back
Top