Link to home
Start Free TrialLog in
Avatar of gabos
gabos

asked on

C#: What is the simplest way of passing a value from one form to another

I'm trying to code a restaurant biller. The idea is that for each food category, you can display the menu items on a different form, sum those items up, then put it on the calling form. The question is how can you pass a value from the second form into a label on the first form?

My first form is called frmMenu and my second form is called frmAppetizers. I have a double type value that needs to be passed from the frmAppetizers form, to the first one called frmMenu and displayed in a label.
Avatar of kaufmed
kaufmed
Flag of United States of America image

Haha...   Nice catch Idle_Mind. Guess I should check before I hit the submit button  ; )
Avatar of gabos
gabos

ASKER

Ok, maybe a bit more help is needed.

Basically, I am creating an instance from the first form (frmMenu). Then, I am summing two numbers. I want to output that sum back onto a label on frmMenu. I know I am close, so if someone can take a look at my actual code and let me know what I am missing:

frmMenu:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmMenu : Form
    {
        public frmMenu()
        {
            InitializeComponent();
        }

        private void btnAppetizers_Click(object sender, EventArgs e)
        {
            frmAppetizers frm = new frmAppetizers();
            frm.Show();

            lblAppetizers.Text = mainForm.strAppetizers;
        }

        private frmAppetizers mainForm = null;
        public frmMenu(Form callingForm)    
        {
            mainForm = callingForm as frmAppetizers;          
            InitializeComponent();
        }
   
     }

}



frmAppetizer:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmAppetizers : Form
    {
        // Global Variables
        public double dblAppetizers = 0;
        public string strAppetizers = "";
     
        // The Appetizer Menu
        public frmAppetizers()
        {
            InitializeComponent();
        }
       
        //The action event that triggers everything
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //The main function is to add to values and output into a variable
            dblAppetizers = (double.Parse(txtWings.Text)) + (double.Parse(txtRings.Text));
           
            //sets the variable to a string variable
            strAppetizers = dblAppetizers.ToString();

            //Debugging action. Helps to know if the values are being added correctly and to see it output somewhere.  
            MessageBox.Show(strAppetizers);

            //Hides this form
            this.Hide();

           
        }

    }

}
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gabos

ASKER

Thats great! I wish I came up with that to begin with. One slight bug, I have to click the submit button on frmAppetizer twice to get it in the label on frmMenu.

Do I need to add a message box, or is there any way to hit the submit button once, then have the variable placed in the label?
Hitting it once should suffice.  Please show us the current code...  
Avatar of gabos

ASKER

frmAppetizers:

using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmAppetizers : Form
    {
        public double dblAppetizers = 0;
        public double dblRings = 10;
        public double dblWings = 24;

        public frmAppetizers()
        {
            InitializeComponent();
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            //The main function is to add to values and output into a variable
            dblAppetizers = (double.Parse(txtWings.Text)) + (double.Parse(txtRings.Text));

            // Set DialogResult to return execution to the main form:
            this.DialogResult = DialogResult.OK;

        }
       
    }
}


frmMenu:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class frmMenu : Form
    {
       
        public frmMenu()
        {
            InitializeComponent();
        }

        private void btnAppetizers_Click(object sender, EventArgs e)
        {
            frmAppetizers frm = new frmAppetizers();
            frm.ShowDialog();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                lblAppetizers.Text = frm.dblAppetizers.ToString();
            }
           
 
        }

     }
}

I'm also getting a marshal-by-reference warning. I'm not sure if that will be an issue.
You're showing the Appetizers from TWICE!

            frm.ShowDialog(); // <-- Get rid of this line
            if (frm.ShowDialog() == DialogResult.OK)
            {

Get rid of that first ShowDialog() call.  =)

Where is the warning pointing to?
Avatar of gabos

ASKER

Great! I didn't realize you could open and close a form from an if statement like that.

It works flawlessly now! I'm so glad. Thank you Idle_Mind and kaufmed. I really appreciate the help.

Avatar of gabos

ASKER

This solution is great!
You could write it like this if you prefer:

            frmAppetizers frm = new frmAppetizers();
            DialogResult result = frm.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                ...
            }
Avatar of gabos

ASKER

I'm not sure what the difference is though.
It seems like the extra line of code is not needed.
It's just a more verbose way of doing the same thing.  The return value of ShowDialog() is stored in another locally declared variable and then you use variable in the if statement.

I prefer the original, shorter version.
Avatar of gabos

ASKER

That solution should have been obvious to me, but it got me back on track. I added in a few more buttons, a splash screen, a menu strip items and an output label to add all the elements together. It work pretty nice now.

Idle_Mind: The shorter version seems smoother and easy to follow.