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.
gabosAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
käµfm³d 👽Commented:
Haha...   Nice catch Idle_Mind. Guess I should check before I hit the submit button  ; )
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

gabosAuthor Commented:
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();

           
        }

    }

}
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Get rid of these lines:

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

Then use ShowDialog() instead of Show():

        private void btnAppetizers_Click(object sender, EventArgs e)
        {
            frmAppetizers frm = new frmAppetizers();
            if (frm.ShowDialog() == DialogResult.OK);
            {
                lblAppetizers.Text = frm.dblAppetizers.ToString(); // <-- Notice I'm using your instance called "frm"!
            }
        }

Finally, over in frmAppetizers, set DialogResult instread of hiding it:

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

That's it!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
gabosAuthor Commented:
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?
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hitting it once should suffice.  Please show us the current code...  
0
gabosAuthor Commented:
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.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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?
0
gabosAuthor Commented:
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.

0
gabosAuthor Commented:
This solution is great!
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
You could write it like this if you prefer:

            frmAppetizers frm = new frmAppetizers();
            DialogResult result = frm.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                ...
            }
0
gabosAuthor Commented:
I'm not sure what the difference is though.
It seems like the extra line of code is not needed.
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
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.
0
gabosAuthor Commented:
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.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.