call method in container control

pilsdumps

Member
Joined
Jul 27, 2009
Messages
18
Programming Experience
3-5
Hi,

I've created a simple user control (uc_ok_cancel_apply) that includes OK, Cancel and Apply buttons. I want to use this on various user controls or forms so I can provide a consistent look.

If I add this to a user control, I can call a public method by referencing the form via the 'my' namespace
VB.NET:
Private Sub btn_OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_OK.Click

        'call the handling sub directly
        My.Forms.frm_home.Uc_user1.OK_clicked()

    End Sub

where uc_user1 is the name of the user control in which uc_ok_cancel_apply is contained.

However, I want the uc_ok_cancel_apply to call a public function on its parent control without having to resort to hard coding the reference. I thought I could use the 'parent' property eg. me.parent.parent.OK_clicked(), but I don't seem to be able to view the public sub.

Can anyone tell me how I can call a sub in the control that contains my user control? Do I need to use delegates and raiseevents? I've tried this but still don't seem to be able to get it work.

Thanks
 
Put a Public event on the UC, then raise it in the button click event on the UC, then when you create the UC on the main form use the addhandler to point to a sub on the main form that calls that function - when they click the button it will fire this sub:
VB.NET:
'---UC
Public Event ButtonOk()
'---ok_btn_click event
RaiseEvent ButtonOK()
'---Main form
Dim uc As New Usercontrol
Addhandler UC.ButtonOk(), AddressOf ButtonOk

Private Sub ButtonOk()
'call function
End Sub
 
Back
Top