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()
Related
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.
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
Is there a way to modify the text in a textbox, or something like that from a different form?
In my program, I need to spawn a new Form2, and have all the textboxes hold information based on stuff entered in Form3. So far I've only done:
Form2 form2 = new Form2();
form2.Show();
I just don't know how to access form2.textbox1.Text, for example, from form3. I've looked online but didn't find quite what I was looking for, and I've tried a few different things with no success.
Thanks! :)
Pass the instance of Form2 into Form3:
public Form3(Form2 referrer)
{
var txt = referrer.TextBox1Text;
}
and then when calling it:
Form3 f3 = new Form3(this);
f3.Show();
and you'll just have to make sure that you build a property on Form2 like this:
internal string TextBox1Text { get { return textBox1.Text; } }
Assuming that you are instantiating the Form2 within the code behind of Form3 (calling "new" Form2())...
You can create a public method within Form2 that will provide the accessibility you need. Example:
(within Form2 - code behind)
public SetTextboxTextProperty(string text)
{
this.Textbox1.Text = text;
}
From Form3:
Form2 form2 = new Form2();
form2.SetTextboxTextProperty("your data");
form2.Show();
The problem is that the controls defined in each form are not public objects, so you can't access them from outside the form.
Why don't you define a public method in each form to retrieve/set the value you need from the controls you need?
I think that it would be a better approach than exposing your controls.
I am still new to C# so please bear with me.
I have Form1 with a DataGridView and a Button. This button opens Form2.
Form2 contains a TextBox and a Button that closes Form2.
I need to write the text from the TextBox in Form2 into the first cell of the DataGridView in Form1. In the application I am developing, there is other data already in the DataGridView in Form1.
I have uploaded the Visual Studio 2010 file here.
EDIT:
Please look at this screenshot:
Here is the code I'm using:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.dataGridView1.Rows[0].Cells[0].Value = textBox1.Text;
this.Close();
}
}
I seem to instantiating a new Form1 when I don't want to.
Appreciate the help.
You don't need Form2 to instantiate (again) the main form (Form1).
A more appropriate approach is to open the auxiliary form containing the text-box as a modal-dialog window, and let the opener form (Form1) access the text entered by the user at Form2 instance.
Here below are described the changes needed:
Form2 changes:
1.- Add a new class member to store the string that is to be introduced in the text-box textBox1.
public String textFromTextBox = null;
2.- Add code to your OK button's clic event-handler, so that you store in the new class member textFromTextBox the value introduced in the text-box:
3.- Last, in the same clic event-handling code set the DialogResult property to DialogResult.OK.
The Form2 code would look like this:
public partial class Form2 : Form
{
[...]
// This class member will store the string value
// the user enters in the text-box
public String textFromTextBox = null;
[...]
// This is the event-handling code that you must place for
// the OK button.
private void button1_Click(object sender, EventArgs e)
{
this.textFromTextBox = this.textBox1.Text;
this.DialogResult = DialogResult.OK;
}
}
Form1 changes
1.- In your button with the label "Enter Text" (that is actually missing in your code), in the Click event-handler put the code necessary to open the Form2 as a modal Dialog.
2.- Set the cell value in your data-grid accordingly by recovering the value stored in the Form2's textFromTextBox member.
3.- Finally dispose your Form2 instance.
Form2 myFormWithATextBox = new Form2();
if (myFormWithATextBox.ShowDialog(this) == DialogResult.OK)
{
this.dataGridView1.Rows[0].Cells[0].Value = myFormWithATextBox.textFromTextBox;
}
myFormWithATextBox.Dispose();
Take into account that your main form is Form1 while Form2 it is just an auxiliary form control, it should not take much control on the flow of your application, and therefore not assume the responsibility of instantiating the main form.
You can pass variable from Form to another by create another contractor that accept parameters as the following:-
1) go to form1 then create another contractor:
public Form1(string myString)
{
InitializeComponent();
if (myString != null)
dataGridView1.Rows[0].Cells[0].Value = myString;
}
2) go to form2 and under the button write this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1(textBox1.Text);
frm1.ShowDialog();
}
Here you are your application after modification
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.