C#中窗体程序中快捷键的设置

来源:互联网 发布:网络机房整改目的 编辑:程序博客网 时间:2024/05/22 01:51

1.填写表单时摁enter键或是上下键跳到下一编辑框

  private void textBox2_KeyDown(object sender, KeyEventArgs e)//keyDown事件
        {
            if (e.KeyCode == Keys.Enter)//摁enter键,跳到你制定的编辑框
            {
                textBox3.Focus();
            }
            else if (e.KeyCode == Keys.Down)//摁↓键
            {
                textBox3.Focus();
            }
            else if (e.KeyCode == Keys.Up)//摁↑键
            {
                textBox1.Focus();
            }
   else if (e.Control && e.KeyCode == Keys.Left)//摁组合键ctr+←
            {
                listBox1.Focus();
            }
}

2.填表单时邮箱的验证(使用正则表达式)

    private string regexStr1 = "^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$";//验证邮箱的正则表达式
      private void button1_Click(object sender, EventArgs e)
        {
            string a = textBox2.Text + "@" + textBox4.Text;//读取用户输入的邮箱信息
             if (!Regex.IsMatch(a,regexStr1))//使用正则表达式的函数Regex来匹配是否符合格式
            {
                MessageBox.Show("邮箱格式不正确");
            }
    }

3.关于窗口关闭问题

  C#windowForm的一个常见问题就是,你关闭主窗口时,其他窗口都会被关闭。这是因为该窗体同时是启动窗体,即它是应用程序中所有窗体的父类,则整个应用程序会被关闭,所以你在关闭时可以给出对话框的提示。
  
1 private void Form2_FormClosing(object sender, FormClosingEventArgs e)   2 {   3     DialogResult result = MessageBox.Show("你确定要关闭吗!", "提示信息",   MessageBoxButtons.OKCancel, MessageBoxIcon.Information);   4     if (result == DialogResult.OK)   5     {   6         e.Cancel = false;  //确认关闭 7     }   8     else  9     {  10         e.Cancel = true;  //取消关闭11     }  12 }

  如果你不想关闭其他窗体,那你可以把主窗体隐藏起来,在formClosing方法中输入e.cancle = true;this.Hide();此时主窗体只是隐藏起来,当你关闭其他窗体时设置Application.Exit();就可以将整个程序关闭。
1 0
原创粉丝点击