Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the C# Control input method

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to realize C#control input method", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "how to realize C#control input method"!

At least three input methods are generally installed in Windows systems, and input methods are often switched when inputting data. Although Windows systems provide switching shortcuts, they still bring a lot of trouble to input work. If you provide intelligent input method automatic switching for users in the application, then such an application will appear more professional and competitive. I don't know if you've ever used Access, but Access automatically switches input methods when you enter table data. Cool, right? Now you can do it all. If you want your program to be cool, please continue...

For C#to control input methods, the. NET class library provides support in the System.Windows.Forms.InputLanguage class. I plan to spend a moment describing the functionality of the InputLanguage class, followed by an example, InputLanguageRichEdit.

1, InputLanguage class is a sealed class, it provides a number of methods and attributes to achieve input method management functions, which have a few attributes are particularly important, I will explain one by one below, if you want to fully understand all the methods and attributes of the class, please browse MSDN.

public static InputLanguage CurrentInputLanguage {get; set;} //Gets or sets the input method for the current thread. public static InputLanguage DefaultInputLanguage {get;} //Gets the default input method. public static InputLanguageCollection InstalledInputLanguages{get;} //Get system input method set. This container object allows you to enumerate a list of currently installed input methods for your system. public string LayoutName {get;} //Gets the name of the input method registered in the system tray. ......

Now that we have examined several important properties provided by the InputLanguage class, we can begin to apply the InputLanguage class to application development. I want to create a. NET Window Form system program that lists all the currently installed input methods in a list box and automatically changes the input method for the current thread by changing the options in the list box. At the same time, it also realizes the option of changing the list box according to the change of C#control input method in the desktop tray.

(1), New Project--> Select Visual C#Project--> Enter Project Name: Input Language Rich Edit.

(2)In the Toolbox, drag a RichTextBox control named richTextBox1, a ComboBox control named comboBox1, and a Button control named But_Exit.

(3)Replace with the following code.

private void InitializeComponent()。

{

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report