Click here to Skip to main content
16,008,010 members

sunweichao - Professional Profile



Summary

    Blog RSS
15
Authority
6
Debator
1
Enquirer
98
Organiser
425
Participant
0
Author
0
Editor
This member doesn't quite have enough reputation to be able to display their biography and homepage.

Reputation

Weekly Data. Recent events may not appear immediately. For information on Reputation please see the FAQ.

Privileges

Members need to achieve at least one of the given member levels in the given reputation categories in order to perform a given action. For example, to store personal files in your account area you will need to achieve Platinum level in either the Author or Authority category. The "If Owner" column means that owners of an item automatically have the privilege. The member types column lists member types who gain the privilege regardless of their reputation level.

ActionAuthorAuthorityDebatorEditorEnquirerOrganiserParticipantIf OwnerMember Types
Have no restrictions on voting frequencysilversilversilversilver
Bypass spam checks when posting contentsilversilversilversilversilversilvergoldSubEditor, Mentor, Protector, Editor
Store personal files in your account areaplatinumplatinumSubEditor, Editor
Have live hyperlinks in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Have the ability to include a biography in your profilebronzebronzebronzebronzebronzebronzesilverSubEditor, Protector, Editor
Edit a Question in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Edit an Answer in Q&AsilversilversilversilverYesSubEditor, Protector, Editor
Delete a Question in Q&AYesSubEditor, Protector, Editor
Delete an Answer in Q&AYesSubEditor, Protector, Editor
Report an ArticlesilversilversilversilverSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending ArticlegoldgoldgoldgoldSubEditor, Mentor, Protector, Editor
Edit other members' articlesSubEditor, Protector, Editor
Create an article without requiring moderationplatinumSubEditor, Mentor, Protector, Editor
Approve/Disapprove a pending QuestionProtector
Approve/Disapprove a pending AnswerProtector
Report a forum messagesilversilverbronzeProtector, Editor
Approve/Disapprove a pending Forum MessageProtector
Have the ability to send direct emails to members in the forumsProtector
Create a new tagsilversilversilversilver
Modify a tagsilversilversilversilver

Actions with a green tick can be performed by this member.


 
General解决路径名中不能有空格的问题 Pin
sunweichao15-Dec-10 15:13
sunweichao15-Dec-10 15:13 
GeneralIP Address Input Validation Control In WPF Pin
sunweichao13-Nov-10 14:11
sunweichao13-Nov-10 14:11 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
namespace Control.WPFControlLibrary
{
/// <summary>
/// IpControl.xaml 的交互逻辑
/// 一个IP的地址的输入控件,与系统中网络管理中的那个类似,直接托进来就可以用
/// </summary>
public partial class IpControl : UserControl
{
private string _text = "";
public IpControl()
{
InitializeComponent();
ChangeBackColor(this.textBox1.Background);
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
MaskIpAddr((System.Windows.Controls.TextBox)sender, e);

}
bool checkkey(Key key)
{
if ((key >= Key.D0 && key <= Key.D9) || (key >= Key.NumPad0 && key <= Key.NumPad9))
return true;
else
return false;
}
private void MaskIpAddr(System.Windows.Controls.TextBox textBox, KeyEventArgs e)
{
int len = textBox.Text.Length;
if (checkkey(e.Key) || e.Key == Key.Decimal || e.Key == Key.OemPeriod || e.Key == Key.Back)
{
if (e.Key != Key.Back)
{
#region 处理按下的不是点并且是长度大于3或者等于3验证,并且向下一个textBox焦点
if (len == 2 && e.Key != Key.Decimal && e.Key != Key.OemPeriod)
{
//此处这么写会有问题,因为在长按的时候不会有UP事件。
string tmp = textBox.Text + (e.Key.GetHashCode() - 34);
if (textBox.Name == "textBox1")
{
if (Int32.Parse(tmp) > 223) // 进行验证
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 223 之间的数值。");
textBox.Text = "";
textBox.Text = "223";
textBox.Focus();
textBox.Select(0, textBox.Text.Length);
e.Handled = true;
return;
}
textBox.Text = tmp;
e.Handled = true;
textBox2.Focus();
textBox2.SelectAll();
}
else if (textBox.Name == "textBox2")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "";
textBox.Text = "255";
textBox.Focus();
textBox.Select(0, textBox.Text.Length);
e.Handled = true;
return;
}
textBox.Text = tmp;
e.Handled = true;
textBox3.Focus();
textBox3.SelectAll();
}
else if (textBox.Name == "textBox3")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "";
textBox.Text = "255";
textBox.Focus();
textBox.Select(0, textBox.Text.Length);
e.Handled = true;
return;
}
textBox.Text = tmp;
e.Handled = true;
textBox4.Focus();
textBox4.SelectAll();
}
else if (textBox.Name == "textBox4")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "";
textBox.Text = "255";
textBox.Select(0, textBox.Text.Length);
textBox.Focus();
e.Handled = true;
return;
}
//textBox.Text = tmp;
//textBox4.Focus();
//textBox4.SelectAll();
}
}
#endregion
#region 处理是点的情况 true
if (e.Key == Key.Decimal || e.Key == Key.OemPeriod)
{
if (textBox.Name == "textBox1" && textBox.Text != "")
{
textBox2.Focus();
textBox2.SelectAll();
}
if (textBox.Name == "textBox2" && textBox.Text != "")
{
textBox3.Focus();
textBox3.SelectAll();
}
if (textBox.Name == "textBox3" && textBox.Text != "")
{
textBox4.Focus();
textBox4.SelectAll();
}
if (textBox.Name == "textBox4" && textBox.Text != "")
{
}
e.Handled = true;
}
#endregion
}
else //
{
if (textBox.Name == "textBox1" && textBox.Text == "")
{
}
if (textBox.Name == "textBox2" && textBox.Text == "")
{
textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length;
}
if (textBox.Name == "textBox3" && textBox.Text == "")
{
textBox2.Focus();
textBox2.SelectionStart = textBox2.Text.Length;
}
if (textBox.Name == "textBox4" && textBox.Text == "")
{
textBox3.Focus();
textBox3.SelectionStart = textBox3.Text.Length;
}
e.Handled = false;
}
}
else
{
//if (ss.Length > 0)
//{
// textBox.Text = ss.Substring(0,(ss.Length-1));
//}
e.Handled = true;
}
}
//[Browsable(true)]
// 获取 IpBox 的文本。
public string Text
{
get
{
if (textBox1.Text == ""
|| textBox2.Text == ""
|| textBox3.Text == ""
|| textBox4.Text == "")
{
_text = "";
return _text;
}
else
{
_text = Convert.ToInt32(textBox1.Text).ToString() + "." + Convert.ToInt32(textBox2.Text).ToString() + "." + Convert.ToInt32(textBox3.Text).ToString() + "." + Convert.ToInt32(textBox4.Text).ToString();
return _text;
}
}
set //
{
if(value != null)
{
// 255-255-255-255
//string pattern = @"^([1-9]|\d{2}|1[0-9]{1,2}|2[0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
// 223-255-255-255
string pattern = @"^([1-9]|\d{2}|1[0-9]{1,2}|2[1-2][1-3])\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
if( !Regex.IsMatch(value , pattern) )
{
_text = "";
throw new Exception("IP 地址格式错误!");
}
else
{
string[] ipnum = value.Split('.');
textBox1.Text = ipnum[0];
textBox2.Text = ipnum[1];
textBox3.Text = ipnum[2];
textBox4.Text = ipnum[3];
}
}
_text = value;
}

}
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
if (sender.GetType() == typeof(TextBox))
ChangeBackColor(((TextBox)sender).Background);
else
ChangeBackColor(((Label)sender).Background);
}
private void ChangeBackColor(Brush tb)
{
this.dockPanel1.Background = tb;
this.label1.Background = tb;
this.label2.Background = tb;
this.label3.Background = tb;
this.textBox1.Background = tb;
this.textBox2.Background = tb;
this.textBox3.Background = tb;
this.textBox4.Background = tb;
}
private void textBox1_MouseEnter(object sender, MouseEventArgs e)
{
if (sender.GetType()==typeof(TextBox))
ChangeBackColor(((TextBox)sender).Background);
else
ChangeBackColor(((Label)sender).Background);
}
private void textBox1_GotFocus(object sender, MouseButtonEventArgs e)
{
}

}
}


XAML
<UserControl x:Class="Control.WPFControlLibrary.IpControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="23" Width="125">
<UserControl.Resources>
<Style x:Key="LableStyle" TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<!--设置样式 -->
<ControlTemplate TargetType="{x:Type Label}">
<Grid>
<Rectangle x:Name="Rectangle" Stroke="#FFFFFFFF" StrokeMiterLimit="1.000000" StrokeThickness="0.500000" RadiusX="10" RadiusY="10" Fill="#FF777777">
</Rectangle>
<ContentPresenter x:Name="ContentPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Grid>

</ControlTemplate>
</Setter.Value>
</Setter>
</Style>


</UserControl.Resources>
<Border BorderThickness="1" BorderBrush="LightBlue" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23">
<DockPanel Name="dockPanel1" Height="23" VerticalAlignment="Top">
<TextBox Name="textBox1" MaxLength="3" MouseEnter ="textBox1_MouseEnter" GotFocus="textBox1_GotFocus" KeyDown="textBox_KeyUp" Width="28" Height="23" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center" />
<Label Height="4" MouseEnter ="textBox1_MouseEnter" Name="label1" Style="{StaticResource LableStyle}" Width="4" Margin="0,0,0,0" VerticalContentAlignment="Center">.</Label>
<TextBox MouseEnter ="textBox1_GotFocus" MaxLength="3" Height="23" Name="textBox2" KeyDown="textBox_KeyUp" Width="28" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center"/>
<Label Height="4" Name="label2" MouseEnter ="textBox1_MouseEnter" Style="{StaticResource LableStyle}" Width="4" Background="Transparent" VerticalContentAlignment="Center">.</Label>
<TextBox MouseEnter ="textBox1_GotFocus" MaxLength="3" Height="23" Name="textBox3" KeyDown="textBox_KeyUp" Width="28" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center"/>
<Label Height="4" Name="label3" GotMouseCapture="textBox1_MouseEnter" Style="{StaticResource LableStyle}" Width="4" Background="Transparent" VerticalContentAlignment="Top">.</Label>
<TextBox MouseEnter ="textBox1_GotFocus" MaxLength="3" GotMouseCapture="textBox1_MouseEnter" Height="23" Name="textBox4" KeyDown="textBox_KeyUp" Width="28" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center"/>
</DockPanel>
</Border>
</UserControl>

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.