积极答复者
TextBox如何兼容接收中英文输入?

问题
-
各位老师,
向TextBox(包括WPFToolkit AutoCompleteBox)输入中文时,会多次引发该控件的Text_Changed事件。造成中文输入处理困难。我从https://blog.csdn.net/qq_37214567/article/details/79738974网文中发现了这段代码:
private void SearchTextBox_Populating(object sender, PopulatingEventArgs e)
{
e.Cancel = true;
//获取输入的值
var inputText = searchTextBox.Text;
Task.Run(() =>
{
string text = inputText;
//判断输入是否是中文(根据自己的需求)
for (int i = 0; i < text.Length; i++)
{
if ((int)text[i] > 127)
continue;
else
return;
}
//使用Ui线程的Dispatcher 操作控件
this.Dispatcher.BeginInvoke(new Action(() =>
{
//开始匹配
this.searchTextBox.PopulateComplete();
}), DispatcherPriority.SystemIdle, null);
});
}
可以正确接收中文输入了。但却限制了英文(半角)输入,好像一律屏蔽了(else分支的return;)。请老师们帮忙看看,能不能将这段代码改造成可以兼容接收中英文输入。
谢谢了。
ly_he
答案
-
如果问题已经解决,请将有帮助的回复标记为答案
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已标记为答案 ly_he 2019年9月23日 13:15
全部回复
-
你好
你想判断你当前输入字符是汉字还是英文?我看你的代码都是在用整个文本框字符串在做判断,你可以这样修改代码
int i = 0; private void searchTextBox_TextChanged(object sender, EventArgs e) { var inputText = searchTextBox.Text; string text = inputText; if ((int)text[i] > 127) MessageBox.Show("汉字"); else MessageBox.Show("英文"); i += 1; }
Best Regards,
Alex
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
-
谢谢两位老师回复。
问题已解决:把这段循环去掉,即可兼容中英文字符输入。
ly_he
- 已建议为答案 Yong LuMicrosoft contingent staff, Moderator 2019年9月23日 1:15
-
如果问题已经解决,请将有帮助的回复标记为答案
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
- 已标记为答案 ly_he 2019年9月23日 13:15