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");
}
6
  • Hint: you can add custom constructors to forms. Commented Feb 29, 2016 at 8:55
  • Possible duplicate of Sending variables from Form2 to Form1 Commented Feb 29, 2016 at 8:55
  • make variables static Commented Feb 29, 2016 at 8:55
  • @M.kazemAkhgary ewwww! Commented Feb 29, 2016 at 8:56
  • Possible duplicate of pass a value from one form to another Commented Feb 29, 2016 at 8:57

3 Answers 3

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

4 Comments

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
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.
that's cool I caught that, i've gotten part of it to work, i have other errors now. you're awesome
Thanks) if this is the correct answer - mark it in order all can see that the case is solved.
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#

1 Comment

thanks a bunch everyone...you have no idea how far this has gotten me in a short amount of time. thanks
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();

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.