Example of customizing shortcut keys in C# WinForm, _PHP tutorial

WBOY
Release: 2016-07-13 10:09:17
Original
1035 people have browsed it

Example of customizing shortcut keys in C# WinForm,

Download the source code of this article: http://xiazai.jb51.net/201501/tools/cs-key-setting.rar

During the project development process, it is necessary to implement the custom shortcut key function in the software settings similar to Youdao Dictionary, as shown in the figure below:

When we press Ctrl+Alt+M one after another, the software will automatically display the shortcut keys in the text box.

The final effect is as shown below:

The core code is as follows:

Copy code The code is as follows:

private void keyDown(object sender, KeyEventArgs e)
{
StringBuilder keyValue = new StringBuilder();
​ keyValue.Length = 0;
​ keyValue.Append("");
If (e.Modifiers != 0)
{
           if (e.Control)
              keyValue.Append("Ctrl + ");
If (e.Alt)
              keyValue.Append("Alt + ");
If (e.Shift)
              keyValue.Append("Shift + ");
}
If ((e.KeyValue >= 33 && e.KeyValue <= 40) ||
(e.KeyValue >= 65 && e.KeyValue <= 90) || //a-z/A-Z
(e.KeyValue >= 112 && e.KeyValue <= 123)) //F1-F12
{
         keyValue.Append(e.KeyCode);
}
else if ((e.KeyValue >= 48 && e.KeyValue <= 57)) //0-9
{
         keyValue.Append(e.KeyCode.ToString().Substring(1));
}
This.ActiveControl.Text = "";
//Set the text content of the current active control
This.ActiveControl.Text = keyValue.ToString();
}

private void keyUp(object sender, KeyEventArgs e)
{
String str = this.ActiveControl.Text.TrimEnd();
int len ​​= str.Length;
If (len >= 1 && str.Substring(str.Length - 1) == "+")
{
This.ActiveControl.Text = "";
}
}

Correspondence between e.KeyValue and characters

字符 e.KeyValue
a-z|A-Z 65-90
F1-F12 112-123
0-9 48-57
PageUp 33
PageDown 34
End 35
Home 36
左(←) 37
上( ↑ ) 38
右(→) 39
下( ↓ ) 40

Next, set the _KeyDown and _KeyUp events for the textbox control respectively, and call the above two core functions in them.

As shown below:

Copy code The code is as follows:

private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
KeyDown(sender, e);
}

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
KeyUp(sender, e);
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/946746.htmlTechArticleC# Example of customizing shortcut keys in WinForm. Download the source code of this article: http://xiazai.jb51.net /201501/tools/cs-key-setting.rar During the project development process, it is necessary to implement something like Youdao Dictionary...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template