【Vegas原创】Winform中使用MaskedTextBox制作IP地址输入框

2年前 (2022) 程序员胖胖胖虎阿
271 0 0

环境:C/S Winform C#

 

Demo:

【Vegas原创】Winform中使用MaskedTextBox制作IP地址输入框

 

功能:自动设置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(" ",""));

 

 

参考文档:http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/27d1e86e-8561-4379-b5c5-401f15c24c97

相关文章

暂无评论

暂无评论...