Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42220

Event Handlers for a Usercontrol

$
0
0
Hi

I have usercontrol that im instantiating in another project(same assembly), im displaying it into a panel control at the moment.

im trying to add a handler on the parent form to listen for the events from the usercontrol.

heres the simple assembly plan

prj1 - handles the gui to the user, creates the usercontrol object and displays in a panel, need to handle its events here
prj2 - works with prj1 and another prj3 as a middle man, contains interfaces and usercontrols for the interfaces, no forms on this prj are actually used, just stored for use by prj1

i was searching through google and found a couple of snippets but im not sure what one of them is doing

First is

Code:

Public Event Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1_Click
        RaiseEvent ButtonClicked(sender, e)
End Sub

i think i get this, if i create an event called ButtonClicked, it will run the code there when the button is clicked right. will this be ok as long as im creating a new userform object when im displaying the control inside my form on another project.



Second piece of code is

Code:

Public Delegate Sub AuthorSelected(sender As Object, e As AuthorSelectionArguments)
Public Event authSelected As AuthorSelected




Protected Sub Page_Load(sender As Object, e As EventArgs)


End Sub


Protected Sub btnSelect_Click(sender As Object, e As EventArgs)
        If authSelected <> Nothing Then
                Dim args As New AuthorSelectionArguments(1)
                authSelected(e, args)
        End If
End Sub




<Serializable> _
Public Class AuthorSelectionArguments
        Inherits EventArgs
        Public AuthorID As Integer = 0
        Public Sub New(authorID As Integer)
                AuthorID = authorID
        End Sub
        Public Sub New()
        End Sub
End Class

and

Code:

Protected Sub Page_Load(sender As Object, e As EventArgs)
        UCAuthors1.authSelected += New ProjName.UserControl.Authors.AuthorSelected(UCAuthors1_authSelected)
End Sub

Sub UCAuthors1_authSelected(sender As Object, e As ProjName.UserControl.Authors.AuthorSelectionArguments)
        Label1.Text = e.AuthorID.ToString()
End Sub

ive not had the chance of doing anything like this before, but does this register events from multiple usercontrols that may be displayed.
could someone explain this a little.

and finally, which would be best suited for my situation, note that i dont think ill ever need more then one usercontrol at a time, but it does need to switch between usercontrols, depending what is selected in a list it needs to load a usercontrol and show it pronto..

thanks

Viewing all articles
Browse latest Browse all 42220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>