富贵资源网 Design By www.hznty.com
新写一个TextBoxEx控件,继承于TextBox,并对TextBox的选择事件及字符改变事件做处理,以下是原代码
复制代码 代码如下:
/************************************************************************/
/*
作者:覃小春
时间:20080826
说明:解决silverlightBeta2中TextBox中文输入问题
* blog:blog.csdn.net/colijian
*/
/************************************************************************/
using System.Windows;
using System.Windows.Controls;
namespace TextBoxEx
{
public class TextBoxEx:TextBox
{
#region 属性
private string _OldText = "";
private int _RecSelectStart = 0;
private int _RecSelectLength = 0;
#endregion
public TextBoxEx()
{
TextChanged += new TextChangedEventHandler(TextBoxEx_TextChanged);
SelectionChanged += new RoutedEventHandler(TextBoxEx_SelectionChanged);
}
void TextBoxEx_SelectionChanged(object sender, RoutedEventArgs e)
{
TextBox _sender = sender as TextBox;
if (_sender == null)
return;
if (_sender.SelectionLength > 0)
{
//recode user select position
_RecSelectLength = _sender.SelectionLength;
_RecSelectStart = _sender.SelectionStart;
}
else
{
_RecSelectLength = 0;
}
}
void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox _sender = sender as TextBox;
if (_sender == null)
return;
string textIfnor = _sender.Text;
#region 除去先中部份
if (_RecSelectLength != 0)
{
_OldText = _OldText.Substring(0, _RecSelectStart) + _OldText.Substring(_RecSelectStart + _RecSelectLength, _OldText.Length - _RecSelectStart - _RecSelectLength);
_RecSelectLength = 0;
}
#endregion
int LengthAdd = textIfnor.Length - _OldText.Length;
if (LengthAdd <= 0)
{
_OldText = _sender.Text;
//这种情况是删除数据
return;
}
else if (LengthAdd % 2 == 0)
{
//如果当前是成双的情况下
//得到当前字符串
string AddInfor = textIfnor.Substring(_sender.SelectionStart - LengthAdd, LengthAdd);
if (!AddInfor.Substring(0, AddInfor.Length / 2).Equals(AddInfor.Substring(AddInfor.Length / 2)))
{
_OldText = _sender.Text;
return;
}
//得到实际新增值
AddInfor = AddInfor.Substring(0, AddInfor.Length / 2);
//得到实际理论值
string DealText = textIfnor.Substring(0, _sender.SelectionStart - LengthAdd) + AddInfor + textIfnor.Substring(_sender.SelectionStart, textIfnor.Length - _sender.SelectionStart);
int RecodeSelectSTart = _sender.SelectionStart - LengthAdd / 2;
_sender.SelectionStart = 0;
_sender.Text = DealText;
_sender.SelectionStart = RecodeSelectSTart;
_OldText = DealText;
}
else
{
_OldText = _sender.Text;
}
}
}
}
使用:
复制代码 代码如下:
<UserControl x:Class="MutilTextBox.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CT="clr-namespace:TextBoxEx;assembly=TextBoxEx"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<TextBox x:Name="FirstTextBox" Text="first" Grid.Row="0" TextChanged="FirstTextBox_TextChanged"></TextBox>
<CT:TextBoxEx x:Name="SecondTextBox" Grid.Row="1"></CT:TextBoxEx>
<CT:TextBoxEx x:Name="ThreeTextBox" Grid.Row="2"></CT:TextBoxEx>
<TextBox x:Name="Four" Grid.Row="3" ></TextBox>
</Grid>
</UserControl>
注意:要先加入名称空间,具体的值是:
clr-namespace:名称空间全名;assembly=程序集名称
不清楚怎样上传程序集!否则将程序集上传
若发此控件有问题,或是不足,请给我留言
复制代码 代码如下:
/************************************************************************/
/*
作者:覃小春
时间:20080826
说明:解决silverlightBeta2中TextBox中文输入问题
* blog:blog.csdn.net/colijian
*/
/************************************************************************/
using System.Windows;
using System.Windows.Controls;
namespace TextBoxEx
{
public class TextBoxEx:TextBox
{
#region 属性
private string _OldText = "";
private int _RecSelectStart = 0;
private int _RecSelectLength = 0;
#endregion
public TextBoxEx()
{
TextChanged += new TextChangedEventHandler(TextBoxEx_TextChanged);
SelectionChanged += new RoutedEventHandler(TextBoxEx_SelectionChanged);
}
void TextBoxEx_SelectionChanged(object sender, RoutedEventArgs e)
{
TextBox _sender = sender as TextBox;
if (_sender == null)
return;
if (_sender.SelectionLength > 0)
{
//recode user select position
_RecSelectLength = _sender.SelectionLength;
_RecSelectStart = _sender.SelectionStart;
}
else
{
_RecSelectLength = 0;
}
}
void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox _sender = sender as TextBox;
if (_sender == null)
return;
string textIfnor = _sender.Text;
#region 除去先中部份
if (_RecSelectLength != 0)
{
_OldText = _OldText.Substring(0, _RecSelectStart) + _OldText.Substring(_RecSelectStart + _RecSelectLength, _OldText.Length - _RecSelectStart - _RecSelectLength);
_RecSelectLength = 0;
}
#endregion
int LengthAdd = textIfnor.Length - _OldText.Length;
if (LengthAdd <= 0)
{
_OldText = _sender.Text;
//这种情况是删除数据
return;
}
else if (LengthAdd % 2 == 0)
{
//如果当前是成双的情况下
//得到当前字符串
string AddInfor = textIfnor.Substring(_sender.SelectionStart - LengthAdd, LengthAdd);
if (!AddInfor.Substring(0, AddInfor.Length / 2).Equals(AddInfor.Substring(AddInfor.Length / 2)))
{
_OldText = _sender.Text;
return;
}
//得到实际新增值
AddInfor = AddInfor.Substring(0, AddInfor.Length / 2);
//得到实际理论值
string DealText = textIfnor.Substring(0, _sender.SelectionStart - LengthAdd) + AddInfor + textIfnor.Substring(_sender.SelectionStart, textIfnor.Length - _sender.SelectionStart);
int RecodeSelectSTart = _sender.SelectionStart - LengthAdd / 2;
_sender.SelectionStart = 0;
_sender.Text = DealText;
_sender.SelectionStart = RecodeSelectSTart;
_OldText = DealText;
}
else
{
_OldText = _sender.Text;
}
}
}
}
使用:
复制代码 代码如下:
<UserControl x:Class="MutilTextBox.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:CT="clr-namespace:TextBoxEx;assembly=TextBoxEx"
Width="400" Height="300">
<Grid x:Name="LayoutRoot" Background="White">
<Grid.RowDefinitions>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
<RowDefinition Height="50"></RowDefinition>
</Grid.RowDefinitions>
<TextBox x:Name="FirstTextBox" Text="first" Grid.Row="0" TextChanged="FirstTextBox_TextChanged"></TextBox>
<CT:TextBoxEx x:Name="SecondTextBox" Grid.Row="1"></CT:TextBoxEx>
<CT:TextBoxEx x:Name="ThreeTextBox" Grid.Row="2"></CT:TextBoxEx>
<TextBox x:Name="Four" Grid.Row="3" ></TextBox>
</Grid>
</UserControl>
注意:要先加入名称空间,具体的值是:
clr-namespace:名称空间全名;assembly=程序集名称
不清楚怎样上传程序集!否则将程序集上传
若发此控件有问题,或是不足,请给我留言
富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?