Passing Parameters to Events

Errods

Well-known member
Joined
Dec 17, 2007
Messages
71
Location
Kundapur, Karnataka, Udupi, India.
Programming Experience
1-3
Can user parameters can be passes to Events such as Form_Load..........:)
I have a form where the user inputs a integer and in the next form i want a loop to repete until the count is equall to the integer passed in the previus form.

Can i pass the Integer variable like this.....

form2.Show(i) and the form2_Load event can be something like.......

Private sub form2_Load(byval i as integer .....................)handles.............

Can this be done?????:confused: Is this way Correct?????:confused: Or is there some otheer way...........????????????:confused:

Just want to reduce the use of Variables with Global Scope even Friend Variables............

Thanks for any Help
 
First of all, you're talking about event handlers, not events. Your form raises its Load event and your Form1_Load method handles that event. You cannot change the argument list because it must match the signature of the event its handling. The Form.Load event has two arguments: the first an Object and the second an EventArgs. If you try to change that then the compiler will tell you that the method cannot handle that event.

What you should be doing is either declaring a public property in your Form2 class and setting that before calling Show:
VB.NET:
Dim f2 as New Form2

f2.SomeProperty = i
f2.Show()
or else declaring your own constructor and passing the value when you create the form:
VB.NET:
Dim f2 as New Form2(i)

f2.Show()
In both cases it's up to you to write the code in Form2 to accept and use those values. In both cases it will involve assigning the value to a member variable and then reading that value in your Load event handler.
 
Back
Top