📄 form1.cs
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WinApp10_2数字加密示例
{
public partial class Form1 : Form
{
string x;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "";
label1.Text = "您实际输入的是: ";
this.Text = "数字加密示例";
}
// KeyValue值:录入键区0~9 48~57 数字键区0~9 96~105
// End键的KeyValue值:35
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (textBox1.Text == "")
x = "";
else
x = textBox1.Text;
// 如果用户按下的不是〈BackSpace〉键
if ((int)e.KeyCode != (int)Keys.Back && (e.KeyValue >= 48
&& e.KeyValue <= 57 || e.KeyValue >= 96 && e.KeyValue <= 105))
{
// 将输入的实际字符存入Label1的Text属性中
if(e.KeyValue<96) // 录入键区的数字健
label1.Text += (char)e.KeyValue;//(char)e.KeyCode;
else // 数字键区的数字健
label1.Text += (char)(e.KeyValue-48);
}
else if ((int)e.KeyCode == (int)Keys.Back) // 如果按下的是〈BackSpace〉键,删除标签中最后一个字符
{
if (label1.Text[label1.Text.Length - 1] == ':')
//{
// MessageBox.Show("已无字符可删除!", "提示", MessageBoxButtons.OK,
// MessageBoxIcon.Error);
return;
//}
label1.Text = label1.Text.Remove(label1.Text.Length - 1);
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
// 如果用户按下了〈Ctrl〉+〈Shift〉+〈End〉组合键,则直接退出
if (e.Control && e.Shift && e.KeyValue == 35)
this.Close();
// 如果用户按下的不是〈BackSpace〉或〈Enter〉键
if ((int)e.KeyCode != (char)Keys.Back && (int)e.KeyCode != (char)Keys.Enter)
{
switch ((int)e.KeyCode)
{
case (char)Keys.D1: // 录入键区的“1”与数字键区的“1”共享同一操作
case (char)Keys.NumPad1: textBox1.Text = x + "!"; break;
case (char)Keys.D2:
case (char)Keys.NumPad2: textBox1.Text = x + "&"; break;
case (char)Keys.D3:
case (char)Keys.NumPad3: textBox1.Text = x + "#"; break;
case (char)Keys.D4:
case (char)Keys.NumPad4: textBox1.Text = x + "/"; break;
case (char)Keys.D5:
case (char)Keys.NumPad5: textBox1.Text = x + ")"; break;
case (char)Keys.D6:
case (char)Keys.NumPad6: textBox1.Text = x + "$"; break;
case (char)Keys.D7:
case (char)Keys.NumPad7: textBox1.Text = x + "*"; break;
case (char)Keys.D8:
case (char)Keys.NumPad8: textBox1.Text = x + "@"; break;
case (char)Keys.D9:
case (char)Keys.NumPad9: textBox1.Text = x + "\\"; break;
case (char)Keys.D0:
case (char)Keys.NumPad0: textBox1.Text = x + "+"; break;
}
textBox1.SelectionStart = textBox1.TextLength; // 将文本框中的光标移动到最后
}
if ((int)e.KeyCode == (int)Keys.Enter) // 如果用户按下的是〈Enter〉键
{
if (MessageBox.Show("您确实要退出程序吗?", "确认退出",
MessageBoxButtons.OKCancel,MessageBoxIcon.Information)
== DialogResult.OK) // 如果用户单击了【确定】按钮则结束程序运行
this.Close();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -