Write a new TextBoxEx control that inherits from TextBox and processes the selection event and character change event of TextBox. The following is the original code.
Copy codeThe Code is as follows: /*************************************** *********************************/
/*
Author: Xiao xiaochun
Time: 20080826
Note: The problem of TextBox Chinese Input in silverlightBeta2 is solved.
* Blog: blog.csdn.net/colijian
*/
/*************************************** *********************************/
Using System. Windows;
Using System. Windows. Controls;
Namespace TextBoxEx
{
Public class TextBoxEx: TextBox
{
# Region attributes
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 remove the central portion
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;
// In this case, the data is deleted.
Return;
}
Else if (LengthAdd % 2 = 0)
{
// If the current double
// Obtain the current string
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;
}
// Get new value
AddInfor = AddInfor. Substring (0, AddInfor. Length/2 );
// Obtain the actual theoretical value
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;
}
}
}
}
Usage:Copy codeThe Code is as follows: <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>
Note: you must first add a namespace. The specific value is:
Clr-namespace: namespace full name; assembly = assembly name
I don't know how to upload the assembly! Otherwise, upload the assembly.
If the control is faulty or is insufficient, please leave a message for me