环境:C/S Winform C#
Demo:
图片
功能:自动设置ip掩码,输入形如999.999.999.999的格式,并设置keydown事件,当输入.的时候,自动跳至下一栏。
方法:
1,从工具箱中拖入一个MaskedTextBox,命名为txtPACSIP;
2,在mask属性中,输入:999.999.999.999
3,在prompt属性中,将_换为空格。如果你喜欢_的话,也可以不用换。
4,创建KeyDown事件,附以下代码:
private void txtPACSIP_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Decimal) { int pos = txtPACSIP.SelectionStart; int max = (txtPACSIP.MaskedTextProvider.Length - txtPACSIP.MaskedTextProvider.EditPositionCount); int nextField = 0; for (int i = 0; i < txtPACSIP.MaskedTextProvider.Length; i++) { if (!txtPACSIP.MaskedTextProvider.IsEditPosition(i) && (pos + max) >= i) nextField = i; } nextField += 1; // We're done, enable the TabStop property again txtPACSIP.SelectionStart = nextField; } }
当然, 你如果想将tab键也实现自动跳至下一栏的话,多加个条件就行。
5,在取值的过程中,记得要replace空格:
PingReply reply = p1.Send(this.txtPACSIP.Text.Replace(" ",""));本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报。