Vb作为一种流行的可视化编程语言,其强大的界面设计功能为程序设计者省了不少时间。不过在面对大量相同控件的整齐排列时,虽可在设计时排列好,但难免在调试中不小心移动,或后来又增减一些。于是有人用语句在程序中调节,其艰辛是可想而知的(笔者深有体会),即使位置排好了,由于控件添加的先后问题,其索引属性(.TabIndex)往往一片混乱.能不能让控件的位置、索引属性的排序实现自动化呢?经过一番思索,笔者终于找到了很好的解决办法,并成功应用于自己开发的注册表修改器中。
例子:新建工程,放入一个Frame控件Frame1,再在Frame1 中放入4个复选框checkbox1、checkbox2、checkbox3、checkbox4
在form_load()子过程中加入一句:ArrangeChildren frame1 运行结果为4个复选框等间距整齐地排列在其容器frame1 中。在设计窗口中,你可以任意调整它们的上下位置,运行后将按它们设计时的上下顺序整齐排列,并且它们的索引顺序按由下到大排列。(索引顺序的作用大家知道吧——让你的程序支持键盘操作)。更妙的是,你可在容器中任意增加、减少控件数量(类型要一样),运行后它们都能整齐排列,从而一劳永逸。
以下是具体的子过程代码
Public Sub ArrangeChildren(Father As Control) 'Father为容器控件
'功能:(1)对容器控件内的子控件的TabIndex值进行排序
'排序依据是:由上到下(.Top值由小到大),TabIndex小到大
'(2)对容器控件内的子控件等间距整齐排列
Dim Child As Control '窗体中的任一控件
Dim Children() As Control '属于容器中的控件数组
Dim Tags() As Integer '元素的值记录了控件的TabIndex值
Dim TempChild As Control '临时控件
Dim i As Integer, j As Integer
Dim x As Integer, Y As Integer
Dim wChild As Integer, hChild As Integer
Dim num As Integer
Dim strTemp As String
Const ADJUST as integer=150 ’微调(可适当增减)
num = 0
For Each Child In Father.Parent.Controls ‘搜索容器所在窗体中的每一个控件
If TypeOf Child Is CheckBox Then ‘这个判断是为了提高效率,可不要
If Child.Container Is Father Then
ReDim Preserve Children(num)
ReDim Preserve Tags(num)
Set Children(num) = Child
Children(num).Tag = num
Tags(num) = Children(num).TabIndex
num = num + 1
End If
End If
Next
If num < 1 Then Exit Sub ‘当容器中一个子控件也没有时,退出
num = UBound(Children)
SortProc Tags ‘将数组Tags()按由小到大顺序排序
ArrayTagProc Children ‘越在屏幕上面的控件,其<.top>值越小,故让其<.tag>值也小
For i = 0 To num
Children(i).TabIndex = Tags(Children(i).Tag)
Next i ‘越在屏幕上面的控件,其索引值小(实现索引值的排序)
ArrayTabIndexProc Children '
x = 200 ‘控件在其容器中的起始位置
wChild = 4000 ‘控件宽度
hChild = 255 ‘控件高度
Y = (Father.Height - ADJUST - (num + 1) * hChild) / (num + 2)
For j = 0 To num
Children(j).Move x, (j + 1) * Y + j * hChild + ADJUST, wChild, hChild
Next j
End Sub
Public Sub SortProc(ArrInt() As Integer) '对整数数组进行排序
Dim i As Integer, j As Integer
Dim temp As Integer
Dim num As Integer '数组大小
num = UBound(ArrInt)
For i = 0 To num
For j = i + 1 To UBound(ArrInt)
If ArrInt(i) > ArrInt(j) Then
temp = ArrInt(i)
ArrInt(i) = ArrInt(j)
ArrInt(j) = temp
End If
Next j
Next i
End Sub
Public Sub ArrayTabIndexProc(ArrControl() As Control) '对控件数组进行排序
'控件数组 ArrControl(0),ArrControl(1),ArrControl(2),.....ArrControl(n)
'的TabIndex值按由低到高顺序排列
Dim i As Integer, j As Integer
Dim temp As Control
Dim num As Integer
num = UBound(ArrControl)
For i = 0 To num
For j = i + 1 To UBound(ArrControl)
If ArrControl(i).TabIndex > ArrControl(j).TabIndex Then
Set temp = ArrControl(i)
Set ArrControl(i) = ArrControl(j)
Set ArrControl(j) = temp
End If
Next j
Next i
End Sub
Public Sub ArrayTagProc(arr() As Control) '对控件的<.tag>属性进行排序
'越在屏幕上面的控件,其<.top>值越小,故让其<.tag>值也小(最综的目的是让其索引值小)
Dim i As Integer, j As Integer
Dim temp As Variant
Dim num As Integer
Dim arrTop() As Integer '存储控件<.Top属性>数组
num = UBound(arr)
ReDim arrTop(num)
For i = 0 To num
arrTop(i) = arr(i).Top
Next
SortProc arrTop '按照由小到大顺序
For i = 0 To num
For j = 0 To num
If arr(i).Top = arrTop(j) Then arr(i).Tag = j
Next j
Next i
End Sub
以上程序在Vb5.0、Vb6.0中调试通过。读者可以用到自己的程序中,今后再也不用为调整位置和索引顺序而作一些简单重复劳动了。
本文地址:http://com.8s8s.com/it/it6828.htm