1. 窗体Tab键的顺序设置
选中窗体-视图-tab键顺序
label不适用tab键
2. 热键设置和快捷键设置
热键:无论光标在哪都可以
快捷键:出现界面后才能按
添加label
更改label的Text属性改为:热键关闭&C
当按下Alt+C时,Close按钮获得焦点【本应该是热键关闭(C)这个label获得焦点,但是label不能使得光标停留,因此光标自动跳转到label的下一个控件上】
当按下Alt+G时,相当于直接点击了关闭(G)按钮
3. ComboBox
不让用户输入,修改DropDownStyle为DropDownList
如果想要其显示默认值,则在Form_load里面为其SelectedIndex赋值
4. 选择默认按钮
选中form,在其属性AccpetButton里面选择相应的按钮
四则运算的代码
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 四则运算器 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Resize(object sender, EventArgs e) { //目标:点击最大化,窗口大小不变或者使得最大化的按钮失效 //触发的是最大化按钮 if(this.WindowState == FormWindowState.Maximized) { //设置窗口的大小为正常大小 this.WindowState = FormWindowState.Normal; } } private void button2_Click(object sender, EventArgs e) { MessageBox.Show("快捷键Alt+G,Window is closing"); this.Close(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("快捷键Alt+C,Window is closing"); this.Close(); } private void txtNum_KeyPress(object sender, KeyPressEventArgs e) { //触发该事件的控件,通过sender转换成严控件 TextBox currentTxt = sender as TextBox; if(e.KeyChar < '0' || e.KeyChar > '9') { //阻止输入 e.Handled = true; } //让用户能输入退格键,其ASCII是8 if(e.KeyChar == 8) { e.Handled = false; } //让用户可以输入小数点,小数点ASCII为46 if(e.KeyChar == 46) { //检测是否已经输入小数点,么有找到则返回-1 if(currentTxt.Text.IndexOf(".") == -1) { //小数点不能出现在第一位 if(currentTxt.SelectionStart > 0) { e.Handled = false; } } } //MessageBox.Show(((int)(e.KeyChar)).ToString()); } private void btnEqual_Click(object sender, EventArgs e) { double num1 = Convert.ToDouble(txtNum1.Text); double num2 = Convert.ToDouble(txtNum2.Text); //C#特有 switch(cboOperator.Text) { case "+": lbResult.Text = (num1 + num2).ToString(); break; case "-": lbResult.Text = (num1 - num2).ToString(); break; case "*": lbResult.Text = (num1 * num2).ToString(); break; case "/": lbResult.Text = (num1 / num2).ToString(); break; } } private void Form1_Load(object sender, EventArgs e) { cboOperator.SelectedIndex = 0; } } }