2011年12月31日

Public Class Form1   Dim myButton() As Button   Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) '這邊的部份使...

VB2008 使用物件陣列範例

Public Class Form1
  Dim myButton() As Button

  Private Sub myButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) '這邊的部份使用其他物件的click事件,並把後面Handle...給刪除
    Dim thisButton As Button = sender
    MsgBox("你按了第" & Val(Strings.Right(thisButton.Name, 2)) & "個按鈕!") '右邊兩個字即為按鈕的編號
  End Sub

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    For i = 0 To 14
      ReDim Preserve myButton(i) '新增陣列長度
      myButton(i) = New Button '產生按鈕類別
      myButton(i).Name = "myButton" & Format(i, "00") '
      myButton(i).Size = New Size(30, 30) '設定大小
      myButton(i).Left = (i Mod 5) * 35 + 10 '按鈕由左到右排列,五個則換一行
      myButton(i).Top = (i \ 5) * 35 + 10
      myButton(i).Text = Format(i, "00")
      Me.Controls.Add(myButton(i)) '將新增的按鈕放到表單上
      AddHandler myButton(i).Click, AddressOf myButton_Click '指派事件到這個按鈕
    Next
  End Sub
End Class

本文引用於此

沒有留言:

張貼留言

希望各位在留言時可以選擇身分
Yahoo帳號請選OpenID
網址打yahoo.com
以便回覆時較好辨認及交流
謝謝合作

Powered by s911415 and Blogger