Results 1 to 5 of 5

Thread: UserControl get instance from another form

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    UserControl get instance from another form

    Hello, I have am creating a custom text box using a usercontrol. I created a FindReplace dialog, and the usercontrol will display the dialog when the user presses CTRL+F.

    However, how do I access that instance of the usercontrol? From the FindReplace dialog wouldn't making a call to the usercontrol only get the base instance?

    How do I address the specific instance of the usercontrol that called the FindReplace dialog? Thanks.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Ancient City, U.S.
    Posts
    1,254

    Re: UserControl get instance from another form

    If you create a usercontrol and call it, say, 'myUserControl', and then add it to a form, that form will contain an instance of 'myUserControl', not 'usercontrol'. The instance you have of myUserControl - say you call it 'myUserControl1' (although you could call it anything. You could call it fred if you wanted to. The name doesn't matter to VB) would have all the controls, properties, methods, and events that you placed on it when you designed it. So then you would access that instance of myUserControl through the name 'myUserControl1' - or whatever you decided to name it. There wouldn't be an instance of usercontrol and myUserControl on the form - all there would be would be an instance of myUserControl.

    When a child class inherits from a base class, and you create an instance of the child class, there is only one instance of the class - the child class. There are not two instances of the class - the base and the child. The two become one.
    I always add to the reputation of those whose posts are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    Re: UserControl get instance from another form

    I'm saying the user control project also has a dialog in it. I want the dialog to be able to access the usercontrol's methods.

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

    Re: UserControl get instance from another form

    Quote Originally Posted by theryan722 View Post
    I'm saying the user control project also has a dialog in it. I want the dialog to be able to access the usercontrol's methods.
    Why? What does the dialog need to know about the UC? The dialog is essentially a child of the UC, it shouldn't need to know or care about the UC... the UC can use the dialog, but I'm not sure it should be the other way around.

    That said, you could try the .Parent property, or the UC can pass a reference of itself to the dialog when it creates it... but I'm not sure either is the optimal design in the first place.

    Consider the OpenFileDialog component, it doesn't know or really care what container it is on... it is simply a data entry tool... it takes input from the user and passed it to the calling code.

    -tg
    Last edited by techgnome; Jun 23rd, 2015 at 07:39 PM.
    * 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??? *

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2012
    Posts
    70

    Re: UserControl get instance from another form

    It is the Find Replace dialog, so you need to be able to select "Find Next", "Highlight All", etc.

Tags for this Thread

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