Usercontrol event

David_nyh

Member
Joined
Mar 4, 2005
Messages
16
Programming Experience
Beginner
Hi I have a problem with my usercontrol. I used this example from this board: http://www.vbdotnetforums.com/showthread.php?t=6754&highlight=RaiseEvent+switchTo%28Me%2C+MyApplication.MyForms.

But I have the buttons in my usercontrol and it give my a fault:

Fault:
Value of type 'TestWindows.usrNavigation' cannot be converted to 'System.Windows.Forms.Form'.

On this codeline:
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Event[/COLOR][/SIZE][SIZE=2] switchTo([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] ssender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] formName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MyForms)[/SIZE]
 
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] LinkLabel1_LinkClicked([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Object, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] e [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.LinkLabelLinkClickedEventArgs) [/SIZE][SIZE=2][COLOR=#0000ff]Handles[/COLOR][/SIZE][SIZE=2] LinkLabel1.LinkClicked[/SIZE]
[SIZE=2][COLOR=#0000ff]RaiseEvent[/COLOR][/SIZE][SIZE=2] switchTo([/SIZE][SIZE=2][COLOR=red][U]Me[/U][/COLOR][/SIZE][SIZE=2], MyApplication.MyForms.frmStart)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]

I understand that the usercontrol don't recognize 'Me'. But how can I let it work?

Thanks

David_nyh

 
Usercontrol recognize 'Me', it is a reference to the current instance of the class. Your switchTo event declares first parameter ssender As Form, when you try raiseevent passing 'Me' you are passing an instance of type UserControl/Control. A usercontrol is not of type Form or derived from this type. For example change the event signatures ssender parameter to type Control.
 
So, do you actually subscribe to this event from the class that use this control?

..typically:
VB.NET:
Private Sub UserControl1_switchTo(ByVal ssender As control, ByVal formName As MyForms) _
handles usercontrol1.switchto
  'code
end sub
 
In every form I have this code:
VB.NET:
[SIZE=2]
[/SIZE][SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Event[/COLOR][/SIZE][SIZE=2] switchTo([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] sender [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Form, [/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] formName [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] MyForms)
[/SIZE]
And the usercontrol.

Where do I need to subscribe this event?

Thanks

David_nyh
 
Think about the Button control. The button control have events. When you add a button to a form you can go into code and select the button instance then select the event you want to handle, this result in the event handler method for the selected event being generated in code. Typically you get this:
VB.NET:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click
 
    End Sub
Do you see the similarity to my previous example for the usercontrol event handler?

It it the same when you create a usercontrol, and define a public event in this control. You add a new instance of the control to the form, then go to code of form and select the control and event to handle. With the generated event handler method you must write code for what you want to do when the event happens.
 
Back
Top