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 can either accept the customParent as a argument in the constructor, or better yet, the FindBox form should take a Action<string> that it will call once the find button is pressed.
class MainForm : Form
{
public void FindNext(string find)
{
// Do stuff
}
public void OpenFindWindow()
{
FindBox find = new FindBox(this.FindNext);
find.Show();
}
}
class FindBox : Form
{
private Action<string> callback;
public FindBox(Action<string> callback)
{
this.callback = callback;
}
public void FindButtonPressed()
{
callback(textBox1.text);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With