Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 语言更改后立即访问CurrentInputLanguage属性时不会更新_C#_Multithreading_Input Language - Fatal编程技术网

C# 语言更改后立即访问CurrentInputLanguage属性时不会更新

C# 语言更改后立即访问CurrentInputLanguage属性时不会更新,c#,multithreading,input-language,C#,Multithreading,Input Language,请查看下面的代码,以便更好地理解我的确切意思: using System; using System.Windows.Forms; namespace CurrentInputLanguageTest { static class Program { [STAThread] static void Main() { Console.WriteLine(InputLanguage.CurrentInputLa

请查看下面的代码,以便更好地理解我的确切意思:

using System;
using System.Windows.Forms;

namespace CurrentInputLanguageTest
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's US
            Console.ReadLine(); // Changed my keyboard layout while typing something
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's still US

            var form = new Form();
            var button = new Button();
            button.Click += CheckInputLanguage;
            form.Controls.Add(button);
            Application.Run(form);
        }

        static void CheckInputLanguage(object sender, EventArgs e)
        {
            // I have changed my input language while the form is opened and pressed the button.
            // It changes when called in this event handler.
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); 
        }
    }
}

问题:我的应用程序中有一些事件处理程序,我需要知道触发事件时当前的输入语言是什么。如何执行此操作?

它总是返回我们的原因是因为您在命令中,并且只有该输入行的设置发生了更改

如果用户在显示表单时更改输入语言,则正确返回当前语言。但您也可以通过另一个按钮以编程方式设置is:

using System;
using System.Windows.Forms;

namespace CurrentInputLanguageTest
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's US
            Console.ReadLine(); // Changed my keyboard layout while typing something
            Console.WriteLine(Application.CurrentInputLanguage.LayoutName); // It's still US

            var form = new Form();
            var grid = new TableLayoutPanel();
            var button = new Button();
            var button2 = new Button();
            button2.Left = button.Right + 5;
            form.Refresh();
            button.Click += CheckInputLanguage;
            button2.Click += ChangeInputLanguage;
            form.Controls.Add(button);
            form.Controls.Add(button2);
            Application.Run(form);
        }

        private static void ChangeInputLanguage(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
        }

        static void CheckInputLanguage(object sender, EventArgs e)
        {
            // I have changed my input language while the form is opened and pressed the button.
            // It changes when called in this event handler.
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName);
        }
    }
}

它总是返回我们的原因是因为您在命令中,并且只有该输入行的设置更改

如果用户在显示表单时更改输入语言,则正确返回当前语言。但您也可以通过另一个按钮以编程方式设置is:

using System;
using System.Windows.Forms;

namespace CurrentInputLanguageTest
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName); // It's US
            Console.ReadLine(); // Changed my keyboard layout while typing something
            Console.WriteLine(Application.CurrentInputLanguage.LayoutName); // It's still US

            var form = new Form();
            var grid = new TableLayoutPanel();
            var button = new Button();
            var button2 = new Button();
            button2.Left = button.Right + 5;
            form.Refresh();
            button.Click += CheckInputLanguage;
            button2.Click += ChangeInputLanguage;
            form.Controls.Add(button);
            form.Controls.Add(button2);
            Application.Run(form);
        }

        private static void ChangeInputLanguage(object sender, EventArgs e)
        {
            InputLanguage.CurrentInputLanguage = InputLanguage.FromCulture(System.Globalization.CultureInfo.GetCultureInfo("en-US"));
        }

        static void CheckInputLanguage(object sender, EventArgs e)
        {
            // I have changed my input language while the form is opened and pressed the button.
            // It changes when called in this event handler.
            Console.WriteLine(InputLanguage.CurrentInputLanguage.LayoutName);
        }
    }
}