raising events from an assembly

k3n51mm

Active member
Joined
Mar 7, 2007
Messages
40
Programming Experience
3-5
Wow, the sticky from JuggaloBrotha almost made me not post this, but I guess I have to since I'm facing a deadline. If there's no example for how to use that component, it's not much help anyway. So, please forgive me if I make a political error by asking this question, which is actually more generalized anyway.

I've built an assembly (a login dialog box) that has a component(login.vb) and a form(frmLogin.vb) compiled together. The component calls a ShowDialog() function that displays frmLogin.

Inside the frmLogin I've declared a public property called Picture that is supposed to Get and Set a picturebox image that is one of the form's controls. This is so the assembly can be customized to different applications our company owns. I have also declared this property in the component(login.vb).

I have also declared public events in frmLogin that are supposed to be fired when 1)the Login button or 2)the Cancel button are clicked. This is so I can 1)return the two values in the login and password textboxes on the form(frmlogin), or 2)close the calling application if the user clicks Cancel.

QUESTION: After adding the component to another application and setting the Picture at design time, the image I browsed to and selected from the property window of the component is not getting loaded into the picturebox (no errors are thrown). Why?

QUESTION: How do I add event handlers to a calling application to receive the events from the assembly? Are the events also supposed to be declared both in the form itself and the component that contains the ShowDialog() function? I've tried various ways and places to add the Events, and so far I've come up empty because I'm just guessing.

NOTE: I've never done this before and I'm over my head, so please try to come up with a detailed answer.

THANKS
 
I don't see how use of event is appropriate here. Think of the ColorDialog component/dialogform, you call the ShowDialog method and if its result is 'OK' you retrieve the property values you need. There is no need for event because the dialog is displayed modally and when execution continues the information is readily available from that same call.

Example component:
VB.NET:
Class login
    Inherits System.ComponentModel.Component
 
    Private loginform As New frmlogin
 
    Public Function ShowDialog() As DialogResult
        Return loginform.ShowDialog
    End Function
 
    Public Property Picture() As Image
        Get
            Return loginform.pb.Image
        End Get
        Set(ByVal value As Image)
            loginform.pb.Image = value
        End Set
    End Property
 
    Public ReadOnly Property login() As String
        Get
            Return Me.loginform.tbPass.Text
        End Get
    End Property
 
    Public ReadOnly Property pass() As String
        Get
            Return loginform.tbPass.Text
        End Get
    End Property
End Class
Example loginform, I just put the Friend controls Picturebox and 2 Textboxes you here so you'd see how the login component that holds an instance of this form use these child controls in its properties - you normally just add them controls to form. What you need to do here is to select your OK and Cancel buttons in Designer and change the DialogResult property for each, one is value 'OK' the other value 'Cancel', this ensures that the correct dialogresult value is passed back to the one that calls ShowDialog method. If the your loginform was created from the Dialog form template, the configuration of form and buttons for dialog usage was already done for you.
VB.NET:
Class frmlogin
    Inherits Form
    Friend pb As New PictureBox
    Friend tbPass As New TextBox
    Friend tbLogin As New TextBox
End Class
Then there is the usage example, I just create a new instance of login component, show the dialog and retrieve values. When you add component to form the instance is created that you use, for example 'login1'.
VB.NET:
Sub usage()
    Dim l As New login
    If l.ShowDialog = Windows.Forms.DialogResult.OK Then
        MsgBox(l.login)
        MsgBox(l.pass)
    End If
End Sub
Of course you may add all the events you like, remember the class defines and raises its own events, the user can create an instance of a class and subscribe to its events.
 
Thank you very much for the response John, I will study your post closely.

Note: the reason I wanted to have events was in case we needed to have custom handlers in the calling application.
 
I decided to follow your advice and forego the Events due to the simplicity of this control. It seems to be working ok, except one weird thing - I have to click the OK button twice to make the dialog box disappear. When I click Cancel, I only have to click it once.

The only difference between the buttons is that I have a boolean checkInput() function that makes sure the user enters values for both login and password.

I have used F8 to watch the program execute, and it simply goes through the processing twice. I can't figure out why. Any ideas?
 
Hard to tell without seeing the code, it is not a known issue for me. Any chance you could post a sample project that feature the problem?
 
Back
Top