一个简单的IP地址输入控件

一个模仿Windows的IP地址分段输入控件框
- .cs

namespace IPBox
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        public string get_ip()
        {
            if (ip0.Text == null || ip1.Text == null || ip2.Text == null || ip3.Text == null)
            {
                return null;
            }
            else
            {
                string ipaddr = ip0.Text.Trim() + '.' + ip1.Text.Trim() + '.' + ip2.Text.Trim() + '.' + ip3.Text.Trim();
                return ipaddr;
            }
        }

        private void textChange(object sender, TextChangedEventArgs e)
        {
            TextBox tb = (TextBox)sender;
            Regex rgx = new Regex(@"^(2[0-4]\d|25[0-5]|[0|1]?\d\d?|(2[0-4]\d|25[0-5]|[0|1]?\d\d?\.))$");
            string ss = tb.Text;
            if (!rgx.IsMatch(ss))
            {
                tb.Text = "";
            }
            else
            {
                int temp = ss.IndexOf(".");
                if (temp > -1)
                {
                    ss = ss.Replace(".", "");
                    tb.Text = ss;
                    FrameworkElement fsource = e.Source as FrameworkElement;
                    fsource.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
                if (ss.Length >= 3)
                {
                    FrameworkElement fsource = e.Source as FrameworkElement;
                    fsource.MoveFocus(new TraversalRequest(FocusNavigationDirection.Next));
                }
            }
        }
    }
}
  • .xaml
<Window x:Class="IPBox.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="100" Width="150"  >
    <Grid>
        <Border>
            <StackPanel Orientation="Horizontal" >
                <StackPanel Orientation="Horizontal"  >
                    <TextBox Name="ip0"  Text="" BorderThickness="0"  Width="28" TabIndex="-4"   VerticalAlignment="Center" Margin="0" TextChanged="textChange"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBox  Text="." BorderThickness="0" IsReadOnly="True" VerticalAlignment="Center" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBox Name="ip1"  Text=""  BorderThickness="0" Width="28" TabIndex="-3"   VerticalAlignment="Center" Margin="0" TextChanged="textChange"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBox  Text="." BorderThickness="0" IsReadOnly="True" VerticalAlignment="Center" />
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBox Name="ip2"  Text=""  BorderThickness="0" Width="28" TabIndex="-2"   VerticalAlignment="Center" Margin="0" TextChanged="textChange"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBox  Text="."  BorderThickness="0" IsReadOnly="True" VerticalAlignment="Center"/>
                </StackPanel>
                <StackPanel Orientation="Horizontal">
                    <TextBox Name="ip3"  Text=""  BorderThickness="0" Width="28" TabIndex="-1"   VerticalAlignment="Center" Margin="0" TextChanged="textChange"/>
                </StackPanel>
            </StackPanel>
        </Border>
    </Grid>
</Window>

一个IP地址输入控件框

存在问题:输入IP地址以后,按backspace删除时只能删除一个地址段,不能连续删除

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页