Inside my Main method I’m instantiating the UpdateDialog class inside which based on if the user presses a button or not I need to call function1() from Main. Here is the code:
public partial class Main : Form
{
public void function1()
{
doing_stuff_here();
}
private void button1_Click(Object sender, EventArgs e)
{
var update = new UpdateDialog();
update.ShowDialog();
}
}
public partial class UpdateDialog : Form
{
private void button2_Click(object sender, EventArgs e)
{
//call here function1() from Main
}
}
What should I do to be able to call function1() from Main inside the partial class UpdateDialog?
LE: although the method suggested by Styxxy seems right it doesn’t work well in my app because of cross-thread invalid operation so I ended up using the delegate workaround suggested by Cuong Le.
You’ll have to have an instance of the
Mainform in yourUpdateDialogform. As you say that UpdateDialog is a child form of your Main form, I guess that you create the UpdateDialog in your Main form and do a show there. Before showing that form, you could assign theParentproperty.You get the error
ArgumentException: Top-level control cannot be added to a control.. Now this can be solved in two ways.TopLevelproperty tofalseon your Main form (I’m not a huge fan of this).Ownerproperty to your Main form (this). Below two ways of doing it.You can set the
Ownermanually:Or you can add
thisas parameter to theShow(owner)orShowDialog(owner)methods; this way, theOwneris also being set.“Full” code makes this: