大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
可以实现
我们提供的服务有:网站设计、网站建设、微信公众号开发、网站优化、网站认证、江城ssl等。为1000多家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的江城网站制作公司
首先创建一个Button类型控件数组:
1、创建“Windows应用程序”类型的工程,添加名为ButtonArray的类,并使该类继承 System.Collection.CollectionBase 类。System.Collections.CollectionBase类是.NET框架类库中为集合操作提供抽象的基类,通过对它的继承可以为我们的ButtonArray类具备集合增加、删除、索引的功能。
2、为ButtonArray类添加ParentForm属性,即控件组所在窗体,创建初始化函数(构造函数);
3、为控件数组类增加AddItem方法,该方法在控件数组类中添加成员;
4、为控件数组类增加RemoveItem方法,该方法在控件数组中删除一个成员。
示例代码:
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)
'在这里编写控件数组对点击事件的响应
'例如:
MsgBox("点击:" sender.GetType().ToString CType(CType(sender, Button).Tag, String))
End Sub
End Class
使用创建的控件数组
在Form1中放置两个按钮Button1、Button2,分别测试控件数组的增添、删除。
双击Form添加代码:
Public Class Form1
Inherits System.Windows.Forms.Form
……Windows窗体设计器生成的代码……
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
其他的控件数组也可以用类似的方式来实现
例如 Label控件数组
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)
'在这里编写控件数组对点击事件的响应
'例如:
MsgBox("点击:" sender.GetType().ToString CType(CType(sender, Label).Tag, String))
End Sub
End Class
使用创建的Label控件
在Form1中放置两个按钮Label1、Label2
双击Form添加代码:
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows 窗体设计器生成的代码 "
Public Sub New()
MyBase.New()
'该调用是 Windows 窗体设计器所必需的。
InitializeComponent()
'在 InitializeComponent() 调用之后添加任何初始化
'用来绑定label
BindArray()
End Sub
……Windows窗体设计器生成的其他代码……
#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
然后可以测试点击两个label可以显示相应的Tag的信息。
不需要用数组:
假设已经存在的图片文件存放在 C:\图片 文件夹里,并假设新创建的文件夹位于C:\图片 文件夹里。
在窗体上添加一个按钮,代码如下:
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个文件存放在一个文件夹里
Dim MyPath As String
Dim MyFileName As String
Button1.Enabled = False
MyPath = "C:\图片\" '指定已有图片的路径
MyFileName = UCase(Dir(MyPath, FileAttribute.Normal)) ' 找寻第一项。
Do While MyFileName "" ' 开始循环。
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()) ' 查找下一项
Loop
Button1.Enabled = True
End Sub
代码通过测试。
先在: My Project 中引用 Microsoft Excel 11.0 Object Library(这个是Excel 2003,Excel 2007 是12.0),之后在窗体代码中加入相关内容就行了。以下是一个窗体的简单实例
假设在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) = "数组:A"
For Index = 0 To UBound(A)
excelapp.Cells(Index + 2, 1) = A(Index)
Next
excelapp.Cells(1, 2) = "数组:B"
For Index = 0 To UBound(B)
excelapp.Cells(Index + 2, 2) = B(Index)
Next
excelapp.Visible = True
End Sub
End Class
运行结果:
数组:A 数组:B
A 1
B 22
CC 34
C 50
D 16
99
14
可以的,需要判断控件类型并做一下类型转换。
For Each c As Control In Me.Controls
If TypeOf(c) Is TextBox Then
Dim t As TextBox = CType(c ,TextBox)
'调用t的方法即可
End If
Next