自定义TextBox控件自动切换键盘语言c#WPF [英] Custom TextBox Control that Switches Keyboard Language Automatically c# WPF

查看:386
本文介绍了自定义TextBox控件自动切换键盘语言c#WPF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这样的东西
http://www.codeproject.com/Articles/301832/Custom-Text-box-Control-that-Switch-keyboard-langu
但是对于 WPF strong>和C#

i need to make something like that http://www.codeproject.com/Articles/301832/Custom-Text-box-Control-that-Switch-keyboard-langu but for WPF and C#

我试图用一个简单的 if 语句,但我必须放另一个文本框, p>

i'v tried to do it with a simple if statement but i was have to put another textbox like that

    private void textBox2_TextChanged(object sender, TextChangedEventArgs e)
    {
        textBox1.Focus();
        if (textBox1.Text == "Q" || textBox1.Text == "q")
        {
            textBox2.Text = textBox2.Text+ "ض";
            textBox1.Text = "";
        }
        else if (textBox1.Text == "W" || textBox1.Text == "w")
        {
            textBox2.Text = textBox2.Text + "ص";
            textBox1.Text = "";
        }
        // and so ..
    }



工作,但我想做一些像上面的链接

it works but i want to do something like the link above

推荐答案

你可以通过创建一个新的自定义控件继承文本框。在创建一个新的TextLanguage属性和覆盖OnKeyDown方法

You can do that in WPF by creating a new custom Control that inherits TextBox. In that Create a new TextLanguage Property and Override the OnKeyDown method

namespace WpfApplication
{
    public enum TextLanguage
    {
        English,
        Arabic
    }

    public class CustomTextBox : TextBox
    {        
        public TextLanguage TextLanguage { get; set; }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            if (TextLanguage != WpfApplication.TextLanguage.English)
            {
                e.Handled = true;
                if (Keyboard.Modifiers == ModifierKeys.Shift)
                {
                    // Shift key is down
                    switch (e.Key)
                    {
                        case Key.Q:
                            AddChars("ص");
                            break;
                        // Handle Other Cases too
                        default:
                            e.Handled = false;
                            break;
                    }
                }
                else if (Keyboard.Modifiers == ModifierKeys.None)
                {
                    switch (e.Key)
                    {
                        case Key.Q:
                            AddChars("ض");
                            break;
                        // Handle Other Cases too
                        default:
                            e.Handled = false;
                            break;
                    }
                }
            }
            base.OnKeyDown(e);
        }

        void AddChars(string str)
        {
            if (SelectedText.Length == 0)
                AppendText(str);
            else
                SelectedText = str;

            this.SelectionLength = 0;
            this.CaretIndex = Text.Length;

        }
    }
}

这篇关于自定义TextBox控件自动切换键盘语言c#WPF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆