Set value to another form - c#

I have to form Form1 and Form2
Form1 source
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
Form2 source
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}
I try to set text of label in Form1 and text of button on Form2
I use same method to set value but only work when set value from Form1 to Form2. Button1.Text on Form2 changed to teste test but nothing happend on Form1

You need to pass a reference to your Form1 instance to the Form2 instance.
You can do that like this:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this); // <---- Pass a reference to this form to Form2
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
And you would need to change the Form2 implementation a little aswell:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private Form1 other;
//Empty constructor for the designer
public Form2()
{
InitializeComponent();
}
public Form2(Form1 other)
{
this.other = other;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
other.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}

Use Constructor method , here is example
In form2
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
In form1 click event
private void label1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}
For more information , reference Passing Data Between Forms !

In Form2 you have to add a parameter for the Form1.
i.e.
// Form1
...
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.SetBtn = "teste test";
frm.Show();
}
...
// Form2
public Form2(Form frm1)
{
InitializeComponent();
frm1.setLb = "test test";
}

public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
Form2 frm = new Form2(this);
frm.Show();
}
}
public partial class Form2 : Form
{
Form _frm;
public Form2(Form frm)
{
_frm = frm;
InitializeComponent();
}`enter code here`
private void button1_Click(object sender, EventArgs e)
{
Form1 formVariable = (Form1)_frm;
formVariable.textBox.Text = "Hello";
}
}

Related

Changing text in one form, from another form

Im trying to change the text in Form1 when pushing the button on Form2
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textCh = "Text has been changed";
}
}
Form 1:
public partial class Form1 : Form
{
public string textCh {
get
{
return this.textCh;
}
set
{
this.label1.Text = value;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
}
}
When I'm pushing button nothing happens, the text remain the same.
Another method of passing Form1 to Form2 via the Show() method and the .Owner property:
// In Form1
Form2 f2 = new Form2();
f2.ShowDialog(this); // <-- pass Form1 via "this"
Then, in Form2, you CAST .Owner to type Form1:
// In Form2
Form1 f1 = this.Owner as Form1;
if (f1!=null && !f1.IsDisposed)
{
f1.textCh = "Text has been changed";
}
There are several way to do this here 2 examples
This example use references whitout any use of events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2(this);
frm.Show();
}
}
here the Form2 is created passing the Form1 as parameter
public partial class Form2 : Form
{
Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var lbl = (Label)_parent.Controls.Find("label1", false).First();
lbl.Text = "new text";
_parent.Update();
}
}
Than the Form2 use that for set the value wanted.
This example use events
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var frm = new Form2();
frm.UpdateLabelEvent += Frm_UpdateLabelEvent;
frm.Show();
}
private void Frm_UpdateLabelEvent(string str)
{
label1.Text = str;
}
}
and here the code of Form2
public partial class Form2 : Form
{
public event Action<string> UpdateLabelEvent;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UpdateLabelEvent("new string value");
}
}
Nothing changes in your Form1 because you are creating a new Form1 first and change the text there. The new Form1 is never shown, so you see no changes.
Solution
private void button1_Click(object sender, EventArgs e)
{
//Form1 f1 = new Form1(); GET rid of this line
f1.textCh = "Text has been changed";
}
you need to make sure off course that f1 is known in form2, if you dont know how here is a simple way to do that
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.f1 = this;
f2.ShowDialog();
}
public Form2()
{
public Form1 f1 { get; set; }
...

form1 string is don't pass to form2

want to make form1 pubId value changed from form2. pubId is always get null didn't change. How can I solve this problem?
Form1 code:
public string pubId = string.Empty;
public void button1_Click(object sender, EventArgs e)
{
try
{
form2 _frm2 = new form2();
_frm2 .FormClosed += _frm2_FormClosed;
}
catch (Exception ex)
{
MessageBox.show(ex);
}
}
private void _frm2_FormClosed(object sender, FormClosedEventArgs e)
{
if (pubId == "8")
{
MessageBox.show("works");
}
}
Form2 code:
public void buttonsend_Click(object sender, EventArgs e)
{
idfrm2 = "8";
form1 _frm1 = new form1 ();
_frm1.pubId = _idfrm2;
this.Close();
}
In this line you create a new form with new empty pubId.
form1 _frm1 = new form1 ();
Just create a constructor for form2 to pass it.
For example:
private string _pubId;
public form2(string pubId)
{
_pubId = pubId;
}
Then you can use it:
form2 _frm2 = new form2(pubId);
You can maintain reference to Form2 inside a List of controls maintained by Form1 and access it as below :
Form1 Code :
public partial class Form1 : Form
{
Form2 localfrm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.FormClosed += Frm2_FormClosed;
localfrm2 = frm2;
frm2.Show();
}
private void Frm2_FormClosed(object sender, FormClosedEventArgs e)
{
MessageBox.Show(localfrm2.PubId);
}
}
Form 2 :
public partial class Form2 : Form
{
public string PubId { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
PubId = textBox1.Text;
}
}
Upvote if this solves your problem

How to pass information from one form to another without global variable or arguments when creating new form [duplicate]

I have to form Form1 and Form2
Form1 source
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
Form2 source
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}
I try to set text of label in Form1 and text of button on Form2
I use same method to set value but only work when set value from Form1 to Form2. Button1.Text on Form2 changed to teste test but nothing happend on Form1
You need to pass a reference to your Form1 instance to the Form2 instance.
You can do that like this:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this); // <---- Pass a reference to this form to Form2
frm.SetBtn = "teste test";
frm.Show();
}
public string setLb
{
set
{
label1.Text = value;
}
}
}
}
And you would need to change the Form2 implementation a little aswell:
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private Form1 other;
//Empty constructor for the designer
public Form2()
{
InitializeComponent();
}
public Form2(Form1 other)
{
this.other = other;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
other.setLb = "test test";
}
public string SetBtn
{
set
{
button1.Text = value;
}
}
}
}
Use Constructor method , here is example
In form2
public Form2(string strTextBox)
{
InitializeComponent();
label1.Text=strTextBox;
}
In form1 click event
private void label1_Click(object sender, System.EventArgs e)
{
Form2 frm=new Form2(textBox1.Text);
frm.Show();
}
For more information , reference Passing Data Between Forms !
In Form2 you have to add a parameter for the Form1.
i.e.
// Form1
...
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.SetBtn = "teste test";
frm.Show();
}
...
// Form2
public Form2(Form frm1)
{
InitializeComponent();
frm1.setLb = "test test";
}
public partial class Form1 : Form
{
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e){
Form2 frm = new Form2(this);
frm.Show();
}
}
public partial class Form2 : Form
{
Form _frm;
public Form2(Form frm)
{
_frm = frm;
InitializeComponent();
}`enter code here`
private void button1_Click(object sender, EventArgs e)
{
Form1 formVariable = (Form1)_frm;
formVariable.textBox.Text = "Hello";
}
}

Pass value from datagridview to textbox on other form

I have Form1 that has textbox; Form2(current form) has datagridview and button Choose. when i run Form1 that shows in new (nothing data that i wrote on form)
How can i pass value from Form2 to Form1 that keeps all data.
public void btnChoose_Click(object sender, EventArgs e)
{
Form1 form = new Form1;
form.txtMaKeHoach.Text = "value";
form.Show();
this.Close();
}
I hope this is what you are searching for...
Code for Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 frm = new Form2();
DialogResult res = frm.ShowDialog();
if (res != System.Windows.Forms.DialogResult.OK)
{
frm.Dispose();
return;
}
this.txtMaKeHoach.Text = frm.ChosenEntry;
frm.Dispose();
}
}
Code for Form2:
public partial class Form2 : Form
{
private string _ChosenEntry = "";
public Form2()
{
InitializeComponent();
}
private void btnChoose_Click(object sender, EventArgs e)
{
//...
_ChosenEntry = this.dataGridView1.SelectedCells[0].Value.ToString();
this.DialogResult = System.Windows.Forms.DialogResult.OK;
this.Close();
}
public string ChosenEntry
{
get { return _ChosenEntry; }
}
}

Passing a value from one form to another form

I have two forms named form1 and form2:
form1 is made of a label and a button.
form2 is made of a textBox and a button
When I click the form1 button, this will show up form2. Any inputs in textBox should be written back to form1.label once I hit the button in form2.
I have the code below but it doesn't work.
// 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();
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
this.Close();
}
}
And a screenshot:
How can I realize that?
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();
}
}
Like mentioned in other posts, you won't be able to reference the original Form1 by creating a new instance of Form1. You can pass Form1 into Form2's constructor or expose Form2's text as a public property, but I usually prefer using delegates for this to maintain loose coupling.
// Code from Form 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.PassValue += new PassValueHandler(objForm2_PassValue);
objForm2.Show();
}
public void objForm2_PassValue(string strValue)
{
label1.Text = strValue;
}
}
// Code From Form 2
public delegate void PassValueHandler(string strValue);
public partial class Form2 : Form
{
public event PassValueHandler PassValue;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (PassValue != null)
{
PassValue(textBox1.Text);
}
this.Close();
}
}
When you are doing:
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
... you are creating a new Form1 and calling the PassValue method on the wrong Form1 object. Instead, you could do:
public partial class Form1 : Form
{
// This is the text that will be entered in form2
public String form2text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Show form2
Form2 objForm2 = new Form2(this);
objForm2.ShowDialog();
// When form2 is closed, update the label text on form1
label1.Text = form2text;
}
}
public partial class Form2 : Form
{
// This is the instance of Form1 that called form2
private Form1 form1caller;
public Form2(Form1 form1caller)
{
InitializeComponent();
this.form1caller = form1caller;
}
private void button1_Click(object sender, EventArgs e)
{
// Pass the textBox value to form1 before closing form2
form1caller.form2text = textBox1.Text;
this.Close();
}
}
I just tried this code and it works, sure it will help you.
in the first form (Form1) type below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(textBox1.Text);
f.ShowDialog();
}
}
in the second form (Form2) use below codes:
public partial class Form2 : Form
{
public Form2( string st)
{
InitializeComponent();
textBox1.Text = st;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
You could do this:
class Form2
{
public string ReturnedText = "";
private void button1_Click(object sender, EventArgs e)
{
ReturnedText = textbox1.Text;
Close();
}
}
and in form1
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
string ret = objForm2.ReturnedText;
You should pass reference on form1 to form2 instead of creating new instance in this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1(); // ← this is another form1, not that you see
objForm1.PassValue(textBox1.Text);
this.Close();
}
The way that I normally approach this requirement is as follows:
I place a public property on the Form2 class:
public string ValueFromForm1 { get; set; }
//In the constructor, or other relevant method, I use the value
public Form2()
{
form2LabelToDisplayForm1Value.Text = ValueFromForm1;
}
In order to return something to Form1, you need to add a public property to the Form1 class to receive the value, and then send a reference to the form to Form2, so that Form2 can set the value:
//Add reference property to Form2 class
public Form1 CallingForm { get; set; }
//Form2 can access the value on Form1 as follows:
private someMethod()
{
this.CallingForm.ValueFromForm2 = "Info coming from form 2";
}
then
//Add public property to Form1 class
public string ValueFromForm2 { get; set; }
//When Form2 is created, set the reference property
Form2 objForm2 = new Form2();
objForm2.CallingForm = this;
objForm2.Show();
Since Form2 now has a reference to the Form1 that created, there is no need to call new Form1() anywhere in Form2. All Form2 has to do is set the value on the reference, and then close itself.
This is what you are going to do:
// Code from Form 1
public partial class Form1 : Form
{
public string MyValue { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.textBox1.Text = MyValue;
objForm2.MainForm = this;
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form1 MainForm { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainForm.MyValue = textBox1.Text;
MainForm.Show();
this.Close();
}
}
Form 1 code...:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form1 : Form
{
string str;
private String value1;//taking values from form no _of_test_cases
public string value
{
get { return value1; }
set { value1 = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
str = f2.passvalue;
}
}
}
Form 2 code....:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form2 : Form
{
private string str;
public string passvalue
{
get { return str; }
set { str = value; }
}
public Form2()
{
InitializeComponent();
}
private void Btn_Ok1_Click(object sender, EventArgs e)
{
passvalue = textBox1.Text;
this.Close();
}
}
}
directly execute it u will get the clear picture....same way u can pass values from one form to other...
post your comments if you face any issues...
hope this will help...
or else you can refer this video...
http://www.youtube.com/watch?v=PI3ad-TebP0

Categories