What I’m trying to make is a search window exactly the same as in VS or Notepad++, where both windows are active (because the FindBox is shown with Show not ShowDialog), and when you press find in the FindBox, the parent performs the search. Here’s an example:
class MainForm : Form
{
public void FindNext(string find)
{
// Do stuff
}
public void OpenFindWindow()
{
FindBox find = new FindBox();
find.customParent = this;
find.Show();
}
}
class FindBox : Form
{
public customParent;
public void FindButtonPressed()
{
((MainForm)customParent).FindNext(textBox1.text);
}
}
But it seems strange I have to manually set this new field “customParent”. What is the official way to do something like this?
You could use
and use the
Ownerproperty offindto access the parent form. Of course you’ll have to cast it toMainFormin this case:This approach also has the advantage that the main form cannot hide the find box (owned forms will always be displayed on top of their owners, even if the owner has the focus …)