How to Pass an Object from Form1 to Form2 and Back to Form1

How do you pass an object from form1 to form2 and back to form1?

What you need to do is create a second constructor to your second form that accepts an object as a parameter... for all I care, it could be the entire Form1 object instance, then you can get whatever you want from it. Preserve this object in your second form and modify it as needed there. Upon completion of your second form, your first form will have that data and you can do whatever "refreshing" once the second form closes.

public partial class YourSecondForm : Form
{
object PreserveFromFirstForm;

public YourSecondForm()
{
... its default Constructor...
}

public YourSecondForm( object ParmFromFirstForm ) : this()
{
this.PreserveFromFirstForm = ParmFromFirstForm;
}

private void YourSecondFormMethodToManipulate()
{
// you would obviously have to type-cast the object as needed
// but could manipulate whatever you needed for the duration of the second form.
this.PreserveFromFirstForm.Whatever = "something";
}

}

Passing data between form1 and form2 in C#

This might work.
There are comments in the code.

The workflow is creating a new instance of the class Form2 and setting two public variables. Public means that they can be accessed from outside of the class (see here, if you want). Then the method Show() is called and the Form appears. In the Form2 code, the public variables now have the values previously specified and can be used.

Form1:

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

namespace FinalProject
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// this is to make sure only one box is checked for both selections. Starts here
private void label1_Click(object sender, EventArgs e)
{

}

private void button2_Click(object sender, EventArgs e)
{

}

private void MulCB_CheckedChanged(object sender, EventArgs e)
{

if ( MulCB.Checked == true)
{
operation = 1;
DivCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}

private void DivCB_CheckedChanged(object sender, EventArgs e)
{
if (DivCB.Checked == true)
{
operation = 2;
MulCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}

private void AddCB_CheckedChanged(object sender, EventArgs e)
{
if (AddCB.Checked == true)
{
operation = 3;
DivCB.Checked = false;
SubCB.Checked = false;
MulCB.Checked = false;
}
}

private void SubCB_CheckedChanged(object sender, EventArgs e)
{
if (SubCB.Checked == true)
{
operation = 4;
DivCB.Checked = false;
AddCB.Checked = false;
MulCB.Checked = false;
}
}

private void oneDCB_CheckedChanged(object sender, EventArgs e)
{
if(oneDCB.Checked == true)
{
digits = 1;
twoDCB.Checked = false;
threeDCB.Checked = false;
}
}

private void twoDCB_CheckedChanged(object sender, EventArgs e)
{
if ( twoDCB.Checked == true)
{
digits = 2;
oneDCB.Checked = false;
threeDCB.Checked = false;
}
}

private void threeDCB_CheckedChanged(object sender, EventArgs e)
{
if (threeDCB.Checked == true)
{
digits = 3;
oneDCB.Checked = false;
twoDCB.Checked = false;
}
}
private void button8_Click(object sender, EventArgs e)
{
// operations: 1. (*) 2. (/) 3. (+) 4. (-)
// digits are as number indicates.

// Second window popup.
// it's the question form, right?
Form2 questionForm = new Form2();
//"Write" your settings in the other form's variables
//You will have to write code that finds out which checkbox is which number! For now its fixed.
questionForm.operation = 2;
questionForm.digits = 1;
questionForm.Show();
//Hide Form1
this.Hide();
}
}
}

Form2:

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

namespace FinalProject
{

public partial class Form2 : Form
{
public static int operation;
public static int digits;

public Form2()
{
InitializeComponent();

}

//do NOT paste this. It can be added by creating an event handler
// you also might not need this, but this method is called when this Form appears. It's an example.
// https://msdn.microsoft.com/en-us/library/zwwsdtbk(v=vs.80).aspx
private void Form2_Load(object sender, EventArgs e)
{
//here you can use your variables for example (also anywhere within this class!)
//e.g.
Textbox1.Text = (string)operation;
}

private void FinishedBtn_Click(object sender, EventArgs e)
{

}
}
}

Passing values from form1 to form2 via button click to form2

There are many ways to do this. Try it this way.

private void btnPlaceOrder_Click(object sender, EventArgs e) {
string fname = textBox1.Text;
frmCakeOrder frm = new frmCakeOrder(textBox1.Text);
frm.Show();
}

And in frmCakeOrder,

public frmCakeOrder(string fname) {
InitializeComponent();
textBox1.Text = fname;
}

Passing a value from one form to another form

You don't access your form1, from which you created form2. In form2 button1_Click you create new instance of Form1, which is not the same as initial. You may pass your form1 instance to form2 constructor like that:

   // Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2(this);
objForm2.Show();
}

}

// Code From Form 2

public partial class Form2 : Form
{
Form1 ownerForm = null;

public Form2(Form1 ownerForm)
{
InitializeComponent();
this.ownerForm = ownerForm;
}

private void button1_Click(object sender, EventArgs e)
{
this.ownerForm.PassValue(textBox1.Text);
this.Close();
}
}

Passing object to different windows forms

There are a few different ways to do this, you could use a static class object, the above example would be ideal for this activity.

public static class MyStaticClass
{
public static string MyStringMessage {get;set;}
}

You don't need to instance the class, just call it

MyStaticClass.MyStringMessage = "Hello World";
Console.WriteLine (MyStaticClass.MyStringMessage);

If you want an instance of the object you can pass the class object that you create on Form1 into Form2 with the following.

private void button1_Click(object sender, EventArgs e)
{
this.Hide();
Form2 form2 = new Form2();
form2.MyClass = class1;
form2.Show();
}

Then create a property on Form2 to accept the class object.

public Class1 MyClass {get;set;}

remember to make the Class1 object a global variable rather than create it within button 2 itself.

Pass data from form2 method to form1 textbox

U can simply ref Form1 in the Constructor in Form2

public Form2(Form1 frmMain){
InitializeComponents();
_FrmMain = frmMain;
}

simply when initializing Form2:

Form2 frm2 = new Form2(this);
frm2.Show();

and now in Form2 you should be able to just change everything by properties:

private void BtnSend_Clicked(object sender, EventArgs e)
{
_FrmMain.Foo = Bar;
}


Related Topics



Leave a reply



Submit