이 코드에서, 예를 들어 표현으로 타입을 사용하는 트릭을 존재합니다유형을 표현식으로 사용하는 방법은 무엇입니까? 나는 궁금했다
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
고맙지 만 수정 사항을 사용하여 처리기를 추가하는 데 문제가 있습니다. 업데이트 된 질문을 참조 할 수 있습니까? – ElektroStudios