Windows Develop Bookmark and Share   
 index > Windows Forms Designer > How can I call a function in parent form from within user control?
 

How can I call a function in parent form from within user control?

Hi all,

Can someone please fill me in on this.  I have created a composite control for my windows application and on this control I have a link label.

In the parent form, where the composite control resides, I have a function I would like called when the link label clicked.  For example, user clicks the link label on the composite control and the delete function in the parent form is executed.

What does the code look like to execute that function?  I assume that you use something similar to "Me.ParentForm..." but I cant seem to figure it out.

Any suggestions would be greatly appreciated.
Gregg.
MigrationUser 1  Tuesday, January 06, 2004 11:42 PM
You could try to call the function directly, but I would instead opt for a event based approach. I.e. declare an event in your composite control and raise that event when your linklabel is clicked. You can then trap this event in your parent control/form and call the function from there...
MigrationUser 1  Wednesday, January 07, 2004 8:24 AM
Hey,

Thanks for the reply.  I added a raiseEvent in my composite control and it looks like this:

Public Event DeleteItem(ByVal itemID As Integer, ByVal CustomerID As String, _
ByVal Index As Integer)

Private Sub lknDelete_LinkClicked(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
Handles lknDelete.LinkClicked
        
RaiseEvent DeleteItem(_Item.ItemID, _CustomerID, _Index)
End Sub

-----

Im new to this concept so I dont know if this is the most efficient way of doing this...
_Item.ItemID is the item number of the item I want deleted from database.
_CustomerID is the customer ID number associated to the item to be deleted.
_Index is the index number of the control array I use.

-----

Now in my parent form I have my control array of the composite control.  That works great.  Let say I have 3 items in the "cart" for a customer and I want to delete one.  I am passing in the ItemID, CUstomerID, and index of control array to be deleted.  

I assume the best place to execute this code is in the parent form.  That way I have both the add and delete methods for the control array together.

This is where it gets confusing for me.  I want to call the delete function to remove the item from the database.

So, in my composite control, I have the code above.  When the link label is clicked I am raising the event DeleteItem() and passing in the itemID, customerID, and Index.

Now, how would I catch that event in its parent form?

Thanks,
Gregg
MigrationUser 1  Wednesday, January 07, 2004 2:08 PM

You can use google to search for other answers

Custom Search

More Threads

• My Control's Are Stacked!
• Showing the selected rectangle in the Designer
• Loading class methods in a property dropdown
• Working With MDI Forms
• Windows form designer would not allow InitializeComponent to accept variables and arithmetic manipulations
• Change the boder colour in datagridview
• UserControl custom properties
• set different widths for cells of Tablelayoutpanel
• Problem with Type casting in UITypeEditor
• Data grid enter event...