To control the input method in a C # application

Source: Internet
Author: User
Tags object net thread access
Program | control in the Windows system generally installed at least three input methods, in the input data often switch input method, although the Windows system provides a switch shortcut, but the input work or brought a lot of trouble. If the user is provided with an intelligent input method for automatic switching in the application, such applications will become more professional and competitive. I do not know you can access, in the form of data entry when Access automatically switch input method, it is cool, now you can also achieve this. If you also want to cool your program, please continue ...

In order to control the input method,. NET class Library provides support in the System.Windows.Forms.InputLanguage class. I plan to take a moment to talk about the functionality of the InputLanguage class, and then give an example of Inputlanguagerichedit.

   1,The InputLanguage class is a sealed class that provides a number of methods and properties to implement input method management, including several attributes that are particularly important, and I'll explain each of the following, and if you want to fully understand all the methods and properties of the class, browse MSDN.

public static inputlanguage Currentinputlanguage {get; set;}
Gets or sets the input method for the current thread.

public static inputlanguage Defaultinputlanguage {get;}
Get the default input method.

public static inputlanguagecollection Installedinputlanguages{get;}
Get the system input method set. This container object allows you to enumerate the list of input methods currently installed by the system.

public string LayoutName {get;}
Obtain the registration name of the input method in the system tray.

......

2,We have already studied several important attributes provided by the InputLanguage class and can now begin to apply InputLanguage classes in application development. I want to create a system program for the. NET Window form, enumerate all the input methods installed by the current system with a list box, and automatically change the input method of the current thread by changing the option of the list box. It also implements the option of changing the list box according to the changes in the input method in the desktop tray.

(1), New project--> select Visual C # project--> Enter the project name: Inputlanguagerichedit.

(2), in the Toolbox, drag a RichTextBox control, named: RichTextBox1; a combobox control named: ComboBox1; a button control named: But_exit.

(3) Replace the private void InitializeComponent () with the following code.


{
This.combobox1 = new System.Windows.Forms.ComboBox ();
This.richtextbox1 = new System.Windows.Forms.RichTextBox ();
This. But_eixt = new System.Windows.Forms.Button ();
This. SuspendLayout ();
//
ComboBox1
//
This.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
This.comboBox1.DropDownWidth = 160;
This.comboBox1.Location = new System.Drawing.Point (8, 232);
This.comboBox1.Name = "ComboBox1";
This.comboBox1.Size = new System.Drawing.Size (168, 20);
This.comboBox1.TabIndex = 1;
This.comboBox1.SelectedIndexChanged + = new System.EventHandler (this.combobox1_selectedindexchanged);
//
RichTextBox1
//
This.richTextBox1.Dock = System.Windows.Forms.DockStyle.Top;
This.richTextBox1.Name = "RichTextBox1";
This.richTextBox1.Size = new System.Drawing.Size (292, 208);
This.richTextBox1.TabIndex = 0;
This.richTextBox1.Text = "";
//
But_eixt
//
This. But_eixt.location = new System.Drawing.Point (200, 232);
This. But_eixt.name = "But_eixt";
This. But_eixt.tabindex = 2;
This. But_eixt.text = "Eixt";
//
Form1
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 273);
This. Controls.AddRange (new system.windows.forms.control[] {
This.but_eixt,this.combobox1,this.richtextbox1});
This. Name = "Form1";
This. Text = "Form1";
This. Load + = new System.EventHandler (this. Form1_Load);
This. Inputlanguagechanged + = new System.Windows.Forms.InputLanguageChangedEventHandler (this. Changeinput);
This. ResumeLayout (FALSE);
}

(4), insert the following code:

private void Form1_Load (object sender, System.EventArgs e)
{
Inputlanguagecollection ILC = inputlanguage.installedinputlanguages;
foreach (InputLanguage il in ILC)
{
COMBOBOX1.ITEMS.ADD (IL. LayoutName);
}
Combobox1.selectedindex = InputLanguage.InstalledInputLanguages.IndexOf (inputlanguage.currentinputlanguage);
}

private void ComboBox1_SelectedIndexChanged (object sender, System.EventArgs e)
{
InputLanguage il = inputlanguage.installedinputlanguages[Combobox1.selectedindex];
inputlanguage.currentinputlanguage = il;
}

private void Changeinput (object sender, System.Windows.Forms.InputLanguageChangedEventArgs e)
{
InputLanguage il = e.inputlanguage;
int i = InputLanguage.InstalledInputLanguages.IndexOf (IL);
if (i >= 0 && i < InputLanguage.InstalledInputLanguages.Count)
{
Combobox1.selectedindex = i;
}
}

private void But_eixt_click (object sender, System.EventArgs e)
{
Application.exit ();
}

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.