Results 1 to 8 of 8

Thread: [RESOLVED] Calling a Sub of the MainForm from a UserControl

  1. #1

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    Resolved [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?
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    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)

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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.....
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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.
    My usual boring signature: Nothing

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Calling a Sub of the MainForm from a UserControl

    Quote Originally Posted by opus View Post
    @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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

  8. #8

    Thread Starter
    I don't do your homework! opus's Avatar
    Join Date
    Jun 2000
    Location
    Good Old Europe
    Posts
    3,863

    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)
    Last edited by opus; Dec 7th, 2013 at 09:55 AM.
    You're welcome to rate this post!
    If your problem is solved, please use the Mark thread as resolved button


    Wait, I'm too old to hurry!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width