Reputation: 175
I have 3 forms. The second is for displaying help, the third is a modal dialog.
In Form1 I have:
Form2 HelpForm = new Form2();
Form3 EditForm = new Form3();
and a Help menu that just does:
HelpForm.Show();
I would like to show HelpForm also from a control in Form3. How can I do that?
Upvotes: 0
Views: 619
Reputation: 21
Pass HelpForm object in the constructor of EditForm and assign the same in private variable. Invoke show when needed using the variable.
Form2 HelpForm = new Form2();
Form3 EditForm = new Form3(HelpForm);
.....
EditForm.ShowHelp();
With in Form3:
Class Form3
{
private Form2 helpForm = null;
public Form3(Form2 HelpForm)
{
helpForm = HelpForm;
}
public void ShowHelp()
{
helpForm.Show();
}
}
Upvotes: 0
Reputation: 10576
As an option you can use Application.OpenForms
FormCollection fc = Application.OpenForms;
if (fc.OfType<Form3>().Any())
{
fc.OfType<Form3>().First().Show();
}
Upvotes: 0
Reputation: 4646
the help is kind of global and (if your application grows) it should be accessible anywhere. So maybe a (kind of) Singleton for your Help could help!
class Help
{
private static Form helpForm = null;
static void ShowHelp {
if (helpForm == null)
helpForm = generateHelpForm(); //TODO implement
helpForm.Show();
}
access it from anywhere...
// somewhere in formx
Help.ShowHelp();
Upvotes: 1