Pass a value to and from two different forms C# - c#

I have 2 windows forms (Form1 and Form2). (C#)
In Form1 I have a Text and a LinkLabel. When I click the LinkLabel in Form1, Form2 shows.
In Form2, there is a TextBox and 2 buttons (Ok and Cancel).
What I am trying to do is have the string from the Text in Form1 show up in the TextBox in Form2. The user will then be able to edit the Text in the TextBox, and can Press the Ok button to confirm the changes or press the Cancel button to discard the changes. If the Ok button is pressed, Form2 will close and the Text in Form1 will be changed to whatever was entered into the TextBox from Form2.

Pass the string into Form2's cstor. Form1 has a reference to the Form2 object because it constructs Form2. Add a property to Form2. Set the property and read the property after Form2 closes in Form1. Conditionally use the value to set the textbox value in Form1 based on the DialogResult.

Might be useful from MSDN
http://msdn.microsoft.com/en-us/library/aa288422(v=vs.71).aspx
And for form to show:
Form2 frm = new Form2();
frm.Show();

Similar question to others... Here's some solutions I've offered in the past...
Option 1
Option 2

Related

How to update data in form2 without create new instance of form2

it's just a simple stuff that i wanna test
i have two forms (Form1 and Form2)
In Form1 have:
textbox and button
In Form2 have only TextBox that use for displayData when I click button in Form1
the problems is when i clicked buttton the data send to Form2 but it's created a new form everytime I clicked it. Is there any way to updateData in Form2 without create a new one.
Here the code:
In Form1:
private void sendBt1_Click(object sender, EventArgs e) //sendBt1 is button in Form1
{
Form2 f2 = new Form2(typeBox1.Text);//typeBox1 is TextBox in Form1 use to type
f2.Show();
}
In Form2:
public Form2(string data)
{
InitializeComponent();`enter code here`
this.ds2.Text = data; //ds2 is textBox in Form2 that i wanna display
}
Can anyone help? Im kinda stuck rn :)
As commented already, there are numerous ways to get this done. The current code, as you describe, is creating multiple Form2 objects since the code is creating a new Form2 each time the button is clicked on Form1.
Form2 f2 = new Form2(typeBox1.Text);
In addition, you want the “TEXT” in typeBox1 TextBox to be passed to Form2's TextBox ds2. The current code does this however, if the text in typeBox1 TextBox in Form1 changes, the TextBox in Form2 (ds2) is NOT updated. Again, there are numerous ways to fix this, however, I suggest to create a DataBinding for Form2's TextBox such that IF the typeBox1 TextBox changes in Form1, then the TextBox ds2 in Form2 will automatically be updated at the same time and the user does not have to do anything.
The example below uses this DataBinding approach and works as described….
I may be missing something here, however, in my tests, one approach is to simply “check” to see if Form2 is already open. If Form2 IS OPEN… then, we do not have to do anything since our code sets up the data binding for the text box in Form2’s constructor. Since Form2 IS OPEN it may be minimized, therefore, if the user clicks the button on Form1 and Form2 IS OPEN then we will set the forms state back to normal which will basically restore the form. Also the code will call the forms Show() method in case the Form has somehow been hidden.
IF Form2 IS NOT OPEN, then obviously we need to create it as your current code does, however, instead of passing in JUST the TEXT from the TextBox, I suggest you send over the WHOLE TextBox from From1 to Form2. This way, we can set up the DataBinding for Form2’s TextBox and Bind Form2’s TextBox to the passed in TextBox from Form1. This will automatically update Form2’s TextBox ds2 when Form1’s TextBox changes.
Therefore I suggest you change the code in Form1’s sendBt1_Click event to something like…
private void sendBt1_Click_Click(object sender, EventArgs e) {
var form = Application.OpenForms["Form2"];
if (form == null) {
form = new Form2(typeBox1);
}
form.Show(); // <- in case the form is somehow hidden
form.WindowState = FormWindowState.Normal; // <- in case the form is minimized
}
NOTE: in the above code we are passing the WHOLE TextBox to From2, not just the text in the TextBox.
Then Form2’s constructor may look something like….
public Form2(TextBox f1TextBox) {
InitializeComponent();
ds2.DataBindings.Add("Text", f1TextBox, "Text");
}
This should be all you need. When the text is changed in Form1’s text box, the text box in Form2 is automatically updated.

How can I hide Form2 using a button from Form1?

I have two windows forms, Form1 and Form2. In Form1 I have two buttons: "show form 2" and "hide form 2". When I press "show form 2", it shows the second form. But I don't know how to hide the second form when I press "hide form 2". Any ideas?
If Form2 is open you can access it by:
Form form2 = Application.OpenForms["Form2"];
And then inside button click event, call:
form2.Hide();

Give focus to form2

I have a program with two forms, but Form1 is opening up over Form2, which isn't good since you need to use Form2 first. How can you force focus to Form2 when the application runs?
Use ShowDialog for Form2 instead of Show.
You can also use Application.OpenForms property to get the already opened form Form2 and then call its Focus method like:
if(Application.OpenForms["Form2"] != null)
Application.OpenForms["Form2"].Focus();

c# gui - navigate between forms

I'm programming c# GUI and I have 2 forms.
Form1 is my main form, and it has a button to open form2.
When the button in form1 is being clicked, I hide form1, create a new object of form2 and show form2.
I have a back button in form2. I want the behavior of this button to close form2, and show again the hidden form1.
How can I do it?
Have your form1 subscribe to the VisibleChange event of form2 and act accordingly. It will have to "remember" whether form2 is visible or hidden (or query it directly).
The alternatives are:
Your form2 will need a reference to form1.
This can be done in a number of ways - passing it in a constructor parameter, adding a property and assigning form1 to it.
Either of these ways will tightly couple these forms to each other (bad thing).
Have you tried
Form1.Visible = true;
Form1.Activate();
And then in the Form1 VisibleChanged Event you simply write
Form2.Close();

Is there an accepted practice for cross-form control manipulation?

Let's say I have two forms: Form1 and Form2. Form1 has a textbox, Textbox1 and Button1.
When Button1 is clicked, a new instance of Form2 is created and shown. If Form2 needs to access Textbox1, how should expose it? Should Form2 have a public variable to hold a reference to the textbox? Or, when Form2 closes, should it invoke some public method on Form1 that updates the textbox? Thanks for your advice.
You should probably add a public property to the first form that exposes the textbox's text.
However, much more importantly, you should name your controls and forms.
There is (almost) nothing worse than a form with controls textBox1, button1, comboBox13, checkBox37.
If Form2 needs the text box value from Form1 when it loads, I would add the value to Form2's constructor method and pass it in that way.
If Form1 needs to obtain a new value entered in Form2 you can create a delegate with an Event that passes the value back to an assigned event handler in Form1.
Creating a public property may be the fastest solution, but I would try to stay away from circular references between forms if that is the case.
Hope this helps!

Categories

Resources