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.
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.
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.
Re: UserControl get instance from another form
Quote:
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
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.