可以實(shí)現(xiàn)
我們提供的服務(wù)有:網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、微信公眾號(hào)開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、江城ssl等。為1000多家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的江城網(wǎng)站制作公司
首先創(chuàng)建一個(gè)Button類型控件數(shù)組:
1、創(chuàng)建“Windows應(yīng)用程序”類型的工程,添加名為ButtonArray的類,并使該類繼承 System.Collection.CollectionBase 類。System.Collections.CollectionBase類是.NET框架類庫(kù)中為集合操作提供抽象的基類,通過(guò)對(duì)它的繼承可以為我們的ButtonArray類具備集合增加、刪除、索引的功能。
2、為ButtonArray類添加ParentForm屬性,即控件組所在窗體,創(chuàng)建初始化函數(shù)(構(gòu)造函數(shù));
3、為控件數(shù)組類增加AddItem方法,該方法在控件數(shù)組類中添加成員;
4、為控件數(shù)組類增加RemoveItem方法,該方法在控件數(shù)組中刪除一個(gè)成員。
示例代碼:
Public Class ButtonArray
Inherits System.Collections.CollectionBase
Private ReadOnly ParentForm As System.Windows.Forms.Form
Public Sub New(ByVal pForm As System.Windows.Forms.Form)
ParentForm = pForm
End Sub
Default Public ReadOnly Property Item(ByVal index As Integer) As System.Windows.Forms.Button
Get
Return Me.List.Item(index) ' ButtonArray的List 屬性從CollectionBase 繼承
End Get
End Property
Public Sub AddItem()
Dim btnItem As New System.Windows.Forms.Button
Me.List.Add(btnItem)
ParentForm.Controls.Add(btnItem) '向窗體中增加控件
btnItem.Tag = Me.Count 'Count屬性從CollectionBase 繼承
btnItem.Top = Me.Count * 30
btnItem.Left = 200
btnItem.Text = "Button" Me.Count.ToString
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
End Sub
Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Button)
Me.List.Add(btnItem)
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
End Sub
Public Sub RemoveItem()
If Me.Count 0 Then
ParentForm.Controls.Remove(Me(Me.Count - 1))
Me.List.RemoveAt(Me.Count - 1)
End If
End Sub
Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'在這里編寫控件數(shù)組對(duì)點(diǎn)擊事件的響應(yīng)
'例如:
MsgBox("點(diǎn)擊:" sender.GetType().ToString CType(CType(sender, Button).Tag, String))
End Sub
End Class
使用創(chuàng)建的控件數(shù)組
在Form1中放置兩個(gè)按鈕Button1、Button2,分別測(cè)試控件數(shù)組的增添、刪除。
雙擊Form添加代碼:
Public Class Form1
Inherits System.Windows.Forms.Form
……Windows窗體設(shè)計(jì)器生成的代碼……
Dim Buttons As New ButtonArray(Me)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Buttons.AddItem()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Buttons.RemoveItem()
End Sub
End Class
其他的控件數(shù)組也可以用類似的方式來(lái)實(shí)現(xiàn)
例如 Label控件數(shù)組
LabelArray.vb代碼如下:
Public Class LabelArray
Inherits System.Collections.CollectionBase
Private ReadOnly ParentForm As System.Windows.Forms.Form
Public Sub New(ByVal pForm As System.Windows.Forms.Form)
ParentForm = pForm
End Sub
Default Public ReadOnly Property Item(ByVal index As Integer) As System.Windows.Forms.Label
Get
Return Me.List.Item(index) ' ButtonArray的List 屬性從CollectionBase 繼承
End Get
End Property
Public Sub AddItem(ByVal btnItem As System.Windows.Forms.Label)
Me.List.Add(btnItem)
AddHandler btnItem.Click, AddressOf btnItem_Click '綁定事件處理程序
End Sub
Public Sub btnItem_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'在這里編寫控件數(shù)組對(duì)點(diǎn)擊事件的響應(yīng)
'例如:
MsgBox("點(diǎn)擊:" sender.GetType().ToString CType(CType(sender, Label).Tag, String))
End Sub
End Class
使用創(chuàng)建的Label控件
在Form1中放置兩個(gè)按鈕Label1、Label2
雙擊Form添加代碼:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗體設(shè)計(jì)器生成的代碼 "
Public Sub New()
MyBase.New()
'該調(diào)用是 Windows 窗體設(shè)計(jì)器所必需的。
InitializeComponent()
'在 InitializeComponent() 調(diào)用之后添加任何初始化
'用來(lái)綁定label
BindArray()
End Sub
……Windows窗體設(shè)計(jì)器生成的其他代碼……
#End Region
Dim Labels As New LabelArray(Me)
Public Sub BindArray()
Me.Label1.Tag = "1111"
Me.Label2.Tag = "222"
Labels.AddItem(Me.Label1)
Labels.AddItem(Me.Label2)
End Sub
End Class
然后可以測(cè)試點(diǎn)擊兩個(gè)label可以顯示相應(yīng)的Tag的信息。
不需要用數(shù)組:
假設(shè)已經(jīng)存在的圖片文件存放在 C:\圖片 文件夾里,并假設(shè)新創(chuàng)建的文件夾位于C:\圖片 文件夾里。
在窗體上添加一個(gè)按鈕,代碼如下:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim n As Long = -1
Dim P As Long = -1
Dim Pn As String
Dim MyNum As Integer = 100 '每100個(gè)文件存放在一個(gè)文件夾里
Dim MyPath As String
Dim MyFileName As String
Button1.Enabled = False
MyPath = "C:\圖片\" '指定已有圖片的路徑
MyFileName = UCase(Dir(MyPath, FileAttribute.Normal)) ' 找尋第一項(xiàng)。
Do While MyFileName "" ' 開始循環(huán)。
If InStr(MyFileName, "JPG") 0 Or InStr(MyFileName, "GIF") 0 Or InStr(MyFileName, "PNG") 0 Then
n = n + 1
If n \ MyNum P Then
P = P + 1
Pn = CStr(n \ MyNum + 1)
MkDir(MyPath Pn)
End If
FileCopy(MyPath MyFileName, MyPath Pn "\" MyFileName)
End If
MyFileName = UCase(Dir()) ' 查找下一項(xiàng)
Loop
Button1.Enabled = True
End Sub
代碼通過(guò)測(cè)試。
先在: My Project 中引用 Microsoft Excel 11.0 Object Library(這個(gè)是Excel 2003,Excel 2007 是12.0),之后在窗體代碼中加入相關(guān)內(nèi)容就行了。以下是一個(gè)窗體的簡(jiǎn)單實(shí)例
假設(shè)在C盤根文件夾中有:TEST.xls
Public Class 引用EXCEL窗體
Private A() As String = {"A", "B", "CC", "C", "D"}
Private B() As Integer = {1, 22, 34, 50, 16, 99, 14}
Private excelapp As New Microsoft.Office.Interop.Excel.Application
Private excelworkbook As Microsoft.Office.Interop.Excel.Workbook
Private Sub 引用EXCEL_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Index As Integer
excelworkbook = excelapp.Workbooks.Open("c:\test.xls")
excelapp.Application.Workbooks.Add(True)
excelapp.Cells(1, 1) = "數(shù)組:A"
For Index = 0 To UBound(A)
excelapp.Cells(Index + 2, 1) = A(Index)
Next
excelapp.Cells(1, 2) = "數(shù)組:B"
For Index = 0 To UBound(B)
excelapp.Cells(Index + 2, 2) = B(Index)
Next
excelapp.Visible = True
End Sub
End Class
運(yùn)行結(jié)果:
數(shù)組:A 數(shù)組:B
A 1
B 22
CC 34
C 50
D 16
99
14
可以的,需要判斷控件類型并做一下類型轉(zhuǎn)換。
For Each c As Control In Me.Controls
If TypeOf(c) Is TextBox Then
Dim t As TextBox = CType(c ,TextBox)
'調(diào)用t的方法即可
End If
Next