Send Values from One Form to Another Form

Passing Values from one Form to another Form in a Button click

Actually I have an object to pass. So I did this

in form1-->

private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}

in form2-->

public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();

}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;


a.set(g);
this.Close();
}
}

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

pass a value from one form to another

You create a new form, so old values will be lost. Default values are null.

Form1 F1 = new Form1(); //I'm a new Form, I don't know anything about an old form, even if we are the same type

You can use static vars, which would be the easiest solution to archive your goal, but there are other ways like constructors, containers, events etc.

public static string En1
{
get { return En; }
set { En = value; }
}

public static string Ed1
{
get { return Ed; }
set { Ed = value; }
}

And in the other form

private void button1_Click(object sender, EventArgs e)
{
Form1 F1 = new Form1();
Form1.Ed1 = textBox1.Text;
Form1.En1 = textBox2.Text;
}

Please be advised that a static variable exists only once for a class. So if you have multiple instances and you change the static variable in one, the change also affects all other instances.

Passing Textbox Values one form to another form in Windows Forms

One way is to just have a method on your second form that returns any needed details

using (DrawForm drawForm = new DrawForm ())
{
drawForm.ShowDialog(); //will wait for the form to close before continuing
var userResults = drawForm.GetResults(); //returns whatever you need the form to return
return userResults;
}

The above logic only works if the second form is a popup form of sorts. If you want both forms to be persistent, then it's a bit different. You might want to ensure both forms have access to each other. When you create the drawform be sure to save a reference to it. and in the constructor for your drawform add an argument to include the main form.

  DrawForm drawForm = new DrawForm(this);//now the forms can talk to each other

in your drawing form when the user hits 'go'

 mainForm.TriggerFinishedDrawing(drawingObject);

There are several ways to achieve this sort of behaviour, you just need to decide what sort of flow makes the most sense in your use case

Sending values from one form to another without returning to form 1

  1. Create Form1 with a label (label1) and a button (button1)
  2. Create Form2 with a textbox (textBox1)

Form1.cs

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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this.SetTextFromForm2, label1.Text);
form2.ShowDialog();
}

private void SetTextFromForm2(string str)
{
label1.Text = str;
}
}
}

Explain :

Form2 form2 = new Form2(this.SetTextFromForm2, label1.Text);

Send 2 informations to Form2 : method to change Text of label on Form1, and current text of that label

Form2.cs

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;

public delegate void SetTextToForm1(string str);

namespace WindowsFormsApp1
{

public partial class Form2 : Form
{
public SetTextToForm1 setTextToForm1 { get; set; }

public Form2(SetTextToForm1 SetTextMethod, string strTextFromForm1)
{
InitializeComponent();

this.setTextToForm1 = SetTextMethod;
this.textBox1.Text = strTextFromForm1;
}

private void TextBox1_TextChanged(object sender, EventArgs e)
{
setTextToForm1(textBox1.Text);
}
}
}

Explain :

public delegate void SetTextToForm1(string str);

"A delegate is a reference type variable that holds the reference to a method"

setTextToForm1(textBox1.Text);

Call Form1's method to change text of label on Form1


Answer your question :

This bit of code did basically exactly what I'm looking for, but do
you know if theres any way to essentially reverse it? I see when you
input text into the textBox in Form 2 it does put that into the label,
so it works perfectly. But is there a way to have the text box in Form
1 and when you hit the button just have Form 2 pop up with the label
containing the text from the text box in Form 1?

  1. Create Form1 with a textbox (textBox1) and a button (button1)
  2. Create Form2 with a label (label1)

Form1.cs

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;

public delegate void SetTextToForm2(string str);

namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
Form2 form2 = new Form2();

public SetTextToForm2 setTextToForm2 { get; set; }

public Form1()
{
InitializeComponent();

setTextToForm2 = form2.SetTextFromForm1;
}

private void TextBox1_TextChanged(object sender, EventArgs e)
{
setTextToForm2(textBox1.Text);
}

private void Button1_Click(object sender, EventArgs e)
{
form2.Visible = !form2.Visible;
}
}
}

Form2.cs

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 WindowsFormsApp1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

public void SetTextFromForm1(string str)
{
label1.Text = str;
}
}
}

How to pass string value from one form to another form's load event in C #

Just create a property on the Form2 class and set it before you show Form2.

public class Form2
{
...
public string MyProperty { get; set; }

private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(this.MyProperty);
}
}

From Form1:

public void button1_Click(object sender, EventArgs e)
{
string departmentName = "IT";
Form2 frm2 = new Form2();
frm2.MyProperty = departmentName;
frm2.Show();
this.Hide();
}


Related Topics



Leave a reply



Submit