I encountered a problem today. The Textbox Control of Silverlight will repeat when entering Chinese characters. I checked the information online and found it a bug. There is a solution function on the Internet, which doesn't feel very comfortable to use, so I wrote one by myself, basically solving the problem and having bugs, and I was unable to adapt to the pasted data. The function is as follows:
String mlasttext = NULL;
Private void textbox_textchanged (Object sender, textchangedeventargs E)
{
Textbox TXT = sender as textbox;
If (mlasttext = NULL)
Mlasttext = "";
// Length difference
Int num = (txt. Text. Length-mlasttext. Length );
// It is not a question of Chinese Input
If (Num <= 0 | num % 2! = 0)
{
Mlasttext = TXT. text;
Return;
}
Int start = TXT. selectionstart;
// Neither
If (START-num <0)
{
Mlasttext = TXT. text;
Return;
}
String tmpstr = TXT. Text. Remove (START-Num, num );
// Not yet
If (! Tmpstr. Equals (mlasttext ))
{
Mlasttext = TXT. text;
Return;
}
Tmpstr = TXT. Text. substring (START-Num, num );
// Still not
If (tmpstr. substring (0, num/2 )! = Tmpstr. substring (Num/2 ))
{
Mlasttext = TXT. text;
Return;
}
TXT. textchanged-= new textchangedeventhandler (this. textbox_textchanged );
TXT. textchanged + = new textchangedeventhandler (this. textbox_textchanged_non );
TXT. Text = TXT. Text. Remove (txt. selectionstart-num/2, num/2 );
TXT. selectionstart = start-num/2;
Mlasttext = TXT. text;
}
Private void textbox_textchanged_non (Object sender, textchangedeventargs E)
{
Textbox TXT = sender as textbox;
TXT. textchanged-= new textchangedeventhandler (this. textbox_textchanged_non );
TXT. textchanged + = new textchangedeventhandler (this. textbox_textchanged );
}
You can assign the first event to textbox. However, there are still some shortcomings. We hope Microsoft can quickly release the official version.