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

问题描述

silverlight Beta 2.0 中TetBox输入汉字,除MS自己的输入法,其它所有输入法都会出现输入的东西会在TextBox中重复一次的现像,google ,Baidu了一下,大家说好像是silverlight自己的一个BUG,可能会在Repleass的时候修改。

 

解决办法:

       新写一个TextBoxEx控件,继承于TextBox,并对TextBox的选择事件及字符改变事件做处理,以下是原代码

  1. /************************************************************************/
  2. /*
  3. 作者:覃小春
  4. 时间:20080826
  5. 说明:解决silverlightBeta2中TextBox中文输入问题
  6.  * blog:blog.youkuaiyun.com/colijian
  7. */
  8. /************************************************************************/
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. namespace TextBoxEx
  12. {
  13.     public class TextBoxEx:TextBox
  14.     {
  15. #region  属性
  16.         private string _OldText = "";
  17.         private int _RecSelectStart = 0;
  18.         private int _RecSelectLength = 0;
  19. #endregion
  20.         public TextBoxEx()
  21.         {
  22.             TextChanged += new TextChangedEventHandler(TextBoxEx_TextChanged);
  23.             SelectionChanged += new RoutedEventHandler(TextBoxEx_SelectionChanged);
  24.         }
  25.         void TextBoxEx_SelectionChanged(object sender, RoutedEventArgs e)
  26.         {
  27.             TextBox _sender = sender as TextBox;
  28.             if (_sender == null)
  29.                 return;
  30.            if (_sender.SelectionLength > 0)
  31.             {
  32.                //recode user select position
  33.                 _RecSelectLength = _sender.SelectionLength;
  34.                 _RecSelectStart = _sender.SelectionStart;
  35.             }
  36.            else 
  37.            {
  38.                _RecSelectLength = 0;
  39.            }
  40.         }
  41.         void TextBoxEx_TextChanged(object sender, TextChangedEventArgs e)
  42.         {
  43.             TextBox _sender = sender as TextBox;
  44.             if (_sender == null)
  45.                 return;
  46.             string textIfnor = _sender.Text;
  47.             #region 除去先中部份
  48.             if (_RecSelectLength != 0)
  49.             {
  50.                 _OldText = _OldText.Substring(0, _RecSelectStart) + _OldText.Substring(_RecSelectStart + _RecSelectLength, _OldText.Length - _RecSelectStart - _RecSelectLength);
  51.                 _RecSelectLength = 0;
  52.             }
  53.             #endregion
  54.             int LengthAdd = textIfnor.Length - _OldText.Length;
  55.             if (LengthAdd <= 0)
  56.             {
  57.                 _OldText = _sender.Text;
  58.                 //这种情况是删除数据
  59.                 return;
  60.             }
  61.             else if (LengthAdd % 2 == 0)
  62.             {
  63.                 //如果当前是成双的情况下
  64.                 //得到当前字符串
  65.                 string AddInfor = textIfnor.Substring(_sender.SelectionStart - LengthAdd, LengthAdd);
  66.                 if (!AddInfor.Substring(0, AddInfor.Length / 2).Equals(AddInfor.Substring(AddInfor.Length / 2)))
  67.                 {
  68.                     _OldText = _sender.Text;
  69.                     return;
  70.                 }
  71.                     //得到实际新增值
  72.                 AddInfor = AddInfor.Substring(0, AddInfor.Length / 2);
  73.                 //得到实际理论值
  74.                 string DealText = textIfnor.Substring(0, _sender.SelectionStart - LengthAdd) + AddInfor + textIfnor.Substring(_sender.SelectionStart, textIfnor.Length - _sender.SelectionStart);
  75.                 int RecodeSelectSTart = _sender.SelectionStart - LengthAdd / 2;
  76.                 _sender.SelectionStart = 0;
  77.                 _sender.Text = DealText;
  78.                 _sender.SelectionStart = RecodeSelectSTart;
  79.                 _OldText = DealText;
  80.             }
  81.             else 
  82.             {
  83.                 _OldText = _sender.Text;
  84.             }
  85.         }
  86.     }
  87. }

使用:

 

 

  1. <UserControl x:Class="MutilTextBox.Page"
  2.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  3.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  4.              xmlns:CT="clr-namespace:TextBoxEx;assembly=TextBoxEx"
  5.     Width="400" Height="300">
  6.     <Grid x:Name="LayoutRoot" Background="White">
  7.         <Grid.RowDefinitions>
  8.             <RowDefinition Height="50"></RowDefinition>
  9.             <RowDefinition Height="50"></RowDefinition>
  10.             <RowDefinition Height="50"></RowDefinition>
  11.             <RowDefinition Height="50"></RowDefinition>
  12.         </Grid.RowDefinitions>
  13.         <TextBox x:Name="FirstTextBox" Text="first" Grid.Row="0" TextChanged="FirstTextBox_TextChanged"></TextBox>
  14.         <CT:TextBoxEx x:Name="SecondTextBox" Grid.Row="1"></CT:TextBoxEx>
  15.         <CT:TextBoxEx x:Name="ThreeTextBox" Grid.Row="2"></CT:TextBoxEx>
  16.         <TextBox x:Name="Four" Grid.Row="3" ></TextBox>
  17.     </Grid>
  18. </UserControl>

    

注意:要先加入名称空间,具体的值是:

clr-namespace:名称空间全名;assembly=程序集名称

 

不清楚怎样上传程序集!否则将程序集上传

 

若发此控件有问题,或是不足,请给我留言

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值