Advertisement
Guest User

Untitled

a guest
Dec 14th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace TextBoxTest
  12. {
  13. public partial class Form1 : Form
  14. {
  15. public Form1()
  16. {
  17.  
  18. InitializeComponent();
  19. this.btnOK.Enabled = true;
  20. // Tag values for testing if the data is valid
  21. this.txtAddress.Tag = false;
  22. this.txtAge.Tag = false;
  23. this.txtName.Tag = false;
  24. this.txtOccupation.Tag = false;
  25. // Subscriptions to events
  26. this.txtName.Validating += new
  27. System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
  28. this.txtAddress.Validating += new
  29. System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
  30. this.txtOccupation.Validating += new
  31. System.ComponentModel.CancelEventHandler(this.txtOccupation_Validating);
  32. this.txtAge.KeyPress += new
  33. System.Windows.Forms.KeyPressEventHandler(this.txtAge_KeyPress);
  34. this.txtAge.Validating += new
  35. System.ComponentModel.CancelEventHandler(this.txtBoxEmpty_Validating);
  36. this.txtName.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
  37. this.txtAddress.TextChanged += new
  38. System.EventHandler(this.txtBox_TextChanged);
  39. this.txtAge.TextChanged += new System.EventHandler(this.txtBox_TextChanged);
  40. this.txtOccupation.TextChanged += new
  41. System.EventHandler(this.txtBox_TextChanged);
  42.  
  43. }
  44.  
  45. private void Form1_Load(object sender, EventArgs e)
  46. {
  47.  
  48. }
  49.  
  50. private void label1_Click(object sender, EventArgs e)
  51. {
  52.  
  53. }
  54.  
  55. private void textBox5_TextChanged(object sender, EventArgs e)
  56. {
  57.  
  58. }
  59.  
  60.  
  61.  
  62.  
  63. private void btnOK_Click(object sender, System.EventArgs e)
  64. {
  65. // No testing for invalid values are made, as that should
  66. // not be necessary
  67. string output;
  68. // Concatenate the text values of the four TextBoxes
  69. output = "Name: " + this.txtName.Text + "\r\n";
  70. output += "Address: " + this.txtAddress.Text + "\r\n";
  71. output += "Occupation: " + this.txtOccupation.Text + "\r\n";
  72. output += "Age: " + this.txtAge.Text;
  73. // Insert the new text
  74. this.txtOutput.Text = output;
  75. }
  76.  
  77.  
  78. private void textBox3_TextChanged(object sender, EventArgs e)
  79. {
  80.  
  81. }
  82.  
  83. private void btnHelp_Click(object sender, System.EventArgs e)
  84. {
  85. // Write a short description of each TextBox in the Output TextBox
  86. string output;
  87. output = "Name = Your name\r\n";
  88. output += "Address = Your address\r\n";
  89. output += "Occupation = Only allowed value is 'Programmer'\r\n";
  90. output += "Age = Your age";
  91. // Insert the new text
  92. this.txtOutput.Text = output;
  93. }
  94.  
  95. private void txtOccupation_Validating(object sender, System.ComponentModel.CancelEventArgs e)
  96. {
  97. // Cast the sender object to a textbox
  98. TextBox tb = (TextBox)sender;
  99. // Check if the values are correct
  100. if (tb.Text.CompareTo("Programmer") == 0 || tb.Text.Length == 0)
  101. {
  102. tb.Tag = true;
  103. tb.BackColor = System.Drawing.SystemColors.Window;
  104. }
  105. else
  106. {
  107. tb.Tag = false;
  108. tb.BackColor = Color.Red;
  109. }
  110. // Set the state of the OK button
  111. ValidateAll();
  112. }
  113.  
  114. private void txtBoxEmpty_Validating(object sender, System.ComponentModel.CancelEventArgs e)
  115. {
  116. // We know the sender is a TextBox, so we cast the sender object to that
  117. TextBox tb = (TextBox)sender;
  118. // If the text is empty we set the background color of the
  119. // Textbox to red to indicate a problem. We use the tag value
  120. // of the control to indicate if the control contains valid
  121. // information.
  122. if (tb.Text.Length == 0)
  123. {
  124. tb.BackColor = Color.Red;
  125. tb.Tag = false;
  126.  
  127. // In this case we do not want to cancel further processing,
  128. // but if we had wanted to do this, we would have added this line:
  129. // e.Cancel = true;
  130. }
  131. else
  132. {
  133. tb.BackColor = System.Drawing.SystemColors.Window;
  134. tb.Tag = true;
  135. }
  136. // Finally, we call ValidateAll which will set the value of
  137. // the OK button.
  138. ValidateAll();
  139. }
  140.  
  141. private void txtAge_KeyPress(object sender,
  142. System.Windows.Forms.KeyPressEventArgs e)
  143. {
  144. if ((e.KeyChar < 48 || e.KeyChar > 57) && e.KeyChar != 8)
  145. e.Handled = true; // Remove the character
  146. }
  147.  
  148. private void txtBox_TextChanged(object sender, System.EventArgs e)
  149. {
  150. // Cast the sender object to a Textbox
  151. TextBox tb = (TextBox)sender;
  152. // Test if the data is valid and set the tag and background
  153. // color accordingly.
  154. if (tb.Text.Length == 0 && tb != txtOccupation)
  155. {
  156. tb.Tag = false;
  157. tb.BackColor = Color.Red;
  158. }
  159. else if (tb == txtOccupation &&
  160. (tb.Text.Length != 0 && tb.Text.CompareTo("Programmer") != 0))
  161. {
  162. // Don't set the color here, as it will color change while the user
  163. // is typing
  164. tb.Tag = false;
  165. }
  166. else
  167. {
  168. tb.Tag = true;
  169. tb.BackColor = SystemColors.Window;
  170. }
  171. // Call ValidateAll to set the OK button
  172. ValidateAll();
  173. }
  174.  
  175. private void ValidateAll()
  176. {
  177. // Set the OK button to enabled if all the Tags are true
  178. this.btnOK.Enabled = ((bool)(this.txtAddress.Tag) &&
  179. (bool)(this.txtAge.Tag) &&
  180. (bool)(this.txtName.Tag) &&
  181. (bool)(this.txtOccupation.Tag));
  182. }
  183.  
  184.  
  185.  
  186. }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement