zoukankan      html  css  js  c++  java
  • 【Vegas原创】Winform中使用MaskedTextBox制作IP地址输入框

    环境:C/S Winform C#

    Demo:

    image

    功能:自动设置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

    喜欢请赞赏一下啦^_^
  • 相关阅读:
    Eclipse中配置Tomcat碰到Server Tomcat v6.0 Server at localhost failed to start问题
    解决java中对URL编码的问题
    上白泽慧音
    小K的农场
    [USACO15JAN]草鉴定Grass Cownoisseur
    [HNOI2012]矿场搭建/Mining Your Own Business
    [POI2008]BLO-Blockade
    「JOISC 2018 Day 1」帐篷
    Sudoku
    序列
  • 原文地址:https://www.cnblogs.com/amadeuslee/p/3744170.html
Copyright © 2011-2022 走看看