Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- How to update textbox in form1 from form2?
- // 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 );
- }
- 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; }
- 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; }
- }
- }
- 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;
- }
- AddEntryWindow addEntryWindow = new AddEntryWindow();
- addEntryWindow.ShowDialog(this);
- foreach (AddEntry list in addedEntry)
- {
- // Displaying and formating the output in text box in MainWindow.
- ((MainWindow)owner).txtDisplayFileContent.Text += txtUserName.Text;
- }
Add Comment
Please, Sign In to add comment