vb.net里沒有像vb那樣的控件數(shù)組。也就是說復(fù)制控件后粘貼沒有提示你是否創(chuàng)建控件數(shù)組。
創(chuàng)新互聯(lián)建站專業(yè)提供成都主機托管四川主機托管成都服務(wù)器托管四川服務(wù)器托管,支持按月付款!我們的承諾:貴族品質(zhì)、平民價格,機房位于中國電信/網(wǎng)通/移動機房,樂山服務(wù)器托管服務(wù)有保障!
你可以試驗下面的代碼。
Form1.Designer.vb 設(shè)計器中的窗體代碼如下,上面有六個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 は、コンポーネント一覧に后処理を?qū)g行するために 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
'按鈕標題數(shù)組。
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是控件,你在定義的時候就初始化到數(shù)組,但是此時控件本身還沒有初始化,顯然數(shù)組得不到正確的值
正確的做法是先定義數(shù)組,在對話框load事件中初始化數(shù)組,然后再使用
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窗體應(yīng)用程序。
3、新建完畢后,如圖所示。
4、拖動一個按鈕。
5、定義數(shù)組最常見的方法,如圖示。
6、運行后,點擊按鈕,彈出提示正常。
7、定義數(shù)組第二種方法,屬于動態(tài)的方法。
8、運行后,點擊按鈕,數(shù)組成功輸出。