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中调试通过。读者可以用到自己的程序中,今后再也不用为调整位置和索引顺序而作一些简单重复劳动了。