winforms 在键盘上输入验证数字,检查文本框是否为空

3mpgtkmj  于 2022-11-25  发布在  其他
关注(0)|答案(1)|浏览(131)

我实现了一个登录,在这个登录中我输入了两个文本框,一个用于用户名,一个用于密码。在文本框下面我插入了一个数字键盘,现在我想对键盘上按下的数字进行验证。我设置了如果tetxBox为空,labeltext将返回一个错误。我计划做的是检查输入是否正确,它会向我发出信号,表明输入正确,然后按下Enter按钮,我可以在下一个textBox上写入,另一方面,如果textBox无效,它不会让我转到下一个textBox
请帮帮我,我要为此疯狂了:)

private void button15_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(txt_nomeUtente.Text))
        {

            label6.ForeColor = Color.Red;
            label6.Text = "username empty, cannot be left empty";
            txt_nomeUtente.Text += ((Button)sender).Text;



        }
        else
        {
            label6.ForeColor = Color.Green;
            label6.Text = "username is acceptable";
            txt_Password.Text += ((Button)sender).Text;



            if (string.IsNullOrEmpty(txt_Password.Text))
            {
                label6.ForeColor = Color.Red;
                label6.Text = " password is empty";

            }
            else
            {
                label6.ForeColor = Color.Green;
                label6.Text = "La password is acceptable";

            }

        }

    }
c86crjj0

c86crjj01#

如果用户输入的用户名无效,您似乎希望将重点放在用户名上;如果用户名有效,您似乎希望将重点放在密码上:

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 Check_User_Input
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            label6.Text = txt_nomeUtente.Text.Length.ToString("N0");
        }

        private void button15_Click(object sender, EventArgs e)
        {
            CheckForValidEntries();
        }

        private void CheckForValidEntries()
        {
            if (string.IsNullOrEmpty(txt_nomeUtente.Text))
            {
                label6.ForeColor = Color.Red;
                label6.Text = "username empty, cannot be left empty";
                //txt_nomeUtente.Text += ((Button)sender).Text;
            }
            else
            {
                label6.ForeColor = Color.Green;
                label6.Text = "username is acceptable";
                txt_Password.Focus();
                if (string.IsNullOrEmpty(txt_Password.Text))
                {
                    label6.ForeColor = Color.Red;
                    label6.Text = " password is empty";
                }
                else
                {
                    label6.ForeColor = Color.Green;
                    label6.Text = "La password is acceptable";
                }
            }
        }

        private void txt_nomeUtente_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                CheckForValidEntries();
        }
    }
}

相关问题