2012-02-16 2 views
1

다른 양식에 의해 보여지는 vb .net winform을 가졌습니다. 나는 frmA.vb, frmB.vb, frmC.vb 및 frmD.vb를 가졌습니다.다중 상위 양식

이 모든 양식은 frmItem.vb를 호출 할 수 있습니다. frmItem.vb를 사용하면 데이터베이스에서 항목을 선택할 수 있으며이 항목은 부모의 속성 설정을 호출하여 전송됩니다. 내가 항목을 추가 할 때 확인을 작동

fA.addItem(item_id) 

:

fi = new frmItem(frmA) 'frmItem has 4 New() methods, frmA.. b... c and d 
    'i need to pass the correct parent. 
    fi.showModal() 

같은

즉, 내가 버튼을 클릭 frmA을 열

(뭔가 그래서, 그것은 호출 내 의심 나는 frmItem을 복제했기 때문에 최적화에 관한 것입니다. 한 복사본은 frmA와 frmB를 관리하고 다른 하나는 frmC와 frmD를 관리합니다. frmItem2에

private fB as frmB 
private fA as frmA 



if parentFrmA is nothing then 
    'Is frmB 
    fB.addItem(item_id) 
else 
    'Is frmA 
    fA.addItem(item_id) 
end if 

그리고 :

frmItem1에서

내가 항목을 전송하도록했습니다, 내가 사용하는 즉, 내가 frmItem1을 수정하는 경우

private fC as frmC 
private fD as frmD 



if parentFrmC is nothing then 
    'Is frmD 
    fD.addItem(item_id) 
else 
    'Is frmC 
    fC.addItem(item_id) 
end if 

, 내가했습니다 frmItem2와 viceversa를 수정하는 것은 마치 하나처럼 보이고 작동해야하기 때문입니다.

네 개의 폼 모두 동일한 Set Property를 가졌지 만 다른 폼과 마찬가지로 frmItem에서 고유 한 Form 클래스를 사용할 수 없습니다.

하나의 양식으로 여러 부모를 쉽게 관리 할 수있는 가능성이 있습니까 ??

자세한 정보가 필요하면 알려주세요. 고마워요

답변

0

나는 당신의 예를 완전히 따라갈 수 없기 때문에, 따라하기가 어렵다고 생각합니다.

그러나 일반적으로 이러한 하위 양식이 상위 양식이 수신 대기중인 이벤트를 제기해야하는 것처럼 들립니다. 그렇게하면, 당신은 당신의 관심사를 조금씩 분리 할 수 ​​있고 이러한 의존성을 하드 코드하지 않을 수 있습니다.

당신은 모범 사례를 따르도록 자신있는 EventArgs 클래스를 만드는 시도 할 수 있습니다

: 공개 이벤트를 할 것이다

자녀 양식

Public Class ChildFormEventArgs 
    Inherits EventArgs 

    Private _ItemID As Integer 

    Public Sub New(ByVal itemID As Integer) 
    _ItemID = itemID 
    End Sub 

    ReadOnly Property ItemID() As Integer 
    Get 
     Return _ItemID 
    End Get 
    End Property 
End Class 
을 영원히이 "추가"일이 일어날 때 당신이 그것을 올릴 것 :

Public Class Form2 
    Public Event ItemAdded(ByVal sender As Object, ByVal e As ChildFormEventArgs) 

    Private _ItemID as Integer 

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
    RaiseEvent ItemAdded(Me, New ChildFormEventArgs(_ItemID)) 
    End Sub 
End Sub 

그리고 그런 다음 부모 폼은 하나 청취하고 그에 따라 행동 할 수

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 
    Using testForm As New Form2() 
    AddHandler testForm.ItemAdded, AddressOf ChildForm_ItemAdded 
    testForm.ShowDialog(Me) 
    RemoveHandler testForm.ItemAdded, AddressOf ChildForm_ItemAdded 
    End Using 
End Sub 

Private Sub ChildForm_ItemAdded(ByVal sender As Object, ByVal e As ChildFormEventArgs) 
    '// do something here. 
    '// sender is the child form that called it 
    '// e is the event arguments that contains the ItemID value 
End Sub 
+0

와우, 내가 만년설했습니다 이러한 종류의 물건을 사용하십시오. 시도해 볼게. 감사! – Jaxedin