微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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.csdn.net/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=程序集名称

 

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

 

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐