程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> ASP.NET >> ASP.NET基礎 >> silverlight2.0Beta版TextBox輸入中文解決方法

silverlight2.0Beta版TextBox輸入中文解決方法

編輯:ASP.NET基礎
新寫一個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=程序集名稱
不清楚怎樣上傳程序集!否則將程序集上傳
若發此控件有問題,或是不足,請給我留言
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved