code_junkie

C# issue: How do I manipulate a Textbox on one form from another form

Nov 14th, 2011
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.41 KB | None | 0 0
  1. namespace LCR_ShepherdStaffupdater_1._0
  2. {
  3. partial class formErrorWindow
  4. {
  5. /// <summary>
  6. /// Required designer variable.
  7. /// </summary>
  8. public System.ComponentModel.IContainer components = null;
  9.  
  10. /// <summary>
  11. /// Clean up any resources being used.
  12. /// </summary>
  13. /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  14. ///
  15.  
  16. protected override void Dispose(bool disposing)
  17. {
  18. if (disposing && (components != null))
  19. {
  20. components.Dispose();
  21. }
  22. base.Dispose(disposing);
  23. }
  24.  
  25. #region Windows Form Designer generated code
  26.  
  27. /// <summary>
  28. /// Required method for Designer support - do not modify
  29. /// the contents of this method with the code editor.
  30. /// </summary>
  31. // ********* HERE IS THE FUNCTION THAT IVE ADDED BELOW. THIS WOULD WORK BUT.... *********
  32. public static void showError(string errorTitle, string usrMsg, string errorMsg)
  33. {
  34. formErrorWindow errorWindow = new formErrorWindow();
  35. errorMsgItem.Text = errorMsg;
  36. errorTitleItem.Text = "Error! : " + errorTitle;
  37. usrMsgItem.Text = usrMsg;
  38. errorWindow.ShowDialog();
  39. }
  40. // ********* HERE IS THE FUNCTION THAT IVE ADDED ABOVE. THIS WOULD WORK BUT.... *********
  41. // ********* I get an error: "An object reference is required for the non-static field, method, or property." *********
  42. public void InitializeComponent()
  43. {
  44. System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(formErrorWindow));
  45. this.usrMsgItem = new System.Windows.Forms.TextBox();
  46. this.errorTitleItem = new System.Windows.Forms.Label();
  47. this.errorMsgItem = new System.Windows.Forms.TextBox();
  48. this.button1 = new System.Windows.Forms.Button();
  49. this.panel1 = new System.Windows.Forms.Panel();
  50. this.label2 = new System.Windows.Forms.Label();
  51. this.panel1.SuspendLayout();
  52. this.SuspendLayout();
  53. //
  54. // usrMsgItem
  55. //
  56. this.usrMsgItem.Enabled = false;
  57. this.usrMsgItem.Location = new System.Drawing.Point(13, 37);
  58. this.usrMsgItem.Multiline = true;
  59. this.usrMsgItem.Name = "usrMsgItem";
  60. this.usrMsgItem.Size = new System.Drawing.Size(334, 81);
  61. this.usrMsgItem.TabIndex = 0;
  62. this.usrMsgItem.Text = "Undefined";
  63. //
  64. // errorTitleItem
  65. //
  66. this.errorTitleItem.AutoSize = true;
  67. this.errorTitleItem.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  68. this.errorTitleItem.ForeColor = System.Drawing.Color.Red;
  69. this.errorTitleItem.Location = new System.Drawing.Point(12, 9);
  70. this.errorTitleItem.Name = "errorTitleItem";
  71. this.errorTitleItem.Size = new System.Drawing.Size(152, 20);
  72. this.errorTitleItem.TabIndex = 1;
  73. this.errorTitleItem.Text = "Error! : Undefined";
  74. //
  75. // errorMsgItem
  76. //
  77. this.errorMsgItem.Enabled = false;
  78. this.errorMsgItem.Location = new System.Drawing.Point(0, 21);
  79. this.errorMsgItem.Multiline = true;
  80. this.errorMsgItem.Name = "errorMsgItem";
  81. this.errorMsgItem.Size = new System.Drawing.Size(329, 101);
  82. this.errorMsgItem.TabIndex = 2;
  83. this.errorMsgItem.Text = "Undefined";
  84. //
  85. // button1
  86. //
  87. this.button1.Location = new System.Drawing.Point(272, 256);
  88. this.button1.Name = "button1";
  89. this.button1.Size = new System.Drawing.Size(75, 23);
  90. this.button1.TabIndex = 3;
  91. this.button1.Text = "Continue";
  92. this.button1.UseVisualStyleBackColor = true;
  93. this.button1.Click += new System.EventHandler(this.button1_Click);
  94. //
  95. // panel1
  96. //
  97. this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))));
  98. this.panel1.Controls.Add(this.label2);
  99. this.panel1.Controls.Add(this.errorMsgItem);
  100. this.panel1.Location = new System.Drawing.Point(12, 124);
  101. this.panel1.Name = "panel1";
  102. this.panel1.Size = new System.Drawing.Size(335, 126);
  103. this.panel1.TabIndex = 4;
  104. //
  105. // label2
  106. //
  107. this.label2.AutoSize = true;
  108. this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
  109. this.label2.Location = new System.Drawing.Point(68, 1);
  110. this.label2.Name = "label2";
  111. this.label2.Size = new System.Drawing.Size(189, 17);
  112. this.label2.TabIndex = 3;
  113. this.label2.Text = "Technical Error Message";
  114. //
  115. // formErrorWindow
  116. //
  117. this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
  118. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
  119. this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(192)))), ((int)(((byte)(192)))));
  120. this.ClientSize = new System.Drawing.Size(359, 290);
  121. this.Controls.Add(this.panel1);
  122. this.Controls.Add(this.button1);
  123. this.Controls.Add(this.errorTitleItem);
  124. this.Controls.Add(this.usrMsgItem);
  125. this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
  126. this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  127. this.MaximizeBox = false;
  128. this.MinimizeBox = false;
  129. this.Name = "formErrorWindow";
  130. this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
  131. this.Text = "Error!";
  132. this.VisibleChanged += new System.EventHandler(this.formErrorWindow_VisibleChanged);
  133. this.panel1.ResumeLayout(false);
  134. this.panel1.PerformLayout();
  135. this.ResumeLayout(false);
  136. this.PerformLayout();
  137.  
  138. }
  139.  
  140. #endregion
  141.  
  142. private System.Windows.Forms.Button button1;
  143. private System.Windows.Forms.Panel panel1;
  144. private System.Windows.Forms.Label label2;
  145. public System.Windows.Forms.TextBox usrMsgItem;
  146. public System.Windows.Forms.Label errorTitleItem;
  147. public System.Windows.Forms.TextBox errorMsgItem;
  148. }
  149. }
  150.  
  151. pseudo: public void updateTextBox(string new_text)
  152.  
  153. MyMessageBox messageBox = new MyMessageBox("My error message");
  154. messageBox.Show();
  155.  
  156. public Form2(Form1 form1)
  157. {
  158. InitializeComponent();
  159.  
  160. Owner = form1;
  161. _form1 = form1;
  162. }
  163.  
  164. _form1.theForm1ControlName
  165.  
  166. public void SetErrorText(string errorText)
  167. {
  168. this.ErrorMessage.Text = errorText; // assuming the TextBox' ID is ErrorMessage.
  169. }
  170.  
  171. formErrorWindow myErrorWindow;
  172.  
  173. formErrorWindow myErrorWindow = new formErrorWindow();
  174.  
  175. formErrorWindow myErrorWindow = new formErrorWindow();
  176. myErrorWindow.showError("Title", "User Message Here", "Error Message Here");
  177. myErrorWindow.ShowDialog();
  178.  
  179. public partial class Form1 : Form
  180. {
  181. private static TextBox box;
  182.  
  183. public Form1()
  184. {
  185. InitializeComponent();
  186. }
  187.  
  188.  
  189.  
  190. private void Form1_Load(object sender, EventArgs e)
  191. {
  192.  
  193. // box
  194. box = new TextBox();
  195. box.Location = new System.Drawing.Point(87, 230);
  196. box.Name = "box";
  197. box.Size = new System.Drawing.Size(100, 20);
  198. box.TabIndex = 1;
  199.  
  200. this.Controls.Add(box);
  201.  
  202. }
  203. }
Add Comment
Please, Sign In to add comment