投稿日 | : 2003/12/12(Fri) 19:19 |
投稿者 | : 魔界の仮面弁士 |
Eメール | : |
URL | : |
タイトル | : Re^7: ボタン |
> コントロール配列オブジェクトとコントロールオブジェクトの配列
> では違いはないのでしょうか?
ちょっとややこしいのですが、両者は別物です。
一般的に「コントロール配列」と呼ばれる物は、実は配列ではありません。
一種のコレクションオブジェクトなのです。
フォームに、Command1、Text1(0)、Text1(1)を貼り付けた上で、
下記のコードを試してみてください。
'===============================
Option Explicit
Private TextBoxes() As VB.TextBox
Private WithEvents SampleEvent As VB.TextBox
Private Sub Form_Load()
Dim Index As Integer
ReDim TextBoxes(2)
Text1(0).Text = "Text1(0)"
Text1(1).Text = "Text1(1)"
For Index = 0 To 2
Set TextBoxes(Index) = Controls.Add("VB.TextBox", "TextBox" & CStr(Index))
With TextBoxes(Index)
.Text = .Name
.Top = (.Height + 240) * Index
.Visible = True
End With
Next
End Sub
Private Sub Command1_Click()
If IsArray(TextBoxes) Then
Debug.Print "TextBoxesは、配列です。"
Else
Debug.Print "TextBoxesは、配列ではありません。"
End If
If IsArray(Text1) Then
Debug.Print "Text1は、配列です。"
Else
Debug.Print "Text1は、配列ではありません。"
End If
If TypeOf TextBoxes(0) Is VB.TextBox Then
Debug.Print "TextBoxes(0) は、TextBox型です。"
Else
Debug.Print "TextBoxes(0) は、TextBox型ではありません。"
End If
If TypeOf Text1(0) Is VB.TextBox Then
Debug.Print "Text1(0) は、TextBox型です。"
Else
Debug.Print "Text1(0) は、TextBox型ではありません。"
End If
On Error Resume Next
Set SampleEvent = TextBoxes(0)
If Err.Number = 0 Then
Debug.Print "TextBoxes(0) は、SampleEvent変数に代入できます。"
Else
Debug.Print "TextBoxes(0) は、SampleEvent変数に代入できません。"
End If
Err.Clear
On Error Resume Next
Set SampleEvent = Text1(0)
If Err.Number = 0 Then
Debug.Print "Text1(0) は、SampleEvent変数に代入できます。"
Else
Debug.Print "Text1(0) は、SampleEvent変数に代入できません。"
End If
On Error GoTo 0
End Sub
'===============================