Newbie question please help

  • Thread starter Thread starter jed
  • Start date Start date
J

jed

I have a 2 windows forms the first form needs to retrieve a numeric
value in a textbox on the second
form.The second form is a calculater and i need to send the calculated
value in the textbox back to form1 when i click the calculate key.Is
there a quick way to do this.Thanks
 
I would have the method that calls the calculator form like this:

CalculatorForm calForm = new CalculatorForm();
if(calForm.ShowDialog() == DialogResult.OK)
{
int result = calForm.Result;
}


In the calculator form have an ok button and have the following in the
button click event handler method:

this.DialogResult = dialogResult.OK;

(have a similar one to catch a cancel button click).


You also need this in the calculator form:

public int Result
{
get{return int.Parse(textBox1.text);}
}


HTH,
Calum
 
A simple way is to think what you want from the another form, a result.
So you make a public method on that form that retrieves the result, like:

(Form 2):

public int GetCalcResult()
{
this.ShowDialog();
return int.Parse(textBox1.Text);
}

Now all you have to do is call the Form2.GetCalcResult() to get the result
from the form 1:

(form 1)
using (Form2 calculator = new Form2())
{
int result = calculator.GetCalcResult();
}

That's it.
 
One more way is to use events
Form 1 owns a form2 object which it instantiates and displays 'normally'
i.e. Not as a modal dialog box
When you click the button on Form 2 it raises an event carrying the result
as its payload.
You write an eventhandler in form1 which responds to the event and unpacks
the result.
This allows the interaction between form1 and form2 to be asynchronous.

regards
Bob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top