[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-10-17
15
Medium Priority
?
270 Views
Last Modified: 2012-05-12
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.
0
Comment
Question by:gabos
  • 7
  • 6
  • 2
15 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36980416
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36980462
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36980480
Haha...   Nice catch Idle_Mind. Guess I should check before I hit the submit button  ; )
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gabos
ID: 36983707
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 36983771
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
 

Author Comment

by:gabos
ID: 36985636
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36985761
Hitting it once should suffice.  Please show us the current code...  
0
 

Author Comment

by:gabos
ID: 36986250
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36986270
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
 

Author Comment

by:gabos
ID: 36986305
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
 

Author Closing Comment

by:gabos
ID: 36986310
This solution is great!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36986363
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
 

Author Comment

by:gabos
ID: 36986460
I'm not sure what the difference is though.
It seems like the extra line of code is not needed.
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36986490
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
 

Author Comment

by:gabos
ID: 37000500
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question