Reputation: 364
I need to put my extracted text (using regular expression) in a TextBox
, instead of a MessageBox
.
This is my current code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim source As String
Using wc As New WebClient()
source = wc.DownloadString("http://www.twstats.com/en71/index.php?page=rankings&mode=players")
End Using
Dim mcol As MatchCollection = Regex.Matches(source, "page=player&id=\d+"">(.+)</a>")
For Each m As Match In mcol
MessageBox.Show(m.Groups(1).Value)
Next
End Sub
Now I need to add the text displayed in the MessageBox
in a TextBox
.
How can I do this?
EDIT:
If I use a TextBox
in the loop instead of the MessageBox
it shows only the last extracted value.
Upvotes: 1
Views: 703
Reputation: 7830
You need to save your intermediate string into a variable. When adding strings one to each other, a good choice is the the StringBuilder class, that .NET offers. The operation is called string concatenation
- it could be used to expand the same string with new content dynamically.
A possible solution could look like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim source As String
Using wc As New WebClient()
source = wc.DownloadString("http://www.twstats.com/en71/index.php?page=rankings&mode=players")
End Using
' save temporarily the different strings
Dim sb as StringBuilder = new StringBuilder()
'alternative
'Dim output as String = String.Empty;
Dim mcol As MatchCollection = Regex.Matches(source, "page=player&id=\d+"">(.+)</a>")
For Each m As Match In mcol
'MessageBox.Show(m.Groups(1).Value)
' add every line to the "output"
sb.AppendLine(m.Groups(1).Value)
'output = output + Environment.NewLine + m.Groups(1).Value
Next
' show the output = all lines
textBox.Text = sb.ToString()
'textBox.Text = output
End Sub
Rename the textBox
with your variable name. It could be also a RichTextbox
control. I have added also a second variant using just a string variable to achieve the desired result. You can choose one of the implementations.
Upvotes: 5