This question already has answers here:
Communicate between two windows forms in C#
(12 answers)
Closed 5 years ago.
I have the following:
I have a GUI with two forms. Form2 is opened via Form1 by a button. Both forms have Textboxes and I want them to communicate with each other (one form can grab the entries of textboxes from another form). What I did now in Form2 is:
private Form1 m_form = null;
public Form2(Form1 f)
{
InitializeComponent();
m_form = f;
}
and for the Textboxes functions like:
public String getLocation()
{
return LocationBox.Text;
}
That works fine. So Form2 can read the entries from Form1. Now I wanted Form1 to read the textbox entries from Form2 and tried the same thing (which is probably wrong):
private Form2 m_form2 = null;
public Form1(Form2 f2)
{
InitializeComponent();
m_form2 = f2;
}
and then some functions like the one I've posted but everytime I want to read a textbox with Form1 which is in Form2 I get "null" and "NullReference" Exception. Where is the error?
EDIT: Ok I solved a part. Adding
Form2 m_form2 = new Form2(this);
m_form2.Show();
solves the problem with the NullReferenceException. Without the line
m_form2.Show()
it passes empty strings but now everytime I hit a button the form2 appears.
I think the problem is that you never call the consturctor public Form1(Form2 f2) . When you open Form2 from the first one you have to save that instance within your instance of Form1:
void createForm2() {
Form2 frm2 = new Form2(this);
this.m_form2 = frm2;
}
try this in Form1:
this.TextBoxName.Text = m_Form2.getLocation();
or in Form2:
m_form.TextBoxName.Text = this.getLocation();
Note
in Form1 when call form 2:
m_Form2 = new Form2(this);
//do anything with your code
For sending values between two forms, you may
1-> Send the values in the constructor of the second form. You may create a paramterized constructor and send the values when you initialize the form as :
Form1 obj = new Form1(Object);
2-> You may take a reference in to your first form in the second form.
In second form,
public Form1 objForm1;
and in First Form,
Form2 objForm2=new Form2();
Form2.objForm1=this;
and then you can use Form2's objForm1 to refer to Form1's textbox or any control.
Consider you want to send all values from Form1 to Form2
In your second form you must have a variable of type Form1 that refers to the prev form. So in second form,
public Form1 objForm1;
and then you need to send the current instance of the Form1 to Form2 as
Form2 objForm2=new Form2();
Form2.objForm1=this;
i.e. the objForm1 that you created in Form2 refers to this instance of Form1.
Now in Form2 you may use any of Form1's control or variable as,
Form1.TextBox1 or Form1.Variable
Related
I've hidden a form Form1 and have shown a new form Form2 using the following code:
form_Form2 f = new form_Form2();
this.Hide();
f.Show();
where this == Form1. How do I then make Form1 visible again from Form2? I do not want to create a new instance of Form1, I just want to make it show up again because the this.Hide() function keeps the data on the form instead of fully closing it.
Form2 will need a reference to Form1
form_Form2 f = new form_Form2();
f.OtherForm = this;
this.Hide();
f.Show();
You will have to add OtherForm property. Then later in Form 2
OtherForm.Show();
This question already has answers here:
How to update textbox in form1 from form2?
(3 answers)
Closed 4 years ago.
So this is a bit different to what I've seen:
How to change text in a textbox on another form in Visual C#?
I have a form (Form1) that runs when my C# application runs. A button Form1 opens Form2. On Form2, I have another button to set the text of the textbox on Form1, to the same value as a textbox on Form2.
Using a similar approach to:
Form1 frm1 = new Form1();
frm1.TextBoxValue = "SomeValue";
Doesn't work as it opens a new form completely, but I want to change the form1 that is already open, can someone please assist?
You must store Textboxvalue on Form2 in some property like this:
public string ReturnValue {get;set;}
private void Form2_button2_Click(object sender, EventArgs e)
{
ReturnValue = txtInput.Text;
}
Or You can change the access modifier for the generated field in Form2.Designer.cs from private to public.
Change this
private System.Windows.Forms.TextBox txtInput;
by this
public System.Windows.Forms.TextBox txtInput;
Then in Form1 you can get Value of ReturnValue When end user close Form2
private void Form1_popupButton_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.ShowDialog();
// get ReturnValue from form2
string ReturnValue = frm.ReturnValue ;
//get txtInput value directly
ReturnValue = frm.txtInput.Text;
}
I would like to help you as good as I can but the information you provided is not that clear to me.
What you could do is when creating a new form use the constructor to pass the text
E.G.
Form1 frm1 = new Form1("some value or variable");
When you need tailored information please provide more code and a better explanation of your problem.
You could create a constructor for Form2 that takes in Form1 as a parent:
public partial class Form2 : Form
{
Form1 Parent { get; }
public Form2(Form1 parent)
{
Parent = parent;
Parent.TextBoxValue = "SomeValue";
}
}
However I don't think this is good practice. If your Form2 needs to be passing a result back to Form1, then you need to reverse your approach. Instead you should have a public property or method on Form2 that you can assign from inside Form1.
i understand that this below code make a overflow because its called second times.
this is the explanation,
let say i have form1, form2, form3
And in the form1
Form NewForm2 = new Form2;
And in the form3
Form NewForm3 = new Form2;
When i showing form2 of course its stack overflow because its called second times on Form1 and Form3,
Okay, so my question is there a way to connect the Form2 from multiple form called it? Any reference?
so my question is there a way to connect the Form2 from multiple form called it?
yes just apply a Singleton Pattern on your form
public partial class Form2 : Form
{
private static Form2 inst;
public static Form2 GetForm
{
get
{
if (inst == null || inst.IsDisposed)
inst = new Form2();
return inst;
}
}
}
to show your form
Form2.GetForm.Show();
You may use this code to see if Form2 is already created and if so then show it else create new instance of Form2.
var form = Application.OpenForms.OfType<Form2>().FirstOrDefault();
if (form == null)
{
form = new Form2();
}
form.Show();
but opening 2 forms should not cause SO exception, I assume there is a problem elsewhere but you may try code above and see if it fixed your problems.
I am creating a small program that speaks and can do minor tasks for me. I want to create a second form (Form2) in which I will type in my name and other personal info.
How can I get Form1 to read the textfields in Form2?
How can I get Form1 to read the textfields in Form2?
Presumably Form1 creates an instance of Form2... so hold on to that instance, and expose appropriate properties on the form:
Form2 form2 = new Form2();
form2.Show(); // Or ShowDialog?
string name = form2.UserName;
The implementation of UserName may well just fetch the value from the text field:
public string UserName { get { return userNameTextField.Text; } }
You could expose the text field directly with a property, but personally I tend to think that a form should "own" its UI, and not let other code mess with it.
Create global variables with modifier public and then assign the text of the textboxes to the variables either using Text_Changed event or button_click event (if you want to retrieve text after button click.
Then in your Form1. You use
Form2 form = new Form2();
//Then do whatever u want with the variable
MessageBox.Show(form.globalVariableName);
or
Set the modifier property of the textbox to public in your form2 and in your Form1
Form2 form = new Form2();
//Then do whatever u want with the textbox
MessageBox.Show(form.TextBoxName.Text);
This should get you started:
//Global Variable
Form2 frm2;
//assuming form1 is the creator of form2
public Form1()
{
frm2 = new Form2();
}
//in your Form1, under read data button for example:
string myName = frm2.TextBox1.Text;
You can create Methods and form1 to retreive the data from textfields
public string GetTextFieldText()
{
return textfield.Text;
}
Or wrap it with an property
public string TextField
{
get
{
return textfield.Text;
}
}
Then access it from form2 something like this:
Form1 frm1 = new Form1();
string text = frm1.TextField; // Or GetTextFieldText()
I have two forms Form1 and Form2
I am opening Form2 from Form1 on button_Click
Form2 obj2 = new Form2();
this.Visible = false;
obj2.Show();
then I want to get back Form1 Visible (on disposing Form2) in same states of Controls on which I left.....
Your Form2 doesn't know anything about Form1. It will need a reference to it (you can do that by adding a Form type property on Form2 and assign Form1 to it after construction):
//In Form2
public Form RefToForm1 { get; set;}
//In Form1
Form2 obj2 = new Form2();
obj2.RefToForm1 = this;
this.Visible = false;
obj2.Show();
//In Form2, where you need to show Form1:
this.RefToForm1.Show();
Oded's answer will work perfectly well, another option with the same result will be to expose public event in Form2 called for example "AfterClose", invoke it when Form2 is disposing and have Form1 add event handler where it show itself. Let me know if you're interested and I'll give some sample code.