Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
How to call a form method from a usercontrol
Message
From
31/08/2003 19:09:40
 
 
To
31/08/2003 07:46:46
Keith Payne
Technical Marketing Solutions
Florida, United States
General information
Forum:
ASP.NET
Category:
Forms
Miscellaneous
Thread ID:
00824465
Message ID:
00824984
Views:
40
Keith,

Yep, there are usually several ways to skin a cat ... it's just good to have a coupla tricks up your sleeve so you have the choice of several options and can decide what's best for any given situation.

~~Bonnie



>In VB, you could use an interface. However, there is an alternative that is much simpler IMO. You declare a public event in the class block of the control. Then, in the container class, you declare a sub that handles the event. To fire the event, you RaiseEvent in the control:
>
>
>Public MustInherit Class SearchForm
>    Inherits System.Web.UI.UserControl
>
>    Protected WithEvents btnSearch As System.Web.UI.WebControls.Button
>    Public Event GoSearch As EventHandler
>
>    Private Sub btnSearch_Click(ByVal sender As System.Object, _
>        ByVal e As System.EventArgs) Handles btnSearch.Click
>
>        RaiseEvent GoSearch(Me, e)
>    End Sub
>End Class
>
>Public Class Form1
>    Inherits System.Web.UI.Page
>
>    Protected WithEvents SearchForm As SearchForm
>
>    Private Sub SearchForm_GoSearch(ByVal sender As System.Object, _
>        ByVal e As System.EventArgs) Handles SearchForm.GoSearch
>
>    End Sub
>End Class
>
>
>In this example, the button "btnSearch" is bubbling its Click event to its container, the UserControl SearchForm. The UserControl then bubbles the GoSearch event to its container, the Page Form1.
>
>This method seems simpler than creating an interface. And one of the side benefits is whether or not anything happens in the container object is completely up to the container. It is not necessary to determine if a method in the container object is hooked into the event.
>
>*** Important for anyone using this as a template...
>You have to add the reference to the user control by hand - "Protected WithEvents SearchForm As SearchForm", VS.NET does not create the reference for you when you drop the user control onto the form.
>
>>Keith (and yag),
>>
>>>>The "proper" way to do this is to define an event in the button and bubble it up to the usercontrol. Then the usercontrol bubbles the event to the parent container.<
>>
>>Yeah, I was gonna comment on this earlier, but since we're speaking VB here, I wasn't sure of the syntax, so I kept my mouth shut. But now I'd like to comment ...
>>
>>In C#, I would rely on an interface for working with this button. In other words, have an interface called IAbc (since Robert initally said his method was called ABC) and be sure that every form where that button is placed implements the IAbc interface. That way the button will know that the me.parent.ABC() method always exists (in addition, the button should probably check that me.parent actually *does* implement the IAbc interface).
>>
>>Anyway, it's all well and good of me to speak of implementing interfaces, and I know exactly how to do this in C#, but I haven't a clue of the syntax to do it in VB, so someone else might want to jump in here and spread the word. <g>
>>
>>~~Bonnie
>>
>>
>>>
>>>>Me. on a form will reference that form. That includes events of controls, since the event simply calls up to form methods. So Me.MyMethod() or whatever should do it.
>>>>
>>>>Hope this helps.
>>>>
>>>>yag (sitting in an airport)
>>>>
>>>>>I have a usercontrol that contains a button. I want to place this button on several different forms. When the button is pressed I want the logic to check to see if a method exist on the form containing the usercontrol called "ABC". If so execute it.
>>>>>
>>>>>In FoxPro you could do this by referencing thisform.
>>>>>
>>>>>How would you do this in VB.NET?
>>>>>
>>>>>TIA
>>>>>Robert
Bonnie Berent DeWitt
NET/C# MVP since 2003

http://geek-goddess-bonnie.blogspot.com
Previous
Reply
Map
View

Click here to load this message in the networking platform