📄 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 ASCII_Code
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// 退出程序的指令
Application.Exit();
}
private void button2_Click(object sender, EventArgs e)
{
// 弹出 About 窗口并设置起始位置为其父窗口中央
About newForm = new About();
newForm.TopMost = true;
newForm.StartPosition = FormStartPosition.CenterParent;
newForm.ShowDialog();
}
private void CharBox_TextChanged(object sender, EventArgs e)
{
if (CharBox.Text.Length != 0)
{
// 获取ASCII字符文本框的字符并转换成一个int值
int asciiCode = CharBox.Text.ToCharArray()[0];
// 以十六进制文本格式输出到十六进制文本框里
HexBox.Text = "0x" + asciiCode.ToString("X");
// 以十进制格式输出到十进制文本框里
DecBox.Text = asciiCode.ToString();
}
else
{
HexBox.Clear();
DecBox.Clear();
}
}
private void DecBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyValue < '0' || e.KeyValue > '9') )
{
// 如果按下的字符不是数字
if ( DecBox.Text.Length > 0)
{
// 文本框里的内容不为空
DecBox.Text = DecBox.Text.Substring(0, DecBox.Text.Length - 1);
DecBox.SelectionStart = DecBox.Text.Length;
DecBox.SelectionLength = 0;
}
//e.Handled = false;
return;
}
//else
// e.Handled = true;
if (DecBox.Text.Length != 0)
{
// 获取十进制文本框里的字符串转换成int
int decNumber = Convert.ToInt32(DecBox.Text.ToString());
// 如果数字大于65535,会导致ASCII文本框溢出
if (decNumber >= 0xffff)
decNumber = 0;
// 以十六进制格式输出到十六进制文本框里
HexBox.Text = "0x" + decNumber.ToString("X");
// 转换为ASCII字符
char asciiCode = Convert.ToChar(decNumber);
// 输出到ASCII字符文本框里
this.CharBox.TextChanged -= new EventHandler(CharBox_TextChanged);
CharBox.Text = asciiCode.ToString();
this.CharBox.TextChanged += new EventHandler(CharBox_TextChanged);
}
else
{
DecBox.Clear();
CharBox.Clear();
}
}
private void HexBox_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
if ((e.KeyValue >= '0' && e.KeyValue <= '9') ||
(e.KeyValue >= 'A' && e.KeyValue <= 'F') ||
(e.KeyValue >= 'a' && e.KeyValue <= 'f'))
{
// 输入的内容是十六进制格式的数字
if (HexBox.Text.Length != 0)
{
int decNumber;
bool parseResult = int.TryParse(HexBox.Text.ToString(), System.Globalization.NumberStyles.HexNumber, null, out decNumber);
// 如果转换失败,就退出本函数
if (parseResult == true)
{
DecBox.Text = decNumber.ToString();
char asciiCode = Convert.ToChar(decNumber);
this.CharBox.TextChanged -= new EventHandler(CharBox_TextChanged);
CharBox.Text = asciiCode.ToString();
this.CharBox.TextChanged += new EventHandler(CharBox_TextChanged);
}
else if (HexBox.Text.Length > 0)
{
HexBox.Text = HexBox.Text.Substring(0, HexBox.Text.Length - 1);
HexBox.SelectionStart = DecBox.Text.Length;
HexBox.SelectionLength = 0;
}
}
else
{
DecBox.Clear();
CharBox.Clear();
}
}
else if (HexBox.Text.Length > 0)
{
HexBox.Text = HexBox.Text.Substring(0, HexBox.Text.Length - 1);
HexBox.SelectionStart = DecBox.Text.Length;
HexBox.SelectionLength = 0;
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// 根据复选框状态设置窗口是否为最顶层窗口
if (this.TopCheck.Checked == true)
this.TopMost = true;
else
this.TopMost = false;
this.Show();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -