How do I do this in the string (to display the values of the first line if txtboxintdraws.lines (1) is the value - 2-3-4-6-7-8, in textbox1.Lines1 () to display 2,4,6,8, and in textbox2.text - 3,7. (separated by a comma)
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim number As Integer
Dim numbers As New List(Of Integer)
For Each strLine As String In TxtBoxIntDraws.Lines
Dim nums() As String = strLine.Split(","c)
For Each num As String In nums
If Integer.TryParse(num.Trim, number) Then
numbers.Add(number)
End If
Next
Next
numbers.Sort()
'When you are building strings use a StringBuilder to avoide creating and throwing away
'a bunch of strings. (happens every time you alter a string
Dim TextBox1sb As New StringBuilder
Dim TextBox2sb As New StringBuilder
For Each intNumber As Integer In numbers
'The Mod operator divides the first number by the second number
'and returns the remainder
If intNumber Mod 2 = 0 Then '(number / 2) = Int(number / 2) Then
'Number is even
TextBox1sb.AppendLine(intNumber.ToString)
TextBox1.Text = TextBox1.Text + intNumber.ToString
Else
TextBox2sb.AppendLine(intNumber.ToString)
TextBox2.Text = TextBox1.Text + intNumber.ToString
End If
Next
'Update the UI only once, don't force a redraw on each iteration of the loop
TextBox1.Text = TextBox1sb.ToString
TextBox2.Text = TextBox2sb.ToString
End Sub
It shows me wrong, genre
2
2
2
2
4
4
4
6
6
6
6
So where do I get wrong?
If you want your numbers to be shown for each corresponding line, you would need to keep track of each line as well as keeping two separate list's; one for odds and the other for even numbers. Please see below for a quick implementation I did to allow for this.
Dim even As New Dictionary(Of Integer, String)
Dim odd As New Dictionary(Of Integer, String)
Dim currIndex As Integer = 0
Dim curNum As Integer = 0
Dim evenNumbers As String = String.Empty
Dim oddNumbers As String = String.Empty
' Go through all lines in the textbox
TxtBoxIntDraws.Lines.
ToList().ForEach(Sub(line)
evenNumbers = String.Empty
oddNumbers = String.Empty
' Try and split out the numbers from the line
line.Split({"-"c}, StringSplitOptions.RemoveEmptyEntries).AsEnumerable().
Where(Function(i) Integer.TryParse(i.Trim, New Integer)).OrderBy(Function(num) CInt(num)).ToList().
ForEach(Sub(l)
' For each item if we can parse it, then check if even and or odd and add to the correct dictionary
If Integer.TryParse(l.Trim, curNum) Then
If curNum Mod 2 = 0 Then
If evenNumbers.Length > 0 Then
evenNumbers += ", " & curNum.ToString
Else
evenNumbers += curNum.ToString
End If
Else
If oddNumbers.Length > 0 Then
oddNumbers += ", " & curNum.ToString
Else
oddNumbers += curNum.ToString
End If
End If
End If
End Sub)
If Not String.IsNullOrEmpty(evenNumbers) Then even.Add(currIndex, currIndex & " - " & evenNumbers)
If Not String.IsNullOrEmpty(oddNumbers) Then odd.Add(currIndex, currIndex & " - " & oddNumbers)
currIndex += 1
End Sub)
' Show the results
TextBox1.Text = String.Join(Environment.NewLine, even.Values)
TextBox2.Text = String.Join(Environment.NewLine, odd.Values)
Here are my results
Since that was my code from a previous answer, a few edits fixes things. You can fix the final comma.
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox3.Text = "-2-3-4-6-7-8" 'Pretend TextBox3 is TxtBoxIntDraws
Dim number As Integer
Dim numbers As New List(Of Integer)
For Each strLine As String In TextBox3.Lines
Dim nums() As String = strLine.Split("-"c)
For Each num As String In nums
If Integer.TryParse(num.Trim, number) Then
numbers.Add(number)
End If
Next
Next
numbers.Sort()
'When you are building strings use a StringBuilder to avoide creating and throwing away
'a bunch of strings. (happens every time you alter a string
Dim TextBox1sb As New StringBuilder
Dim TextBox2sb As New StringBuilder
For Each intNumber As Integer In numbers
'The Mod operator divides the first number by the second number
'and returns the remainder
If intNumber Mod 2 = 0 Then '(number / 2) = Int(number / 2) Then
'Number is even
TextBox1sb.Append(intNumber.ToString & ",")
TextBox1.Text = TextBox1.Text + intNumber.ToString
Else
TextBox2sb.Append(intNumber.ToString & ",")
TextBox2.Text = TextBox1.Text + intNumber.ToString
End If
Next
'Update the UI only once, don't force a redraw on each iteration of the loop
TextBox1.Text = TextBox1sb.ToString
TextBox2.Text = TextBox2sb.ToString
End Sub
I did a little bit modification on your code above and I tested it correctly, here you what I think you asked for, especially for the first line, otherwise do customize it as you want. Here is the update code:
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim number As Integer
Dim numbers As New List(Of Integer)
For Each strLine As String In TxtArr.Lines
'Dim nums() As String = strLine.Split(","c)
Dim nums() As String = strLine.Split("-")
For Each num As String In nums
If Integer.TryParse(num.Trim, number) Then
numbers.Add(number)
End If
Next
Next
numbers.Sort()
'When you are building strings use a StringBuilder to avoide creating and throwing away
'a bunch of strings. (happens every time you alter a string
Dim TextBox1sb As New StringBuilder
Dim TextBox2sb As New StringBuilder
For Each intNumber As Integer In numbers
'The Mod operator divides the first number by the second number
'and returns the remainder
If intNumber Mod 2 = 0 Then '(number / 2) = Int(number / 2) Then
'Number is even
TextBox1sb.Append(intNumber.ToString & ",")
TextBox1.Text = TextBox1.Text + intNumber.ToString
Else
TextBox2sb.Append(intNumber.ToString & ",")
TextBox2.Text = TextBox2.Text + intNumber.ToString
End If
Next
'Update the UI only once, don't force a redraw on each iteration of the loop
'TextBox1.Text = Strings.Left(TextBox1sb.ToString, TextBox1sb.Length - 1)
TextBox1.Text = Mid(TextBox1sb.ToString, 1, Len(TextBox1sb.ToString) - 1)
TextBox2.Text = Mid(TextBox2sb.ToString, 1, Len(TextBox2sb.ToString) - 1)
End Sub
I hope this can help you bro, ^_^
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.