silverlight2.0Beta版TextBox輸入中文解決方法
更新時(shí)間:2008年10月13日 23:41:58 作者:
silverlight Beta 2.0 中TetBox輸入漢字,除MS自己的輸入法,其它所有輸入法都會(huì)出現(xiàn)輸入的東西會(huì)在TextBox中重復(fù)一次的現(xiàn)像,google ,Baidu了一下,大家說(shuō)好像是silverlight自己的一個(gè)BUG,可能會(huì)在Repleass的時(shí)候修改。
新寫一個(gè)TextBoxEx控件,繼承于TextBox,并對(duì)TextBox的選擇事件及字符改變事件做處理,以下是原代碼
/************************************************************************/
/*
作者:覃小春
時(shí)間:20080826
說(shuō)明:解決silverlightBeta2中TextBox中文輸入問(wèn)題
* 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;
//這種情況是刪除數(shù)據(jù)
return;
}
else if (LengthAdd % 2 == 0)
{
//如果當(dāng)前是成雙的情況下
//得到當(dāng)前字符串
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;
}
//得到實(shí)際新增值
AddInfor = AddInfor.Substring(0, AddInfor.Length / 2);
//得到實(shí)際理論值
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=程序集名稱
不清楚怎樣上傳程序集!否則將程序集上傳
若發(fā)此控件有問(wèn)題,或是不足,請(qǐng)給我留言
復(fù)制代碼 代碼如下:
/************************************************************************/
/*
作者:覃小春
時(shí)間:20080826
說(shuō)明:解決silverlightBeta2中TextBox中文輸入問(wèn)題
* 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;
//這種情況是刪除數(shù)據(jù)
return;
}
else if (LengthAdd % 2 == 0)
{
//如果當(dāng)前是成雙的情況下
//得到當(dāng)前字符串
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;
}
//得到實(shí)際新增值
AddInfor = AddInfor.Substring(0, AddInfor.Length / 2);
//得到實(shí)際理論值
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;
}
}
}
}
使用:
復(fù)制代碼 代碼如下:
<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=程序集名稱
不清楚怎樣上傳程序集!否則將程序集上傳
若發(fā)此控件有問(wèn)題,或是不足,請(qǐng)給我留言
相關(guān)文章
獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之二(downmoon原創(chuàng))
獲取遠(yuǎn)程網(wǎng)頁(yè)的內(nèi)容之二(downmoon原創(chuàng))...2007-03-03Asp.Mvc?2.0實(shí)現(xiàn)用戶注冊(cè)實(shí)例講解(1)
這篇文章主要介紹了Asp.Mvc?2.0如何實(shí)現(xiàn)用戶注冊(cè),實(shí)例講解很細(xì)致,注冊(cè)功能是每個(gè)網(wǎng)站必不可少的組成部分,感興趣的的朋友可以參考下2015-08-08iis的http 500內(nèi)部服務(wù)器錯(cuò)誤的解決
iis的http 500內(nèi)部服務(wù)器錯(cuò)誤是我們經(jīng)常碰到的錯(cuò)誤之一,它的主要錯(cuò)誤表現(xiàn)就是asp程序不能瀏覽但htm靜態(tài)網(wǎng)頁(yè)不受影響。另外當(dāng)錯(cuò)誤發(fā)生時(shí),系統(tǒng)事件日志和安全事件日志都會(huì)有相應(yīng)的記錄2007-04-04詳解.NET Core 使用HttpClient SSL請(qǐng)求出錯(cuò)的解決辦法
這篇文章主要介紹了.NET Core 使用HttpClient SSL請(qǐng)求出錯(cuò)的解決辦法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-03-03ASP.Net Core3.0中使用JWT認(rèn)證的實(shí)現(xiàn)
這篇文章主要介紹了ASP.Net Core3.0中使用JWT認(rèn)證的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01asp.net中GridView和DataGrid相同列合并實(shí)現(xiàn)代碼
asp.net中GridView和DataGrid相同列合并實(shí)現(xiàn)代碼,需要的朋友可以參考下2012-10-10