Guest User

Untitled

a guest
Aug 17th, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. How to update textbox in form1 from form2?
  2. // specifically typecasting the TYPE of form being passed in,
  3. // not just a generic form. We need to see the exposed elements
  4. Form1 CalledFrom;
  5. // Ensure to do the : this() to make sure default behavior
  6. // to initialize the controls on the form are created too.
  7. public Form2(Form1 viaParameter) : this()
  8. {
  9. CalledFrom = viaParameter;
  10. }
  11.  
  12. private void btnOnForm2_Click(object sender, EventArgs e)
  13. {
  14. CalledFrom.ValuesByProperty = this.txtOnForm2.Text;
  15. MessageBox.Show( "Check form 1 textbox" );
  16.  
  17. string GettingBack = CalledFrom.ValuesByProperty;
  18. MessageBox.Show( GettingBack );
  19.  
  20. CalledFrom.SetViaMethod( "forced value, not from textbox" );
  21. MessageBox.Show( "Check form 1 textbox" );
  22.  
  23. GettingBack = CalledFrom.GetViaMethod();
  24. MessageBox.Show( GettingBack );
  25. }
  26.  
  27. private void button1_Click(object sender, EventArgs e)
  28. {
  29. Form2 oFrm = new Form2(this);
  30. oFrm.Show();
  31. }
  32.  
  33. public string ValuesByProperty
  34. {
  35. get { return this.textBox1.Text; }
  36. set { this.textBox1.Text = value; }
  37. }
  38.  
  39. public void SetViaMethod(string newValue)
  40. { this.textBox1.Text = newValue; }
  41.  
  42. public string GetViaMethod()
  43. { return this.textBox1.Text; }
  44.  
  45. public partial class YourFirstForm
  46. {
  47. // example to expose a method on first form and pass IN a value
  48. public void SetMyObject( string ValueFromSecondForm )
  49. {
  50. this.txtBox1.Text = ValueFromSecondForm;
  51. }
  52.  
  53. // example via a property you are trying to set... identical in results
  54. public string ViaSetAsProperty
  55. { set { this.txtBox1.Text = value; } }
  56.  
  57. // Now, the reverse, lets expose some things from form 1 to the second...
  58. public string GetMyObjectText()
  59. {
  60. return this.txtBox1.Text;
  61. }
  62.  
  63. // or via a GETTER property...
  64. public string GettingText
  65. { get { return this.txtBox1.Text; } }
  66.  
  67.  
  68. // However, if you want to allow both set and get to form 1's values,
  69. // do as a single property with both getter / setter exposed..
  70. public string TextContent
  71. { get { return this.txtBox1.Text; }
  72. set { this.txtBox1.text = value; }
  73. }
  74. }
  75.  
  76. public YourSecondForm( Form passedInVar )
  77. {
  78. // preserve the first form
  79. ObjRefToFirstForm = passedInVar;
  80. }
  81.  
  82. // Now, however you want to handle... via a button click, the text change event, etc
  83. public void SendDataToForm1()
  84. {
  85. // via calling the METHOD on form 1 that is public
  86. ObjRefToFirstForm.SetMyObj( this.SomeOtherTextbox.Text );
  87.  
  88. // via a SETTER on form 1
  89. ObjRefToFirstForm.ViaSetAsProperty = this.SomeOtherTextbox.Text;
  90.  
  91.  
  92. // sample to GET something from form 1 via method
  93. string TestGet = ObjRefToFirstForm.GetMyObjectText();
  94.  
  95. // or from the exposed property
  96. TestGet = ObjRefToFirstForm.GettingText;
  97.  
  98.  
  99. // Now, try via the one property that has both a setter and getter
  100. ObjRefToFirstForm.TextContent = this.SomeOtherTextbox.Text;
  101. TestGet = ObjRefToFirstForm.TextContent;
  102. }
  103.  
  104. AddEntryWindow addEntryWindow = new AddEntryWindow();
  105. addEntryWindow.ShowDialog(this);
  106.  
  107. foreach (AddEntry list in addedEntry)
  108. {
  109. // Displaying and formating the output in text box in MainWindow.
  110. ((MainWindow)owner).txtDisplayFileContent.Text += txtUserName.Text;
  111. }
Add Comment
Please, Sign In to add comment