tags:

views:

135

answers:

5

i am developing a windows app in c#..i have used three decimal variables:counter,narrow and broad which stores different values based on some calculations. On clicking a button, a message box is displayed displaying these three decimal values and the application exits..

now i want to add another form having three labels in which these variable values needs to be shown...please elaborate how do i pass those variables in the next form to display in individual labels.....

A: 

An easy way is to use properties. The form you want to pass the values to is just another class.

add something like this to the second form:

public int counter {get; set;}

then from the first form you'd do something along the lines of

Form2 form2 = new Form2();
form2.counter = 1;
form2.ShowDialog();

or something along those lines.

tardomatic
A: 

There is a blog post describing how to do this without using ShowDialog().

Matthew Jones
+3  A: 

One method is to create a new constructor in the 2nd form. THen you can use those values from the 2nd form.

public Form2(decimal x, decimal y, decimal z):this()
{
   this.TextBox1.Text = Convert.ToString(x);
   this.Label1.Text = Convert.ToString(y);
   etc...
};

From main form

Form2 frm2 = new  Form2(x,y,z);
frm2.Show();
JTA
I think you lose designer support when you do this though.
Max Schmeling
Ive never had issues with it.
JTA
I do this a lot too, the designer doesn't have a problem. Maybe if you hid the default constructor problems might occur, though.
overslacked
You only have problems doing this if you don't also have the default constructor. That is the one Visual Studio calls when in design mode.
adrianbanks
+1  A: 

The easiest way is to probably add a new method, lets call it ShowWithDetails:

    public void ShowWithDetails(double Counter, double Narrow, double Broad)
    {
        CounterLabel.Text = Counter.ToString();
        NarrowLabel.Text = Narrow.ToString();
        BroadLabel.Text = Broad.ToString();

        ShowDialog();
    }
overslacked
+1  A: 

Create a new Form...

public class CalculationResultForm : Form
{
    public CalculationResultForm(){}

    public decimal Counter
    {
        set { labelCounter.Text = value.ToString(); }
    }
    public decimal Broad
    {
        set { labelBroad.Text = value.ToString(); }
    }
    public decimal Narrow
    {
        set { labelNarrow.Text = value.ToString(); }
    }

    private void OkButton_Click(object sender, EventArgs e)
    {
        // This will close the form (same as clicking ok on the message box)
        DialogResult = DialogResult.OK;
    }
}

Then within your existing form button click handler...

private void MyButton_Click(object sender, EventArgs e)
{
    CalculationResultForm resultForm = new CalculationResultForm();
    resultForm.Counter = _counter;
    resultForm.Narrow = _narrow;
    resultForm.Broad = _broad;

    resultForm .ShowDialog();

    Application.Exit();
}
Oll
Why the `Application.Exit` call? Otherwise I like the answer, since it would also allow for the form to be displayed non-modally, enabling it to remain visible and continuously getting updated with new values through the properties.
Fredrik Mörk
The original question seems to imply that that is what happens currently - "On clicking a button, a message box is displayed displaying these three decimal values and the application exits" - Obviously it can be removed if not needed
Oll
thanx a ton! its the best answer as it worked well..instead of the ok button can i have a 'print' button which shows the print dialog box..actualy i want the user to print that scorecard directly...i m new in prog so pl help if there can be such commands to perform the action..thanx!!
knowledgehunter