1

I am developing a windows app in c#. I have used three decimal variables: counter, narrow, and broad which store different values based on some calculations.

On clicking a button, a message box is displayed showing these three decimal values and then the application exits..

Now I want to add another form having three labels in which these variable values needs to be shown. Please explain, how can I pass those variables in the next form to display in individual labels?

5 Answers 5

3

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();
Sign up to request clarification or add additional context in comments.

4 Comments

I think you lose designer support when you do this though.
Ive never had issues with it.
I do this a lot too, the designer doesn't have a problem. Maybe if you hid the default constructor problems might occur, though.
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.
1

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();
}

3 Comments

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.
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
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!!
1

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();
    }

Comments

0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.