2016-07-18 4 views
1

예를 들어 frmMenu, frmParentfrmChild이 있습니다.mdiChild 중 하나에서 타이머 활성화 또는 비활성화

frmChild에 나는 tmrChild.enabled = false이 있습니다. 10 frmChild을 인스턴스화하므로 10 frmChild이 내 frmParent 안에 있습니다. frmChild에는 각각 tmrChild이라는 타이머가 있고 모두 enabled = false입니다.

frmChild 형태의 이름에 frmChild1, frmChild2, frmChild3 .... frmChild10

frmMenu 내가 그들 각 각 frmChildenabled 속성에 해당, 10 buttons 있습니다. 내 frmMenu에서 button1을 클릭 내 frmChild1에 timer을 활성화 할 때, 내가 원하는 무엇

10 buttons on my frmMenu = 10 timer in each of my frmChild

이다. 어떻게해야합니까?

나는 frmChild의 이름, 내가

objForms.tmrChild.enabled = True 

가 어떻게 참조 할 코드의이 부분에 쉽게 뭔가를 누락 것 같습니다이

Private Sub button1_Click(sender As Object, e As EventArgs) Handles button1.Click 
     Dim objForms As Form 
     For Each objForms In frmParent.MdiChildren 
      If objForms.Text = "frmChild1" Then 
       objForms.tmr1.enabled = True 
      End If 
     Next 
End Sub 

같은 것을 목표로 노력했습니다 그것에 timer?

+1

'Dim objForms As Form' 대신'Dim objForms As frmChild'를 사용하십시오. 이렇게하면'Form'의 일부로 선언 된 변수에 추가하여'frmChild'의 일부로 선언 된 변수에 접근 할 수 있습니다. – TnTinMn

답변

1

나는 이것을 위해 Dictionary을 사용하는 경향이 있습니다. 하위 폼에 대해 별도의 필드를 사용하는 대신 Dictionary(Of Button, fmrChild)에 대해 하나의 필드 만 있으면됩니다. 그런 다음 Button/양식 쌍을 Dictionary에 각각 추가 할 수 있습니다. Button이 클릭되면 Dictionary에서 해당 양식을 얻고 필요한 경우 (예 : 그 방법의 Handles 절 모든 Buttons을 포함

Private childFormsByButton As New Dictionary(Of Button, frmChild) 

Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click 
    Dim btn = DirectCast(sender, Button) 
    Dim frm = childFormsByButton(btn) 

    frm.StartTimer() 
End Sub 

참고.

자녀 양식에 StartTimer 메서드를 작성하고 TimerStart 메서드를 호출해야합니다.