欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

silverlight2.0Beta版TextBox输入中文解决方法

程序员文章站 2022-09-06 21:34:04
新写一个textboxex控件,继承于textbox,并对textbox的选择事件及字符改变事件做处理,以下是原代码 复制代码 代码如下:/****************...
新写一个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=程序集名称
不清楚怎样上传程序集!否则将程序集上传
若发此控件有问题,或是不足,请给我留言