Silverlight IP地址控件

Silverlight IP地址控件,第1张

概述<span style="font-size:14px;"><UserControl x:Class="<span style="font-family: Arial, Helvetica, sans-serif;">Controls</span><span style="font-family: Arial, Helvetica, sans-serif;">.IPTextBox"</span>

<span ><UserControl x:Class="<span >Controls</span><span >.IPTextBox"</span> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    xmlns:d="http://schemas.microsoft.com/Expression/blend/2008"    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"      mc:Ignorable="d" d:DesignHeight="26" d:DesignWIDth="180">    <border  borderThickness="1" CornerRadius="1">        <border.borderBrush>            <SolIDcolorBrush Opacity="0.5" color="Black" />        </border.borderBrush>        <GrID HorizontalAlignment="Stretch">            <GrID.ColumnDeFinitions>                <ColumnDeFinition WIDth="*"/>                <ColumnDeFinition WIDth="10"/>                <ColumnDeFinition WIDth="*"/>                <ColumnDeFinition WIDth="10"/>                <ColumnDeFinition WIDth="*"/>                <ColumnDeFinition WIDth="10"/>                <ColumnDeFinition WIDth="*"/>            </GrID.ColumnDeFinitions>            <TextBox GrID.Column="0" name="tbIP_1" TabIndex="1" MaxLength="3" Opacity="1"                         borderThickness="0" Height="25" MinWIDth="40" FontSize="15"                         TextAlignment="Center"                           VerticalAlignment="Center" VerticalContentAlignment="Center"                          HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"                          TextChanged="TextBox_OnTextChanged"                          KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus">            </TextBox>            <sdk:Label GrID.Column="1" Background="{Binding Background,Elementname=tbIP_1}" borderThickness="0" Height="25" WIDth="10" HorizontalAlignment="Center">.</sdk:Label>            <TextBox GrID.Column="2" name="tbIP_2" TabIndex="2" MaxLength="3"  Opacity="1"                         borderThickness="0" Height="25" MinWIDth="40" FontSize="15"                         TextAlignment="Center"                           VerticalAlignment="Center" VerticalContentAlignment="Center"                          HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"                           TextChanged="TextBox_OnTextChanged"                          KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus">            </TextBox>            <sdk:Label GrID.Column="3" Background="{Binding Background,Elementname=tbIP_1}" borderThickness="0" Height="25" WIDth="10" HorizontalAlignment="Center">.</sdk:Label>            <TextBox GrID.Column="4" name="tbIP_3" TabIndex="3" MaxLength="3"  Opacity="1"                         borderThickness="0" Height="25" MinWIDth="40" FontSize="15"                         TextAlignment="Center"                           VerticalAlignment="Center" VerticalContentAlignment="Center"                          HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"                          TextChanged="TextBox_OnTextChanged"                          KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus">            </TextBox>            <sdk:Label GrID.Column="5" Background="{Binding Background,Elementname=tbIP_1}" borderThickness="0" Height="25" WIDth="10" HorizontalAlignment="Center">.</sdk:Label>            <TextBox GrID.Column="6" name="tbIP_4" TabIndex="4" MaxLength="3"  Opacity="1"                         borderThickness="0" Height="25" MinWIDth="40" FontSize="15"                         TextAlignment="Center"                           VerticalAlignment="Center" VerticalContentAlignment="Center"                          HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"                          TextChanged="TextBox_OnTextChanged"                          KeyDown="TextBox_OnKeyDown" GotFocus="TextBox_OnGotFocus">            </TextBox>        </GrID>    </border></UserControl></span>

下面是后台代码:

using System;using System.Collections.Generic;using System.linq;using System.Net;using System.windows;using System.windows.Controls;using System.windows.documents;using System.windows.input;using System.windows.Media;using System.windows.Media.Animation;using System.windows.Shapes;namespace Controls{    public partial class IPTextBox : UserControl    {        public event EventHandler OnValueChanged;        #region 自定义依赖项属性        /// <summary>        /// IP地址        /// </summary>        public string Text        {            get { return GetValue(TextProperty).ToString(); }            set { SetValue(TextProperty,value); }        }        public static Readonly DependencyProperty TextProperty = DependencyProperty.Register("Text",typeof(string),typeof(IPTextBox),new PropertyMetadata(new PropertyChangedCallback(OnTextPropertyChanged)));        private static voID OnTextPropertyChanged(DependencyObject d,DependencyPropertyChangedEventArgs e)        {            IPTextBox tb = d as IPTextBox;            tb.SetIP(tb.GetValue(TextProperty).ToString());        }        /// <summary>        /// IP地址模式(normal:普通IP; Mask:子网掩码)        /// </summary>        public IPAddressMode IPMode        {            get { return (IPAddressMode)GetValue(IPModelProperty); }            set { SetValue(IPModelProperty,value); }        }        public static Readonly DependencyProperty IPModelProperty = DependencyProperty.Register("IPMode",typeof(IPAddressMode),new PropertyMetadata(IPAddressMode.normal));        #endregion        public IPTextBox()        {            InitializeComponent();        }        private bool isBack = false;        private string GetIP()        {            return string.Format("{0}.{1}.{2}.{3}",tbIP_1.Text,tbIP_2.Text,tbIP_3.Text,tbIP_4.Text);        }        private voID SetIP(string ip)        {            var split = ip.Split('.');            if (split.Length >= 4 && !string.IsNullOrEmpty(split[3])) tbIP_4.Text = split[3];            if (split.Length >= 3 && !string.IsNullOrEmpty(split[2])) tbIP_3.Text = split[2];            if (split.Length >= 2 && !string.IsNullOrEmpty(split[1])) tbIP_2.Text = split[1];            if (split.Length >= 1 && !string.IsNullOrEmpty(split[0])) tbIP_1.Text = split[0];        }        //处理Ctrl+V        private voID OnPrevIEwKeyDown(object sender,KeyEventArgs e)        {            if ((e.Key == Key.V) && Keyboard.ModifIErs == ModifIErKeys.Control)            {                string clipboardString = string.Empty;                if (Clipboard.ContainsText())                {                    clipboardString = Clipboard.GetText();                    if (clipboardString.IsIPv4())                    {                        this.Text = clipboardString;                        e.Handled = true;                    }                }            }        }        //键盘按键按下        private voID TextBox_OnKeyDown(object sender,KeyEventArgs e)        {            if (e.Key != Key.Tab)            {                TextBox tb = sender as TextBox;                if ((tb.name == "tbIP_1" || tb.name == "tbIP_4"))                {                    //普通IP首位末位不能为0                    if (IPMode == IPAddressMode.normal && (e.Key == Key.D0 || e.Key == Key.NumPad0))                    {                        e.Handled = true;                        return;                    }                }                if (e.Key == Key.Decimal)//小数点键                {                    e.Handled = true;                    if (tb.Text.Length != 0)                    {                        NextGetFocus(tb.name);                    }                    return;                }                else if (e.Key == Key.Back)                {                    e.Handled = true;                    isBack = true;                    if (tb.Text.Length == 0)                    {                        PrevGetFocus(tb.name);                    }                    return;                }                else if (e.Key < Key.D0 || e.Key > Key.D9 && e.Key < Key.NumPad0 || e.Key > Key.NumPad9)                {                    e.Handled = true;                    return;                }                //最多输入三位数字                if (tb.Text.Length == 3 && tb.SelectedText.Length == 0)                {                    e.Handled = true;                    return;                }            }        }        //获取到焦点        private voID TextBox_OnGotFocus(object sender,RoutedEventArgs e)        {            TextBox tb = sender as TextBox;            if (isBack)//回退事件不选中当前文本            {                if (tb.Text.Length != 0)                {                    tb.SelectionStart = tb.Text.Length;                }                return;            }            if (tb.Text.Length != 0)            {                tb.SelectAll();            }        }        //文本框文本改变        private voID TextBox_OnTextChanged(object sender,TextChangedEventArgs e)        {            TextBox tb = sender as TextBox;            string ip = this.GetIP();            if (ip == "0.0.0.0" && IPMode != IPAddressMode.Gateway)            {                tb.Text = "";                return;            }            //删除前面的0            if (tb.Text.Length == 2 && tb.Text.StartsWith("0"))            {                tb.Text = tb.Text.Remove(0,1);            }            //验证合法,大于255删除最后一位            if (tb.Text.Length == 3)            {                if (int.Parse(tb.Text) > 255)                {                    tb.Text = tb.Text.Remove(2,1);                    tb.SelectionStart = 2;                }                else                {                    NextGetFocus(tb.name);                }            }            this.SetValue(TextProperty,ip);            if (OnValueChanged != null)            {                OnValueChanged(null,null);            }        }        private voID PrevGetFocus(string tbname)        {            switch (tbname)            {                case "tbIP_4":                    tbIP_3.Focus();                    tbIP_3.SelectAll();                    break;                case "tbIP_3":                    tbIP_2.Focus();                    tbIP_2.SelectAll();                    break;                case "tbIP_2":                    tbIP_1.Focus();                    tbIP_1.SelectAll();                    break;            }        }        private voID NextGetFocus(string tbname)        {            switch (tbname)            {                case "tbIP_1":                    tbIP_2.Focus();                    tbIP_2.SelectAll();                    break;                case "tbIP_2":                    tbIP_3.Focus();                    tbIP_3.SelectAll();                    break;                case "tbIP_3":                    tbIP_4.Focus();                    tbIP_4.SelectAll();                    break;            }        }        public voID Clear()        {            this.tbIP_1.Text = "";            this.tbIP_2.Text = "";            this.tbIP_3.Text = "";            this.tbIP_4.Text = "";        }    }    public enum IPAddressMode    {        /// <summary>        /// 普通IP地址        /// </summary>        normal,/// <summary>        /// 子网掩码        /// </summary>        Mask,/// <summary>        /// 网关        /// </summary>        Gateway    }}
总结

以上是内存溢出为你收集整理的Silverlight IP地址控件全部内容,希望文章能够帮你解决Silverlight IP地址控件所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1011672.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存