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.
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.
How to Pass Data Between Forms in .NET
kaufmed was probably trying link to his article here: =)
https://www.experts-exchange.com/Programming/Languages/.NET/A_4322-How-to-Pass-Data-Between-Forms-in-NET.html
https://www.experts-exchange.com/Programming/Languages/.NET/A_4322-How-to-Pass-Data-Between-Forms-in-NET.html
Haha... Nice catch Idle_Mind. Guess I should check before I hit the submit button ; )
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.Tex t)) + (double.Parse(txtRings.Tex t));
//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(strAppetiz ers);
//Hides this form
this.Hide();
}
}
}
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
{
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.Tex
//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(strAppetiz
//Hides this form
this.Hide();
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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?
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...
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.Tex t)) + (double.Parse(txtRings.Tex t));
// 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.
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.Tex
// 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
{
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?
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?
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.
It works flawlessly now! I'm so glad. Thank you Idle_Mind and kaufmed. I really appreciate the help.
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.Dialo gResult.OK )
{
...
}
frmAppetizers frm = new frmAppetizers();
DialogResult result = frm.ShowDialog();
if (result == System.Windows.Forms.Dialo
{
...
}
ASKER
I'm not sure what the difference is though.
It seems like the extra line of code is not needed.
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.
I prefer the original, shorter version.
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.
Idle_Mind: The shorter version seems smoother and easy to follow.