2013-06-11 3 views
0

이 코드에서, 예를 들어 표현으로 타입을 사용하는 트릭을 존재합니다유형을 표현식으로 사용하는 방법은 무엇입니까? 나는 궁금했다

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

    Dim ControlType = CheckBox ' CheckBox is a type and cannot be used as an expression 

    Dim ControlArray(5) As ControlType ' (5) The number of controls to create. 

    For num As Int64 = 0 To ControlArray.LongLength - 1 
     ControlArray(num) = New ControlType ' Expected: New CheckBox 
     ControlArray(num).Text = (ControlType.ToString & num.ToString) ' Expected string: "CheckBox 0" 
     Me.Controls.Add(ControlArray(num)) 
    Next 

End Sub 

내가 컨트롤 배열을 할 수있는 방법을 묻는 게 아니에요 내가 할 수있는 경우, 내가 부탁 해요 var (ControlType)에 Type을 expecifying하고 위의 코드 예제와 같이 사용하는 일반적인 컨트롤 배열을 수행합니다.

UPDATE는

이 지금은 핸들러

에게 연결하려고 할 때 내가 체크 박스의 "CheckedChanged"이벤트를 인식 할 수

를 사용하기 위해 노력하고있어 코드입니다

또한 값을 확인하려고 할 때 ".Checked"속성을 인식 할 수 없습니다.

Public Class Form1 

    Dim ControlType As Type = GetType(CheckBox) ' The type of Control to create. 
    Dim ControlArray(5) As Control    ' (5) The number of controls to create. 

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 

     For num As Int64 = 0 To ControlArray.LongLength - 1 
      ControlArray(num) = Activator.CreateInstance(ControlType) ' Create the control instance (New CheckBox) 
      ControlArray(num).Name = ControlType.Name & num.ToString ' Name example : CheckBox 0 
      ControlArray(num).Text = ControlType.Name & num.ToString ' String example: CheckBox 0 
      ControlArray(num).Top = 20 * num ' Adjust the location of each control. 
      Me.Controls.Add(ControlArray(num)) ' Add the control to a container. 

      ' This does not work: 
      AddHandler ControlArray(num).CheckedChanged, AddressOf CheckBoxSub ' Add a event handler to a procedure. 

     Next 

    End Sub 

    Public Sub CheckBoxSub(ByVal sender As Object, ByVal e As System.EventArgs) ' Sub which is handling the controls. 

     If sender.Checked = True Then MsgBox(sender.name & " is checked") Else MsgBox(sender.name & " is unchecked") 

     ' Just an example of how to use the controls, 
     ' This does not work: 
     ControlArray(2).checked = True ' CheckBox 2.Checked = True 

    End Sub 

End Class 

답변

1

개체 형식을 유지하려면 System.Type을 사용할 수 있습니다.

Activator.CreateInstance (MyType)를 사용하여 MyType 유형의 개체 인스턴스를 만들 수도 있습니다.

Dim ControlType As System.Type = GetType(CheckBox) 
Dim ControlArray(5) As Control 

For num As Int64 = 0 To 5 
    ControlArray(num) = Activator.CreateInstance(ControlType) 
    ControlArray(num).Left = num * 100 
    ControlArray(num).Name = "CheckBox " & num 
    ControlArray(num).Text = ControlArray(num).Name 
    Me.Controls.Add(ControlArray(num)) 
Next 

편집 :

업데이트 된 질문에 대해
AddHandler DirectCast(ControlArray(Num), CheckBox).CheckedChanged, AddressOf MyHandler 
+0

고맙지 만 수정 사항을 사용하여 처리기를 추가하는 데 문제가 있습니다. 업데이트 된 질문을 참조 할 수 있습니까? – ElektroStudios

1

: 당신은 필요

당신은 DirectCast() 오브젝트 유형으로 제어, 예를 들면에 필요, 이벤트 처리기를 추가하려면 올바른 유형의 객체를 캐스팅하여 CheckBox 관련 등록 정보 또는 이벤트를 사용할 수 있습니다. 예 :

Dim cbx As CheckBox = TryCast(ControlArray(2), CheckBox) 
If cbx IsNot Nothing Then   ' If the second control is a check box 
    cbx.Checked = True 
End If 

TryCast 지정된 유형으로 지정된 객체를 캐스트 또는 객체가 다른 유형이있는 경우 Nothing 반환하는 대신

AddHandler ControlArray(num).CheckedChanged, AddressOf CheckBoxSub 

의 당신은 당신의 두 번째 예를 들어

Dim cbx As CheckBox = TryCast(ControlArray(num), CheckBox) 
If cbx IsNot Nothing Then 
    AddHandler cbx.CheckedChanged, AddressOf CheckBoxSub 
End If 

같은 쓰기 .

+0

답변 해 주셔서 감사합니다. – ElektroStudios

관련 문제