Vb中控件的自动排列 - 中国WEB开发者网络 (http://www.webasp.net) -- 技术教程 (http://www.webasp.net/article/) --- Vb中控件的自动排列 (http://www.webasp.net/article/28/27681.htm) |
| -- 作者:未知 -- 发布日期: 2006-06-02 |
| 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()按由小到大顺序排序 Public Sub SortProc(ArrInt() As Integer) '对整数数组进行排序 Public Sub ArrayTabIndexProc(ArrControl() As Control) '对控件数组进行排序 Public Sub ArrayTagProc(arr() As Control) '对控件的<.tag>属性进行排序 |
| webasp.net |