在如今這個(gè)信息技術(shù)更新速度奇快的時(shí)代中,程序員是幸福的。因?yàn)樗麄兛梢酝ㄟ^這些不斷更新的技術(shù)帶來的更重幫助。輕松的完成各項(xiàng)工作需求。VB.NET編程語言就是其中一個(gè)作用比較大的編程語言。
在VB.NET中,面對(duì)大量相同控件的整齊排列時(shí),雖可在設(shè)計(jì)時(shí)排列好,但難免在調(diào)試中不小心移動(dòng),或后來又增減一些。于是有人用語句在程序中調(diào)節(jié),其艱辛是可想而知的(筆者深有體會(huì)),即使位置排好了,由于控件添加的先后問題,其索引屬性(。TabIndex)往往一片混亂。能不能讓控件的位置、索引屬性的排序?qū)崿F(xiàn)自動(dòng)化呢?經(jīng)過一番思索,筆者終于找到了很好的VB.NET控件自動(dòng)排序解決辦法,并成功應(yīng)用于自己開發(fā)的注冊(cè)表修改器中。
例子:新建工程,放入一個(gè)Frame控件Frame1,再在Frame1 中放入4個(gè)復(fù)選框checkbox1、checkbox2、checkbox3、checkbox4
在form_load()子過程中加入一句:ArrangeChildren frame1 運(yùn)行結(jié)果為4個(gè)復(fù)選框等間距整齊地排列在其容器frame1 中。在設(shè)計(jì)窗口中,你可以任意調(diào)整它們的上下位置,運(yùn)行后將按它們?cè)O(shè)計(jì)時(shí)的上下順序整齊排列,并且它們的索引順序按由下到大排列。(索引順序的作用大家知道吧--讓你的程序支持鍵盤操作)。更妙的是,你可在容器中任意增加、減少控件數(shù)量(類型要一樣),運(yùn)行后它們都能整齊排列,從而一勞永逸。
以下是具體的VB.NET控件自動(dòng)排序子過程代碼
Public Sub ArrangeChildren(Father As Control) 'Father為容器控件
功能:
(1)對(duì)容器控件內(nèi)的子控件的TabIndex值進(jìn)行排序
排序依據(jù)是:由上到下(。Top值由小到大),TabIndex小到大
(2)對(duì)容器控件內(nèi)的子控件等間距整齊排列
Dim Child As Control '窗體中的任一控件
Dim Children()
As Control '屬于容器中的控件數(shù)組
Dim Tags()
As Integer '元素的值記錄了控件的TabIndex值
Dim TempChild As Control '臨時(shí)控件
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 '微調(diào)(可適當(dāng)增減)
num = 0 For Each Child In Father.Parent.Controls '搜索容器所在窗體中的每一個(gè)控件
If TypeOf Child Is CheckBox Then '這個(gè)判斷是為了提高效率,可不要
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
numnum = num + 1
End If
End If
Next
If num < 1 Then Exit Sub '當(dāng)容器中一個(gè)子控件也沒有時(shí),退出
num = UBound(Children)
SortProc Tags '將數(shù)組Tags()按由小到大順序排序
ArrayTagProc Children '越在屏幕上面的控件,其<.top>值越小,故讓其<.tag>值也小
For i = 0 To num
Children(i)。TabIndex = Tags(Children(i)。Tag)
Next i '越在屏幕上面的控件,其索引值小(實(shí)現(xiàn)索引值的排序)
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) '對(duì)整數(shù)數(shù)組進(jìn)行排序
Dim i As Integer, j As Integer
Dim temp As Integer
Dim num As Integer '數(shù)組大小
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) '對(duì)控件數(shù)組進(jìn)行排序 '控件數(shù)組 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) '對(duì)控件的<.tag>屬性進(jìn)行排序 '越在屏幕上面的控件,其<.top>值越小,故讓其<.tag>值也小(最綜的目的是讓其索引值小)
Dim i As Integer, j As Integer
Dim temp As Variant
Dim num As Integer
Dim arrTop()
As Integer '存儲(chǔ)控件<.Top屬性>數(shù)組
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
以上VB.NET控件自動(dòng)排序程序在VB5.0、VB6.0中調(diào)試通過。讀者可以用到自己的程序中,今后再也不用為調(diào)整位置和索引順序而作一些簡單重復(fù)勞動(dòng)了。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
本文題目:解析VB.NET控件自動(dòng)排序代碼相關(guān)信息-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://chinadenli.net/article26/cdohjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、響應(yīng)式網(wǎng)站、虛擬主機(jī)、電子商務(wù)、網(wǎng)站收錄、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容