我有两组文本框,A和B,这些是动态创建的。
我的程序应该像这样工作: 1.文本框具有相应的B文本框。 2.然后,B文本框应按其值按升序排序。 3.根据该顺序,A文本框的值也将被排序。
EX:
A B
5 1
2 0
3 4
1 5
输出为:2 5 3 1
请帮帮我。谢谢!
答案 0 :(得分:0)
创建您拥有的文本框控件的数组,然后编写一个简单的冒泡排序。冒泡排序很慢,但对于少量数据来说速度要快得多。
Dim arrA() As Textbox = {a1, a2, a3, a4, a5}
Dim arrB() As Textbox = {b1, b2, b3, b4, b5}
Dim Changed as Boolean
Do
Changed = False
For i = 0 to arrB.Count - 2 'Stop at the second to last array item because we check forward in the array
If CInt(arrB(i).Text) > CInt(arrB(i + 1).Text) Then 'Next value is smaller than previous --> Switch values, also switch in arrA
Dim Temp as String = arrB(i + 1).Text
arrB(i + 1).Text = arrB(i).Text
arrB(i).Text = Temp
Temp = arrA(i + 1).Text
arrA(i + 1).Text = arrA(i).Text
arrA(i).Text = Temp
Changed = True
End If
Next
Loop Until Changed = False 'Cancle the loop when everything is sorted
现在,文本框值已排序,您可以根据需要显示结果。
要显示标签中的值,例如名为l1-l5:
Dim arrL() as Label = {l1, l2, l3, l4, l5}
For i = 0 to 4
arrL(i).Text = arrA(i).Text
Next