如何在vb中对文本框的值进行排序? [英] How to sort values of textboxes in vb?

查看:46
本文介绍了如何在vb中对文本框的值进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两组文本框,A 和 B,它们是动态创建的.

I have two groups of textboxes, A and B and these are created dynamically.

我的程序应该是这样的:1. A 文本框对应 B 文本框.2. 然后,B 文本框应按其值升序排列.3. 根据该顺序,A 文本框的值也将被排序.

My program should work like this: 1. A textboxes have corresponding B textboxes. 2. Then, B textboxes should be sorted by their values in ascending order. 3. Based on that order, the A textboxes' values will be sorted also.

例如:

A                   B
5                   1
2                   0
3                   4
1                   5

输出为:2 5 3 1

Output is: 2 5 3 1

请帮帮我.谢谢!

推荐答案

创建您拥有的文本框控件的数组,然后编写一个简单的冒泡排序.冒泡排序很慢,但对于少量数据来说已经足够快了.

Create arrays of the textbox controls you have and then write a simple bubble sort. Bubble sort is slow, but more than fast enough for small amounts of data.

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

现在文本框值已排序,您可以在任何地方显示结果.

Now the textbox values are sorted and you can display the results whereever you want.

要显示标签中的值,例如称为 l1-l5:

To display the values in the labels, say called l1-l5:

Dim arrL() as Label = {l1, l2, l3, l4, l5}
For i = 0 to 4
  arrL(i).Text = arrA(i).Text
Next

这篇关于如何在vb中对文本框的值进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆