Silverlight2.0beta version TextBox Input Chinese solution _ Practical Tips

Source: Internet
Author: User
Tags xmlns
Write a new Textboxex control that inherits from the TextBox and handles the selection events and character change events for the textbox, following the original code
Copy Code code as follows:

/************************************************************************/
/*
Author: Chung
Time: 20080826
Description: Solve the problem of Chinese input in textbox in SilverlightBeta2
* Blog:blog.csdn.net/colijian
*/
/************************************************************************/
Using System.Windows;
Using System.Windows.Controls;
Namespace Textboxex
{
public class Textboxex:textbox
{
#region Properties
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 get rid of the middle part first.
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;
This situation is to delete the data
Return
}
else if (lengthadd% 2 = 0)
{
If the current is in a double case
Get 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 the actual new value
addinfor = addinfor.substring (0, ADDINFOR.LENGTH/2);
Get 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;
}
}
}
}

Use:
Copy Code code 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= "height=" >
<grid x:name= "LayoutRoot" background= "White" >
<Grid.RowDefinitions>
<rowdefinition height= "></RowDefinition>"
<rowdefinition height= "></RowDefinition>"
<rowdefinition height= "></RowDefinition>"
<rowdefinition height= "></RowDefinition>"
</Grid.RowDefinitions>
<textbox x:name= "Firsttextbox" text= "a" 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: To add a namespace first, the value is:
Clr-namespace: namespace full name; assembly= assembly name
Don't know how to upload an assembly! Otherwise upload the assembly
If there is a problem with this control, or not enough, please leave me a message

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.