【Vegas原创】Winform中使用MaskedTextBox制作IP地址输入框
环境: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(" ",""));
喜欢请赞赏一下啦^_^赞赏后请加wx:weyocul,相关文章提到的的安装包稍后给到哦!没有收到赞赏直接加我的,直接无视哈!
 
 
                    
                     
                    
                 
                    
                

 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号