|
-
Jun 23rd, 2015, 03:54 PM
#1
Thread Starter
Lively Member
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.
-
Jun 23rd, 2015, 04:39 PM
#2
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
-
Jun 23rd, 2015, 07:31 PM
#3
Thread Starter
Lively Member
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.
-
Jun 23rd, 2015, 07:35 PM
#4
Re: UserControl get instance from another form
 Originally Posted by theryan722
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.
-
Jun 23rd, 2015, 07:58 PM
#5
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|