Passing data to the first form from the second form in C #

I have two forms. Say FormA , FormB .

From FormA, I called FormB using frmB.Show ();

In FormB, I have two text fields and combobox controls. Suppose a user enters some data into these two text fields and selects an item from the combo box in Form2 and clicks OK .

After clicking, I want these text fields to enter the entered values, combobox selected the element value back in FormA .

How can I achieve this with C #.

+6
source share
9 answers

Or you can transfer an object from FormA to FormB and associate its properties with the controls in FormB. If you want FormA to be notified when you click OK, you can declare an event in your data container class, subscribe to it in FormA and run it from FormB.

Be a DataContainer in the class you define

public class DataContainer { public event EventHandler AcceptedChanges; protected virtual void OnAcceptedChanges() { if ((this.AcceptedChanges != null)) { this.AcceptedChanges(this, EventArgs.Empty); } } public void AcceptChanges() { this.OnAcceptedChanges(); } public string Text1 { get; set; } public string Text2 { get; set; } } 

in FormA:

  private void button4_Click(object sender, EventArgs e) { DataContainer data = new DataContainer(); data.Text1 = "text1"; data.Text1 = "text2"; Form2 frm = new Form2(); frm.Data = new DataContainer(); data.AcceptedChanges += new EventHandler(data_AcceptedChanges); frm.Show(); } void data_AcceptedChanges(object sender, EventArgs e) { // your code here } 

and in FormB:

 public DataContainer Data { get; set; } private void Form2_Load(object sender, EventArgs e) { textBox1.DataBindings.Add(new Binding("Text", Data, "Text1")); textBox2.DataBindings.Add(new Binding("Text", Data, "Text2")); } private void button1_Click(object sender, EventArgs e) { Data.AcceptChanges(); } 

You must also implement INotifyPropertyChanging and INotifyPropertyChanged in the DataContainer class to play nicely with bindings.

+3
source share

I went to an easy way .. some jewelry ..

  • create public variables in your ie form class

    public string passVariable1 = "";

  • If you have text fields, go to properties, then click on the zipper and double-click the empty TextChanged event handler. This will create a piece of code in the code that will be executed when the contents of the text field change .. in this block of code, assign the contents of the text field to the corresponding public variable.

  • i.e. my public variable was

    public string issue = "";

    private void txtIssue_TextChanged (object sender, EventArgs e) {issue = txtIssue.Text; }

  • Add a button and create a click event for this button (just double-click the button in the design panel). In the click event code block, set the dilog result to ok and hide

    this.DialogResult = DialogResult.OK;

    this.Hide ();

  • In the window of the parent window, check the result of the dialog and take the form data from the child form public variables

 if (f.ShowDialog() == DialogResult.OK) { string b = f.issue; string e = f.year; string f = f.month; }; 
+6
source share

In the script you are describing, I would call frmB.ShowDialog() , not frmB.Show() .

 // Inside FormA open frmB as a modal dialog waiting for // OK or Cancel result using the following statement if (frmB.ShowDialog() == DialogResult.OK) { // Retrieve selected values from frmB here (while frmB is still not disposed) } 

The advantages of ShowDialog() are that you:

  • Get the return value from the form, easily allowing you to determine that click OK (not cancel) to close.
  • The form is not immediately deleted when closing, which allows you to get the values ​​you need.
  • By opening frmB as a modal dialog, you avoid checking the difficulties that may arise if your user starts interacting with form A and frmB is open.

NOTE. When designing frmB, you need to set the DialogResult property of the OK control button on DialogResult.OK so that the form returns the correct DialogResult dialog when this button is clicked (alternatively, you can also set this.DialogResult in the OK button. Click the event handler)

+4
source share

You can create an EventHandler in FormB to which FormA will be signed. Also, add a couple of common properties in FormB that represent the data you want to use FormA. Then, when FormB disconnects from the event, FormA will know to update its data.

Note. The key principle in this example is the implementation of EventHandler (you can create your own type of event handler), which notifies FormA when the data is ready for updating / viewing / etc. We hope that this example allows you to see how you can implement an event handler for your specific situation.

Example:

FormA -

 public partial class FormA : Form { //FormA has a private instance of FormB private FormB formB = null; public FormA() { InitializeComponent(); } void formB_OnDataAvailable(object sender, EventArgs e) { //Event handler for when FormB fires off the event this.label1.Text = string.Format("Text1: {0}\r\nText2: {1}", formB.Text1, formB.Text2); } private void InitializeFormB() { this.formB = new FormB(); //FormA subscribes to FormB event formB.OnDataAvailable += new EventHandler(formB_OnDataAvailable); } private void button1_Click(object sender, EventArgs e) { this.InitializeFormB(); formB.Show(); } } 

FormB -

 public partial class FormB : Form { //Event that fires when data is available public event EventHandler OnDataAvailable; //Properties that expose FormB data public string Text1 { get; private set; } public string Text2 { get; private set; } public FormB() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { //Set the exposed properties, then fire off the event. this.Text1 = this.textBox1.Text; this.Text2 = this.textBox2.Text; if (OnDataAvailable != null) OnDataAvailable(this, EventArgs.Empty); } } 
+4
source share
+2
source share

A dirty but also the quickest solution is to make these controls publicly available. This can be done by adding the word public to the file Form2.Decisgner.cs. If FormA has a member variable or a local variable FormB, you can access a control (e.g. TextBox1):

 frmB.TextBox1.Text 

which is now available outside of FormB.

+1
source share

I had the same problem and came up with an idea that is slightly different. In my scenario, I am making a flash card program for my youngest two children, and I would like to return the answer provided to the parent form (a new child form for each new question with a flash card so that the parent form can update the number on the left, how many are correct, how many incorrect, etc.) without adding values ​​to the database. It seems to be too complicated. I did to create a class with 3 each type of variable. I figured there would be three types for each task.

This is an example of my new class:

 namespace ClassNamespace { public class ValueHolder { public int intValue1 { get; set; } public int intValue2 { get; set; } public int intValue3 { get; set; } public long longValue1 { get; set; } . . . } } 

I create a new ValueHolder (ValueHolder vh;) from the parent form and pass it to the child form. In the child form, I create a new ValueHolder and then set it equal to the ValueHolder object sent in the constructor of the child form class. Now that the enter key is pressed (answer given), I can set vh.intValue1 to this.answerBox.text; ... well, I have to use int.tryparse (); But you got the idea. I only need the vh.intValue1 link from the parent form to get the entered value.

Parent form:

 for (int i = 0; i < limit; i++) { ValueHolder vh = new ValueHolder(); ChildClass cc = new ChildClass(vh); MessageBox.Show(vh.intValue1.ToString()); //to test that it works } 

and child form:

 ValueHolder vh; public ChildClass (ValueHolder vhIncoming) { vh = vhIncoming; } private void answerBox_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { vh.intValue1 = 1234; } } 

This seems to be the easiest solution for my scenario. I know this question is old, but I wanted to add this option for everyone who is in a similar situation. Just add a class to your project, add more types or more types to the class as needed, repeat the repeat for future projects.

+1
source share

If you can read it on the same page directly from your controls as Textbox1.Text, Textbox2.Text, Combobox.SelectedValue (I think) But if session variables are used on different pages, such as: Session ["date1"] = TextBox1 .Text; Session ["date2"] = TextBox2.Text; Session ["comboValue"] = Combobox.SelectedValue; and use them to fill out the form

0
source share

It will depend on how you usually develop your applications.

  • You can work using an event-driven system in which you have events and delegates. Mentioned @ Dave81
  • Or you can create properties that return the given / selected values ​​so that the parent can restore them from the object (I want to say "Dialogue" but not sure what you are using).
  • Or you can follow @zmilojko and just set them publicly, which is basically similar to creating properties, but more to the dark side of coding practice: D

All this will work, but it all depends on how you like to structure your applications.

0
source share

All Articles