vb.net里没有像vb那样的控件数组。也就是说复制控件后粘贴没有提示你是否创建控件数组。
创新互联建站专业提供成都主机托管四川主机托管成都服务器托管四川服务器托管,支持按月付款!我们的承诺:贵族品质、平民价格,机房位于中国电信/网通/移动机房,乐山服务器托管服务有保障!
你可以试验下面的代码。
Form1.Designer.vb 设计器中的窗体代码如下,上面有六个button。最右边的用来改变前5个地text:
Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated() Partial Class Form1
#Region "Windows フォーム デザイナによって生成されたコード "
System.Diagnostics.DebuggerNonUserCode() Public Sub New()
MyBase.New()
'この呼び出しは、Windows フォーム デザイナで必要です。
InitializeComponent()
End Sub
'Form は、コンポーネント一覧に后処理を実行するために dispose をオーバーライドします。
System.Diagnostics.DebuggerNonUserCode() Protected Overloads Overrides Sub Dispose(ByVal Disposing As Boolean)
If Disposing Then
If Not components Is Nothing Then
components.Dispose()
End If
End If
MyBase.Dispose(Disposing)
End Sub
'Windows フォーム デザイナで必要です。
Private components As System.ComponentModel.IContainer
Public ToolTip1 As System.Windows.Forms.ToolTip
Public WithEvents Command2 As System.Windows.Forms.Button
Public WithEvents _Command1_4 As System.Windows.Forms.Button
Public WithEvents _Command1_3 As System.Windows.Forms.Button
Public WithEvents _Command1_2 As System.Windows.Forms.Button
Public WithEvents _Command1_1 As System.Windows.Forms.Button
Public WithEvents _Command1_0 As System.Windows.Forms.Button
Public WithEvents Command1 As Microsoft.VisualBasic.Compatibility.VB6.ButtonArray
'メモ: 以下のプロシージャは Windows フォーム デザイナで必要です。
'Windows フォーム デザイナを使って変更できます。
'コード エディタを使用して、変更しないでください。
System.Diagnostics.DebuggerStepThrough() Private Sub InitializeComponent()
Me.components = New System.ComponentModel.Container
Me.ToolTip1 = New System.Windows.Forms.ToolTip(Me.components)
Me.Command2 = New System.Windows.Forms.Button
Me._Command1_4 = New System.Windows.Forms.Button
Me._Command1_3 = New System.Windows.Forms.Button
Me._Command1_2 = New System.Windows.Forms.Button
Me._Command1_1 = New System.Windows.Forms.Button
Me._Command1_0 = New System.Windows.Forms.Button
Me.Command1 = New Microsoft.VisualBasic.Compatibility.VB6.ButtonArray(Me.components)
CType(Me.Command1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'Command2
'
Me.Command2.BackColor = System.Drawing.SystemColors.Control
Me.Command2.Cursor = System.Windows.Forms.Cursors.Default
Me.Command2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command2.Location = New System.Drawing.Point(235, 176)
Me.Command2.Name = "Command2"
Me.Command2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Command2.Size = New System.Drawing.Size(65, 25)
Me.Command2.TabIndex = 5
Me.Command2.Text = "Change"
Me.Command2.UseVisualStyleBackColor = False
'
'_Command1_4
'
Me._Command1_4.BackColor = System.Drawing.SystemColors.Control
Me._Command1_4.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_4.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_4, CType(4, Short))
Me._Command1_4.Location = New System.Drawing.Point(16, 176)
Me._Command1_4.Name = "_Command1_4"
Me._Command1_4.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_4.Size = New System.Drawing.Size(93, 25)
Me._Command1_4.TabIndex = 4
Me._Command1_4.Text = "Command1"
Me._Command1_4.UseVisualStyleBackColor = False
'
'_Command1_3
'
Me._Command1_3.BackColor = System.Drawing.SystemColors.Control
Me._Command1_3.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_3.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_3, CType(3, Short))
Me._Command1_3.Location = New System.Drawing.Point(16, 137)
Me._Command1_3.Name = "_Command1_3"
Me._Command1_3.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_3.Size = New System.Drawing.Size(93, 25)
Me._Command1_3.TabIndex = 3
Me._Command1_3.Text = "Command1"
Me._Command1_3.UseVisualStyleBackColor = False
'
'_Command1_2
'
Me._Command1_2.BackColor = System.Drawing.SystemColors.Control
Me._Command1_2.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_2.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_2, CType(2, Short))
Me._Command1_2.Location = New System.Drawing.Point(16, 96)
Me._Command1_2.Name = "_Command1_2"
Me._Command1_2.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_2.Size = New System.Drawing.Size(93, 25)
Me._Command1_2.TabIndex = 2
Me._Command1_2.Text = "Command1"
Me._Command1_2.UseVisualStyleBackColor = False
'
'_Command1_1
'
Me._Command1_1.BackColor = System.Drawing.SystemColors.Control
Me._Command1_1.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_1.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_1, CType(1, Short))
Me._Command1_1.Location = New System.Drawing.Point(16, 56)
Me._Command1_1.Name = "_Command1_1"
Me._Command1_1.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_1.Size = New System.Drawing.Size(93, 25)
Me._Command1_1.TabIndex = 1
Me._Command1_1.Text = "Command1"
Me._Command1_1.UseVisualStyleBackColor = False
'
'_Command1_0
'
Me._Command1_0.BackColor = System.Drawing.SystemColors.Control
Me._Command1_0.Cursor = System.Windows.Forms.Cursors.Default
Me._Command1_0.ForeColor = System.Drawing.SystemColors.ControlText
Me.Command1.SetIndex(Me._Command1_0, CType(0, Short))
Me._Command1_0.Location = New System.Drawing.Point(16, 16)
Me._Command1_0.Name = "_Command1_0"
Me._Command1_0.RightToLeft = System.Windows.Forms.RightToLeft.No
Me._Command1_0.Size = New System.Drawing.Size(93, 25)
Me._Command1_0.TabIndex = 0
Me._Command1_0.Text = "Command1"
Me._Command1_0.UseVisualStyleBackColor = False
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.BackColor = System.Drawing.SystemColors.Control
Me.ClientSize = New System.Drawing.Size(312, 213)
Me.Controls.Add(Me.Command2)
Me.Controls.Add(Me._Command1_4)
Me.Controls.Add(Me._Command1_3)
Me.Controls.Add(Me._Command1_2)
Me.Controls.Add(Me._Command1_1)
Me.Controls.Add(Me._Command1_0)
Me.Cursor = System.Windows.Forms.Cursors.Default
Me.Location = New System.Drawing.Point(4, 23)
Me.Name = "Form1"
Me.RightToLeft = System.Windows.Forms.RightToLeft.No
Me.Text = "Form1"
CType(Me.Command1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
End Class
’==========================================
Form1中的代码如下:
Friend Class Form1
Inherits System.Windows.Forms.Form
Private Sub Command2_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Command2.Click
Dim i As Object
'按钮标题数组。
Dim cArr() As String = New String() {"A", "B", "C", "D", "E", "F", "G"}
For i = 0 To Me.Command1.Count - 1
Me.Command1(i).Text = cArr(i)
Next
End Sub
End Class
button1,button2是控件,你在定义的时候就初始化到数组,但是此时控件本身还没有初始化,显然数组得不到正确的值
正确的做法是先定义数组,在对话框load事件中初始化数组,然后再使用
Dim bt As New ArrayList
bt.Add(Me.Button5)
bt.Add(Me.Button6)
bt.Add(Me.Button7)
上面这段代码本人表示不懂,故没有使用。下面的代码在VS2015中运行通过:
Private Sub btClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click, Button6.Click, Button7.Click
Select Case CType(sender, Button).Name
Case "Button5"
MessageBox.Show("你点击了 Button5 按钮")
Case "Button6"
MessageBox.Show("你点击了 Button6 按钮")
Case "Button7"
MessageBox.Show("你点击了 Button7 按钮")
End Select
End Sub
1、点击VS工具。
2、打开后,新建一个Windows窗体应用程序。
3、新建完毕后,如图所示。
4、拖动一个按钮。
5、定义数组最常见的方法,如图示。
6、运行后,点击按钮,弹出提示正常。
7、定义数组第二种方法,属于动态的方法。
8、运行后,点击按钮,数组成功输出。