secretTina
secretTina

Reputation: 25

passing a variable into another form

this probably is very simple. This is an assignment for a class, there seems to be several versions of this floating around, and several versions of an answer but I'm not sure how they work. The assignment is to create two forms. One with a dorm price and meal price and the second form is to display the total price. I want the price to display into a input label on the price form. Except I'm not sure how to get the information from one to the other. Does this require me doing a get/set somewhere within my Calculator form? This is form 1(Calculator) code:

public partial class Calculator : Form
{
    Price myPrice = new Price();
    decimal dorm = 0;
    decimal meal = 0;

    public Calculator()
    {
        InitializeComponent();
    }

    private void getPriceButton_Click(object sender, EventArgs e)
    {
        decimal price = 0;

        getInput();

        price = dorm + meal;

        myPrice.ShowDialog();


    }

    private void getInput()
    {
        if(allenRadioButton.Checked)
        {
            dorm = 1500;
        }

        if(pikeRadioButton.Checked)
        {
            dorm = 1600;
        }

        if(farthingRadioButton.Checked)
        {
            dorm = 1800;
        }

        if(universityRadioButton.Checked)
        {
            dorm = 2500;
        }

        if(sevenRadioButton.Checked)
        {
            meal = 600;
        }

        if(fourteenRadioButton.Checked)
        {
            meal = 1200;
        }

        if(unlimitedRadioButton.Checked)
        {
            meal = 1700;
        }
    }

This is form2 (Price) code:

public partial class Price : Form
{
    Calculator myCalulator = new Calculator();

    public Price()
    {
        InitializeComponent();
    }

    priceLabel.Text = price.myCalculator.TosString("c");
}

Upvotes: 0

Views: 1808

Answers (3)

Shalinka Randeniya
Shalinka Randeniya

Reputation: 173

You can Do this simply by adding a variable and making it public in the windows form like this.

public int PassValue

And pass a value to it, before the form is called

form1 obj = new form1();
obj.PassValue = 34;
obj.Show();

Upvotes: 0

Krishna Kumar N
Krishna Kumar N

Reputation: 135

Pass the price as a parameter to a new parameterised constructor of the Price form and use this for further operations.

private decimal _price;

public Price(decimal pPrice)
{
  InitializeComponent();
  _price = pPrice;
  ...
}

Also instantiate the myPrice object in your button click event with this new constructor. like;

Price myPrice = new Price(price);
myPrice.ShowDialog();

For other ways to pass the value from one form to other, please refer to this link; How can I pass values from one form to another? and also Passing Parameters back and forth between forms in C#

Upvotes: 0

Katia
Katia

Reputation: 629

you can make a variable of price in second form and pass your price to the constructor of Price form:

    public string price;
    public Price(string price)
    {
        this.price = price;
        InitializeComponent();
    }

    private void getPriceButton_Click(object sender, EventArgs e)
    {
        decimal price = 0;
        getInput();
        price = dorm + meal;
        Price myPrice = new Price(price)
        myPrice.ShowDialog();
    }

Upvotes: 2

Related Questions