0

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");
}
Jamie Rees
  • 7,973
  • 2
  • 45
  • 83
secretTina
  • 25
  • 2
  • 11

3 Answers3

2

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();
    }
Katia
  • 619
  • 6
  • 15
  • I've tried a version of both constructors but it keeps telling me "method must return a type" even though it's a constructor. I'll work on it some more. Thanks a bunch – secretTina Feb 29 '16 at 09:20
  • You need to have it only in Price constructor and pass it during creation of myPrice, and I made a mistake - there should be decimal in your case, not string. – Katia Feb 29 '16 at 09:47
  • that's cool I caught that, i've gotten part of it to work, i have other errors now. you're awesome – secretTina Feb 29 '16 at 09:49
  • Thanks) if this is the correct answer - mark it in order all can see that the case is solved. – Katia Feb 29 '16 at 14:46
0

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#

Community
  • 1
  • 1
0

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