Reputation: 6480
I have 2 Windows Forms.
In first, the main window form, has multiline textbox and a button. The button opens the second form in which I can add data to array by using AddEntry object.
In second form I have textboxes and a button (btnAddEntry) which should update the content of the textbox from first form.
When data is entered I want to display data in textbox from the first form.
The problem is that code I came up with doesn't seem to work.
How would I solve this?
Upvotes: 0
Views: 10253
Reputation: 3312
Your problem is that MainWindow mainWindow = new MainWindow() creates a new version of your MainWindow not a reference to the already existing version. In your MainWindow form, when opening your 2nd form you need to pass a reference along to the 2nd form by passing it into the Show method (which stores it in a variable called owner of type object) like this:
AddEntryWindow addEntryWindow = new AddEntryWindow();
addEntryWindow.ShowDialog(this);
Then you can reference the textbox like this:
foreach (AddEntry list in addedEntry)
{
// Displaying and formating the output in text box in MainWindow.
((MainWindow)owner).txtDisplayFileContent.Text += txtUserName.Text;
}
Upvotes: 1
Reputation: 48129
To get the BASICs working, do the following.. Create a new project. Nothing of your current code, windows, etc... The default project will create a form "Form1" leave it alone for now.
Add a NEW form to the project, it will default to "Form2"... Put a single textbox on it and a single button on it. For grins, and clarification to differentiate between the object names, change the name of the controls on Form2 to "txtOnForm2", and "btnOnForm2" (case sensitive for my sample, and readability vs "txtonform2" all lower case). Now, on the form, right-click and click "View Code". It will bring you to the other "Partial class" declaration where your constructors are found. Add the following, dont worry about compile errors as the other half will be when we put code into Form1 next...
// specifically typecasting the TYPE of form being passed in,
// not just a generic form. We need to see the exposed elements
Form1 CalledFrom;
// Ensure to do the : this() to make sure default behavior
// to initialize the controls on the form are created too.
public Form2(Form1 viaParameter) : this()
{
CalledFrom = viaParameter;
}
private void btnOnForm2_Click(object sender, EventArgs e)
{
CalledFrom.ValuesByProperty = this.txtOnForm2.Text;
MessageBox.Show( "Check form 1 textbox" );
string GettingBack = CalledFrom.ValuesByProperty;
MessageBox.Show( GettingBack );
CalledFrom.SetViaMethod( "forced value, not from textbox" );
MessageBox.Show( "Check form 1 textbox" );
GettingBack = CalledFrom.GetViaMethod();
MessageBox.Show( GettingBack );
}
Save and close the Form2 designer and code window.
Now, open Form1. Put a single textbox and a single button on it. The default names for the controls will be "textbox1" and "button1" respectively. Leave it as is. Double click on the button (on form1). It will bring up a snippet for code for that button. Paste the following
private void button1_Click(object sender, EventArgs e)
{
Form2 oFrm = new Form2(this);
oFrm.Show();
}
public string ValuesByProperty
{
get { return this.textBox1.Text; }
set { this.textBox1.Text = value; }
}
public void SetViaMethod(string newValue)
{ this.textBox1.Text = newValue; }
public string GetViaMethod()
{ return this.textBox1.Text; }
Now, save the forms and run them. Click button on first form, calls the second with the already created instance of itself, not a new SECOND instance of itself. The second form will be displayed. Shift the windows so you can see both.
Enter some text in the textbox second window and click the button... follow the back/forth of what is coming/going.
Upvotes: 3
Reputation: 48129
This has been answers other times, similar principles...
Take a look at similar question
To "Pass-back" information from: Form2 to Form1, you will need to create a public method on it to expose what you want to touch. Then from the second form (which basically has a pointer instance to the first, call that method with whatever you want to do / act upon. This could also be done with doing a public property to act upon.
From what you may already have working...
public partial class YourFirstForm
{
// example to expose a method on first form and pass IN a value
public void SetMyObject( string ValueFromSecondForm )
{
this.txtBox1.Text = ValueFromSecondForm;
}
// example via a property you are trying to set... identical in results
public string ViaSetAsProperty
{ set { this.txtBox1.Text = value; } }
// Now, the reverse, lets expose some things from form 1 to the second...
public string GetMyObjectText()
{
return this.txtBox1.Text;
}
// or via a GETTER property...
public string GettingText
{ get { return this.txtBox1.Text; } }
// However, if you want to allow both set and get to form 1's values,
// do as a single property with both getter / setter exposed..
public string TextContent
{ get { return this.txtBox1.Text; }
set { this.txtBox1.text = value; }
}
}
Now, for how to get from your SECOND form
public partial class YourSecondForm { Form ObjRefToFirstForm;
public YourSecondForm( Form passedInVar )
{
// preserve the first form
ObjRefToFirstForm = passedInVar;
}
// Now, however you want to handle... via a button click, the text change event, etc
public void SendDataToForm1()
{
// via calling the METHOD on form 1 that is public
ObjRefToFirstForm.SetMyObj( this.SomeOtherTextbox.Text );
// via a SETTER on form 1
ObjRefToFirstForm.ViaSetAsProperty = this.SomeOtherTextbox.Text;
// sample to GET something from form 1 via method
string TestGet = ObjRefToFirstForm.GetMyObjectText();
// or from the exposed property
TestGet = ObjRefToFirstForm.GettingText;
// Now, try via the one property that has both a setter and getter
ObjRefToFirstForm.TextContent = this.SomeOtherTextbox.Text;
TestGet = ObjRefToFirstForm.TextContent;
}
}
Hopefully this exposes ways to both get and set content between forms for you... both as method() approach and/or get/set via properties.
Upvotes: 1