Passing values from another form Winforms [duplicate] - c#

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

Related

Force Winform control update [duplicate]

This question already has answers here:
Send values from one form to another form
(20 answers)
Closed 7 years ago.
I has 2 form which is Form1 and Form2. In Form1, I has a button which will open Form2 in button click. In Form2, it use to do some setting for the label text in Form1. When close the form2, the label text in Form1 will update base on the setting, but I can make the label text update. Below is the code for update the text for label in Form1. I'm hardcode the text to simulate the situation.
Form1
public void languageChange()
{
labelControl5.Text = "AAAAAA";
labelControl5.Invalidate();
labelControl5.Update();
labelControl5.Refresh();
Application.DoEvents();
}
In form2, I has below code to fire languageChange function in Form1.
private void innoLanguage_FormClosed(object sender, FormClosedEventArgs e)
{
Main_new main = new Main_new();
main.languageChange();
}
It has call the function in the Form1 when form2 is close but it not update the label text.
I feel I make a mistake but I can't figure out. How I can make this work, please help.
First thing's first you don't need to invalidate a Label when you change its Text variable, it will automatically redraw the control.
Secondly when the Form2 closes you created a new Main_new, called languageChange() on it, but then didn't actually show or display the form. I'm not sure what the situation is but if there's already a Main_new form open you don't need to make a new one, simply get the parent of the Form2 (which will be the Main_new already open), cast it as a Main_new, then call languageChange() on that.
Example
private void innoLanguage_FormClosed(object sender, FormClosedEventArgs e)
{
((Main_new)this.Parent).languageChange();
}
If Main_new isn't the name of the Form that opens Form2 change it to whatever does.

Refresh an open form from another open form

I want to refresh an already opened form (form1) from another opened form's (form2) button_click(). In form1 I display the data saved by form2 and when form1 is already opened I want it to refresh if new data is saved on form2 in order to display it.
The problem is that I've tried iterating through `Application.Openforms`, but it turns out that it is read-only and I cannot access the form once found and I don't know how to access *form1* from *form2*, since I can't simply find it.
How can I access *form1* from *form2*?
Edit:
Form1 is actually opened from Form2.
The problem with Application.Openforms is that , as I've stated, a read-only list of forms already opened and I cant actually access the forms through it. They simply don't have the methods for it, I sugest you try using Application.OpenForms and look it up if you don't know how it works.
Also it's pointless to show what I've already tried because it includes Application.OpenForms, but for the sake of information:
FormCollection of = Application.OpenForms;
foreach (var f in of)
{
if (f.GetType().ToString() == "Kontrl_Doc.Visualizar")
{
f.Refresh();
}
}
When I click the button (button_click()) in Form2 it checks if Form1 is open or not. If Form1 isn't open it opens one and if it is than I'd like it to refresh it. Simultaneously, it closes Form2 and opens Form2 again, in order to reset is fields.
What I wan to do is, if the form1 is already opened , it form2 should tell it to refresh the already opened window with the form 1.
"Form1 is actually opened from Form2" - If this is the case, then just call Refresh using the form variable that you have in Form2. If necessary, make that a private field in the Form2 class or store it in an array for later use.
For example:
(Somewhere in Form2)
Form1 form1 = new Form1();
form1.Show();
(Inside the button click in Form2)
form1.Refresh();
you can use events. In form2 you place this code
public event Action ReloadForm1;
//on the place where you will reload form1
ReloadForm1();
and in form1 if you have opening form2:
form2.ReloadForm1 += Reload;
//outside method
void Reload()
{
this.Reload();
}
Create a void method in form1 and add the components u want to refresh maybe you want to reload a dropdown from db
public void Refresh()
{
...
}
then open a dialog of form2
catch the dialog result

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

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

find form instance from other class

I have Main form with list of data inside listBox. On button click I'm opening new form to create new data object (Main form is inactive in background), when new data is submitted listobox inside main form should be populated with that new object.
I was thinking following:
When Form2 is submitted I was thinking to find MainForm instance and kill that instance and after that it should be easy, load again list of data from the db and display in the listbox.
Question is:
If Form1 is created and on some event Form2 is instantiated with showDialog so Form1 is inactive until data is submitted how to find Form1 instance before Form2 is closed?
So again, how to find instance of Form1 class from Form2 class?
Thanks
You can get a reference to any of the application's currently open forms by using the Application.OpenForms property. Forms in this FormCollection can be accessed by index like so:
var form1 = Application.OpenForms[0];
or by the form's Name property like so:
Form form1 = Application.OpenForms["Form1"];
Hope this helps.
After getting the instance of an open form, I needed to call a method from that form, so this worked for me:
if (System.Windows.Forms.Application.OpenForms["Form1"] != null)
{
Form1 form1 = Application.OpenForms["Form1"] as Form1;
form1.yourMethodCall();
}
if you call
Form1.ShowDialog(this)
then you'll be able to get a reference to the calling form with
this.Owner.Name
in the second form (Form2 in your case)
see http://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog.aspx

Categories

Resources