Passing parameter among three forms C# - c#

I have application which has three form: 1, 2, 3
- Form1: Always visible and has a button btnLogin. When I click on btnLogin, Form2 is opened.
- Form2: has 2 buttons: btnSearch and btnAddNew. When I click on btnAddNeww, Form3 is opened.
- Form3: has two text box and a button: txtA.text, txtB.text and btnOK. If I click on btnOK, two string in two text boxs will pass to Form1.
How can I do that, please help me!

Make your textboxes access modifiers in Form1 public (you can see this on properties of textbox):
Then you can call them directly using:
Form1 frm1 = Application.OpenForms.OfType<Form1>().Take(1).SingleOrDefault();
frm1.txtBox1.Text = txtA.Text + txtB.Text;

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

Pass a value to and from two different forms 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

Passing values from another form Winforms [duplicate]

This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 6 years ago.
All the tutorial in passing forms in the web are all from form1 then open form2 and then pass the value.
I want something reverse. form1 and form 2 is open so if you click ok in form2 the value you get in form 2 will be passed in form 1.
ex. form1 click buttton openform2button (form 2 shows) write 7 (number 7) on a textBox (in form2) click button okbutton then form 2 closes after form2 closes the textbox in form1 will store the data in form2. so the 7 you will put in textbox form2 will be saved and transferred in textbox form1. is that possible?
There's a number of ways you could do it:
Share a model object between form1 and form2.
Have form2 expose an event that form1 subscribes to. Pass the values in the event args.
Have form2 expose the value as a public property that form1 reads after form2 closes.
You can just have a public property on Form2 that Form1 will access when it needs to see the value. Something like this:
using(Form2 form2 = new Form2())
{
if(form2.ShowDialog() == DialogResult.OK)
{
form1Logger(form2.NumberWritten);
}
}
The property can be something as simple as this:
class Form2
{
public String NumberWritten
{
get{return textBox.Value;}
}
}

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

Categories

Resources