[RESOLVED] Calling a Sub of the MainForm from a UserControl
Hi,
I'm using a UserControl-Collection on my Mainform.
Inside those UserControls I want a ClickEvent to call a _MouseUp Routine of the Mainform.
I was able to get it working by declaring this _MouseUp Routine as Public and caling it by
Code:
MainForm.PictureBox_MouseUp(MainForm.PictureBox, myMouseEventArg)
I do understand that that is not a porper way to do, however I'm struggeling on how to do it correctly. Could anybody give me a hint?
Re: Calling a Sub of the MainForm from a UserControl
it's not ideal. it'd be better to put a public sub in mainform that both PictureBox_MouseUp + your uc call.
Code:
DirectCast(Me.FindForm, MainForm).PictureBox_MouseUp(MainForm.PictureBox, myMouseEventArg)
Re: Calling a Sub of the MainForm from a UserControl
Ideally what you should be doing is raising an event from the UC to the container form, and then the containter form calls the sub it needs to.
-tg
Re: Calling a Sub of the MainForm from a UserControl
@tg
How would I handover data using the event technique?
@paul:
Thanks for that one, such a trick didn't come to my mind. However I have a working way which isn't ideal either.....
Re: Calling a Sub of the MainForm from a UserControl
I don't think you ever want a usercontrol to be referencing something like MainForm. If you do that then the UserControl is dependent on the existence of something called MainForm. You may not believe that you will ever use this usercontrol anywhere else, and perhaps you will not, but if you tie it so intimately to a form with a certain name, then you NEVER will be able to use it anywhere else without modifying it.
You may be able to get at the form you need through Me.Parent, if the parent of the usercontrol is the form. Alternatively you might pass a Delegate to the User Control so that it can call that delegate when it needs to.
Better still would be to have the usercontrol raise an event, even a custom event if a different e argument is needed, and have the mainform handle that event. After all, the UC is a Control and should behave like any other control (I make an exception for XNA drawn controls, but not for User Controls). The way the mainform interacts with any other control is that it can handle events raised from the control. So, you might have the UC raise a Click Event, except that the typical Click Event usually just has an EventArgs argument as e, and you almost certainly want something more like the MouseEventArgs argument, so I wouldn't call the event Click, but MouseUp. Then the MainForm would be handling UC_MouseUp.
Re: Calling a Sub of the MainForm from a UserControl
Quote:
Originally Posted by
opus
@tg
How would I handover data using the event technique?
@paul:
Thanks for that one, such a trick didn't come to my mind. However I have a working way which isn't ideal either.....
you create your own custom Event Arg class, let it inherit from the basic SystemEventArg class, add your own properties to hold all the data to send back from the UC to the form... think about it... that's what the Click event does... it passes back a SystemEventArg class and raises the Click event... and handler then listening to the event will then fire off. There isnt' anything in the button that says Form1.ButtonClick ... or Me.Parent.CallButtonClick. ... a UC should be the same...
-tg
Re: Calling a Sub of the MainForm from a UserControl
@Shaggy Hiker
Thanks for the input, you stated the exact reasoning for the change my working solution into something better. Reading a lot of threads in here trigered me to do this change.
Since I already have the needed arguments to hand over, all I need to do is create the event that is raised by the UC and let the parent form react on it.
Will rreport when done, Thanks to you all.
Re: Calling a Sub of the MainForm from a UserControl
It's done and working!
I've done
In UserControl:
Declaring Public Event by
Code:
Public Event PublicEventUserGram As EventHandler
Raising the Event in correct Event-Routine with the correct MyMouseEventArg by
Code:
RaiseEvent PublicEventUserGram(Me, MyMouseEventArg)
In the parent form:
Changing the PictureBox_MouseUp to be Private
When creating new instances of the UserControl adding the EventHandle by
Code:
AddHandler NewUserGram.PublicEventUserGram, AddressOf PictureBox_MouseUp
Thanks everybody
(Sorry can't rate both of you at the moment, I guess I took too much of your help lately)