silverlight2.0Beta版TextBox输入中文解决方法
程序员文章站
2022-05-05 16:03:11
新写一个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=程序集名称
不清楚怎样上传程序集!否则将程序集上传
若发此控件有问题,或是不足,请给我留言
复制代码 代码如下:
/************************************************************************/
/*
作者:覃小春
时间: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=程序集名称
不清楚怎样上传程序集!否则将程序集上传
若发此控件有问题,或是不足,请给我留言
推荐阅读
-
ubuntu安装sublime不能输入中文的解决方法
-
window系统mysql无法输入和无法显示中文的解决方法
-
Internet Explorer 8 beta 中文版与IE7共存的解决方法
-
ubuntu安装sublime不能输入中文的解决方法
-
aaalogo怎么输入中文 aaa logo中文版输入中文方法详解
-
解决3.01版的jquery.form.js中文乱码问题的解决方法
-
mysql输入中文出现ERROR 1366的解决方法
-
会声会影10中文版常见问题及解决方法详情介绍
-
Win10新预览版21343推送:此电脑、中文输入法都变样了
-
mac版steam无法设置中文怎么办 steam更新后中文设置无效解决方法