在VB.net 中如何可以像VB一样批量设置控件数组的属性

Y御剑逍遥Y 2015-08-14 09:08:00
各位大大:在VB中我们可以在程序开始初始化一批控件的某个属性代码如下:
for i=0 to 10
if i/3=int(i/3) then
label1(i).text="hahah"
end if
next i

而在VB.NET中 创建不出控件数组
类似以上的功能该如何完成,望高手不吝赐教。
...全文
892 12 点赞 打赏 收藏 举报
写回复
12 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
mystery1000 2019-04-11
十个以内一个一个写,十个以上就改变设计方式,datagridview
  • 打赏
  • 举报
回复
numbermax 2019-03-24
你自己封装一个类继承组件,里面放集合或者字典,在画面上把你当做数组的控件add到你封装的组件里面。
  • 打赏
  • 举报
回复
yjchina719 2019-03-08
引用 4 楼 Tiger_Zhao的回复:
Public Class Form1

Private labelArray() As Label

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.labelArray = New Label() {Me.Label1, Me.Label2, Me.Label3, Me.Label4, Me.Label5, _
Me.Label6, Me.Label7, Me.Label8, Me.Label9, Me.Label10, _
Me.Label11}
For i = 0 To 10
If i / 3 = Int(i / 3) Then
Me.labelArray(i).Text = "hahah"
End If
Next i
End Sub

End Class
这相当于使用控件数组
  • 打赏
  • 举报
回复
alantan8 2019-03-01
这个Me没必要,搞得这么麻烦
  • 打赏
  • 举报
回复
yzrgr79 2015-09-01
Public Class AxvaControlArray Inherits System.ComponentModel.Component Protected ControlList As System.Collections.Generic.List(Of AxvaControl) Public Sub New() ControlList = New System.Collections.Generic.List(Of AxvaControl) End Sub Public Event Advance(ByVal sender As System.Object, ByVal e As _DControlEvents_AdvanceEvent) Public Event Change(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_ChangeEvent) Public Event ClickEvent(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_ClickEvent) Public Event DblClick(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_DblClickEvent) Public Event Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Public Event LeaveCell(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_LeaveCellEvent) Public Event Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Public Event KeyDownEvent(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_KeyDownEvent) Public Event LeaveRow(ByVal eventSender As Object, ByVal eventArgs As _DControlEvents_LeaveRowEvent) Public Event ComboSelChange(ByVal eventSender As Object, ByVal eventArgs As _DControlEvents_ComboSelChangeEvent) Public Function GetIndex(ByVal o As AxControl.AxvaControl) As Short Return ControlList.IndexOf(DirectCast(o, AxControl.AxvaControl)) End Function Public Sub SetIndex(ByVal o As AxControl.AxvaControl, ByVal Index As Short) If ControlList.Contains(o) = False Then ControlList.Add(o) End If AddHandler DirectCast(o, AxControl.AxvaControl).Change, AddressOf HandleChange AddHandler DirectCast(o, AxControl.AxvaControl).DblClick, AddressOf HandleDblClick AddHandler DirectCast(o, AxControl.AxvaControl).KeyDownEvent, AddressOf HandleKeyDownEvent AddHandler DirectCast(o, AxControl.AxvaControl).ClickEvent, AddressOf HandlelClick AddHandler DirectCast(o, AxControl.AxvaControl).Enter, AddressOf HandlelEnter AddHandler DirectCast(o, AxControl.AxvaControl).Leave, AddressOf HandlelLeave AddHandler DirectCast(o, AxControl.AxvaControl).Advance, AddressOf HandlelAdvance AddHandler DirectCast(o, AxControl.AxvaControl).LeaveRow, AddressOf HandlelLaveRow AddHandler DirectCast(o, AxControl.AxvaControl).ComboSelChange, AddressOf HandlelComboSelChange AddHandler DirectCast(o, AxControl.AxvaControl).LeaveCell, AddressOf HandlelLeaveCell End Sub Default Public ReadOnly Property Item(ByVal Index As Short) As AxControl.AxvaControl Get Item = CType(ControlList(Index), AxControl.AxvaControl) End Get End Property Private Sub HandlelComboSelChange(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_ComboSelChangeEvent) RaiseEvent ComboSelChange(sender, e) End Sub Private Sub HandlelLaveRow(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_LeaveRowEvent) RaiseEvent LeaveRow(sender, e) End Sub Private Sub HandleChange(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_ChangeEvent) RaiseEvent Change(sender, e) End Sub Private Sub HandlelClick(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_ClickEvent) RaiseEvent ClickEvent(sender, e) End Sub Private Sub HandlelEnter(ByVal sender As System.Object, ByVal e As System.EventArgs) RaiseEvent Enter(sender, e) End Sub Private Sub HandlelLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) RaiseEvent Leave(sender, e) End Sub Private Sub HandleDblClick(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_DblClickEvent) RaiseEvent DblClick(sender, e) End Sub Private Sub HandleKeyDownEvent(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_KeyDownEvent) RaiseEvent KeyDownEvent(sender, e) End Sub Private Sub HandlelAdvance(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_AdvanceEvent) RaiseEvent Advance(sender, e) End Sub Private Sub HandlelLeaveCell(ByVal sender As System.Object, ByVal e As AxControl._DControlEvents_LeaveCellEvent) RaiseEvent LeaveCell(sender, e) End Sub End Class
  • 打赏
  • 举报
回复
Mister_Cao 2015-08-30
VB.Net已经取消了在窗体设计界面添加控件数组功能了。如果要用控件数组,必须用代码添加 用代码方式添加控件数组很方便,搜索一下本论坛就会得到好多结果。
  • 打赏
  • 举报
回复
将这一些视作数组的控件放入到同一个容器中,例如Panel中。

        For Each c As Control In Panel1.Controls  //遍历容器中的所有控件
            Dim s As Label = TryCast(c, Label)       //尝试将控件转换成所需的控件类型(如:Label),转换失败时返回Nothing。
            If Not s Is Nothing Then
                s.Text = "hahah"
            End If
        Next
  • 打赏
  • 举报
回复
lgwhs 2015-08-27
对于无需定位显示的控件组可用程序解决。如果控件组中每个控件需要在固定位置显示,采用程序可能就非常麻烦了!
  • 打赏
  • 举报
回复
lgwhs 2015-08-26
貌似不如之前控件数组方便。有人采用附加一段程序实现,有点麻烦。
  • 打赏
  • 举报
回复
Tiger_Zhao 2015-08-26
Public Class Form1

Private labelArray() As Label

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.labelArray = New Label() {Me.Label1, Me.Label2, Me.Label3, Me.Label4, Me.Label5, _
Me.Label6, Me.Label7, Me.Label8, Me.Label9, Me.Label10, _
Me.Label11}
For i = 0 To 10
If i / 3 = Int(i / 3) Then
Me.labelArray(i).Text = "hahah"
End If
Next i
End Sub

End Class
  • 打赏
  • 举报
回复
遍历,查找属于同一类型的控件,然后设置 类似: foreach o as .control..... in me.controls .....
  • 打赏
  • 举报
回复
楚狂歌 2015-08-14
没有控件数组的概念了,只有集合。或者用遍历的方法达到相同的功能
  • 打赏
  • 举报
回复
相关推荐
发帖
VB
加入

1.6w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
帖子事件
创建了帖子
2015-08-14 09:08
社区公告
暂无公告