Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I m building a project with two window forms - MainForm and NextForm.
In MainForm I have a method which depending on the response from NextForm will do either one thing or the other;

C#
public void performAction()
{
if(response == true)
{
//do something
}
//the rest of the code
}

I need to get the boolean response from NextForm.

NextForm contains:
C#
public delegate void Confirmation(bool answer);
public event Confirmation sendConfirmation;

private void button1_Click(object sender, EventArgs e)
{
    this.sendConfirmation(true);
    this.Dispose();
}


private void button2_Click(object sender, EventArgs e)
{
    this.sendConfirmation(false);
    this.Dispose();
}

how can I get a response in MainForm without creating an extra method for that?

I will have like 20 methods which will ask for response from NextForm, so that might give a lot of extra code which I would like to avoid...
Posted
Updated 18-Nov-10 22:08pm
v2

Why not us an enum? e.g.

C#
public enum ActionType { button1, button2 /* ...*/ };
public delegate void Confirmation(bool answer, ActionType actionType);
public event Confirmation sendConfirmation;
private void button1_Click(object sender, EventArgs e)
{
    this.sendConfirmation(true, ActionType.button1);
    this.Dispose();
}

private void button2_Click(object sender, EventArgs e)
{
    this.sendConfirmation(false, ActionType.button2);
    this.Dispose();
}


in main form

C#
public void performAction(NextForm.ActionType actionType)
{
   switch(actionType) {
     case NextForm.ActionType.<pre>
Button1;
break;
case NextForm.ActionType.Button2;
break;
}
}


Also in NextForm you could set the tag on each button to the enum e.g.

button1.Tag = ActionType.button1;
button2.Tag = ActionType.button2;


then have one handler

C#
private void any_button_Click(object sender, EventArgs e)
{
    this.sendConfirmation(true, (ActionType)(sender as Button).Tag);
}
 
Share this answer
 
you can have public variables in mainform and assign values to these in nextform, if you state mainform as the owner of the nextform.
alternatively you can use the DialogResult property of forms.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900