IPAddressBox .cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Drawing;
  5. using System.Data;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. namespace Core.StlMes.Client.ZGMil.ResultConrtrol
  10. {
  11. /// <summary>
  12. /// IP地址输入控件
  13. /// 属性Test 输出IP地址
  14. /// </summary>
  15. public partial class IPAddressBox : UserControl
  16. {
  17. [Category("属性已更改")]
  18. [Browsable(true)]
  19. public event EventHandler TextChanged;
  20. public IPAddressBox()
  21. {
  22. InitializeComponent();
  23. }
  24. private string _text;
  25. [Category("外观")]
  26. [Description("与空间关联的文本")]
  27. [Browsable(true)]
  28. public string Text
  29. {
  30. get
  31. {
  32. if (this.textBox1.Text.Length == 0 || this.textBox2.Text.Length == 0 || this.textBox3.Text.Length == 0 || this.textBox4.Text.Length == 0)
  33. {
  34. _text = "";
  35. return _text;
  36. }
  37. else
  38. {
  39. _text = Convert.ToInt32(this.textBox1.Text).ToString() + "."
  40. + Convert.ToInt32(this.textBox2.Text).ToString() + "."
  41. + Convert.ToInt32(this.textBox3.Text).ToString() + "."
  42. + Convert.ToInt32(this.textBox4.Text).ToString();
  43. return _text;
  44. }
  45. }
  46. set
  47. {
  48. if (value != null)
  49. {
  50. string[] strs = value.Split('.');
  51. Int32[] num = new Int32[4];
  52. if (strs.Length == 4)
  53. {
  54. bool result = true;
  55. for (int i = 0; i < 4; i++)
  56. {
  57. result = result && Int32.TryParse(strs[i], out num[i]);
  58. }
  59. if (result && num[0] <= 223 && num[1] <= 255 && num[2] <= 255 && num[3] <= 255)
  60. {
  61. this.textBox1.Text = strs[0];
  62. this.textBox2.Text = strs[1];
  63. this.textBox3.Text = strs[2];
  64. this.textBox4.Text = strs[3];
  65. }
  66. }
  67. else
  68. {
  69. this.textBox1.Text = "";
  70. this.textBox2.Text = "";
  71. this.textBox3.Text = "";
  72. this.textBox4.Text = "";
  73. _text = "";
  74. }
  75. }
  76. _text = value;
  77. }
  78. }
  79. private void MaskIPAddr(TextBox textBox, KeyPressEventArgs e)
  80. {
  81. //判断输入的值是否为数字或删除键
  82. if (char.IsDigit(e.KeyChar) || e.KeyChar == 8)
  83. {
  84. if (e.KeyChar != 8 && textBox.Text.Length == 2)
  85. {
  86. string tempStr = textBox.Text + e.KeyChar;
  87. if (textBox.Name == "textBox1")
  88. {
  89. if (Int32.Parse(tempStr) > 223)
  90. {
  91. MessageBox.Show(tempStr + " 不是一个有效项目。请指定一个介于 1 和 223 之间的数值。");
  92. textBox.Text = "223";
  93. textBox.Focus();
  94. return;
  95. }
  96. this.textBox2.Focus();
  97. this.textBox2.SelectAll();
  98. }
  99. else if (textBox.Name == "textBox2")
  100. {
  101. if (Int32.Parse(tempStr) > 255)
  102. {
  103. MessageBox.Show(tempStr + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
  104. textBox.Text = "255";
  105. textBox.Focus();
  106. return;
  107. }
  108. this.textBox3.Focus();
  109. this.textBox3.SelectAll();
  110. }
  111. else if (textBox.Name == "textBox3")
  112. {
  113. if (Int32.Parse(tempStr) > 255)
  114. {
  115. MessageBox.Show(tempStr + "不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
  116. textBox.Text = "255";
  117. textBox.Focus();
  118. return;
  119. }
  120. this.textBox4.Focus();
  121. this.textBox4.SelectAll();
  122. }
  123. else if (textBox.Name == "textBox4")
  124. {
  125. if (Int32.Parse(tempStr) > 255)
  126. {
  127. MessageBox.Show(tempStr + " 不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
  128. textBox.Text = "255";
  129. textBox.Focus();
  130. return;
  131. }
  132. }
  133. }
  134. else if (e.KeyChar == 8)
  135. {
  136. if (textBox.Name == "textBox4" && textBox.Text.Length == 0)
  137. {
  138. this.textBox3.Focus();
  139. }
  140. else if (textBox.Name == "textBox3" && textBox.Text.Length == 0)
  141. {
  142. this.textBox2.Focus();
  143. }
  144. else if (textBox.Name == "textBox2" && textBox.Text.Length == 0)
  145. {
  146. this.textBox1.Focus();
  147. }
  148. }
  149. }
  150. else
  151. {
  152. e.Handled = true;
  153. }
  154. }
  155. private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  156. {
  157. MaskIPAddr(this.textBox1, e);
  158. }
  159. private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
  160. {
  161. MaskIPAddr(this.textBox2, e);
  162. }
  163. private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
  164. {
  165. MaskIPAddr(this.textBox3, e);
  166. }
  167. private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
  168. {
  169. MaskIPAddr(this.textBox4, e);
  170. }
  171. private void textBox1_TextChanged(object sender, EventArgs e)
  172. {
  173. if (this.textBox1.Text.Length != 0 && Int32.Parse(this.textBox1.Text) > 223)
  174. {
  175. MessageBox.Show(this.textBox1.Text + "不是一个有效项目。请指定一个介于 1 和 223 之间的数值。");
  176. this.textBox1.Text = "223";
  177. this.textBox1.Focus();
  178. this.textBox1.SelectAll();
  179. }
  180. OnResize(this, new EventArgs());
  181. }
  182. private void textBox2_TextChanged(object sender, EventArgs e)
  183. {
  184. if (this.textBox2.Text.Length != 0 && Int32.Parse(this.textBox2.Text) > 255)
  185. {
  186. MessageBox.Show(this.textBox2.Text + "不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
  187. this.textBox2.Text = "255";
  188. this.textBox2.Focus();
  189. this.textBox2.SelectAll();
  190. }
  191. OnResize(this, new EventArgs());
  192. }
  193. private void textBox3_TextChanged(object sender, EventArgs e)
  194. {
  195. if (this.textBox3.Text.Length != 0 && Int32.Parse(this.textBox3.Text) > 255)
  196. {
  197. MessageBox.Show(this.textBox3.Text + "不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
  198. this.textBox3.Text = "255";
  199. this.textBox3.Focus();
  200. this.textBox3.SelectAll();
  201. }
  202. OnResize(this, new EventArgs());
  203. }
  204. private void textBox4_TextChanged(object sender, EventArgs e)
  205. {
  206. if (this.textBox4.Text.Length != 0 && Int32.Parse(this.textBox4.Text) > 255)
  207. {
  208. MessageBox.Show(this.textBox4.Text + "不是一个有效项目。请指定一个介于 1 和 255 之间的数值。");
  209. this.textBox4.Text = "255";
  210. this.textBox4.Focus();
  211. this.textBox4.SelectAll();
  212. }
  213. OnResize(this, new EventArgs());
  214. }
  215. protected void OnResize(object sender, EventArgs e)
  216. {
  217. if (TextChanged != null)
  218. {
  219. TextChanged(sender, e);
  220. }
  221. }
  222. }
  223. }