删除文本框中的字符

Deleting characters in a textbox

我正在尝试配置文本框,以便在按下 ctrl + delete 键时清除文本框。

这是我的全部代码:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Windows.Forms;

namespace pruebaMensajes
{
    public partial class Providus : Form{
        public Providus(){
            InitializeComponent();
            txtUsuario.MaxLength = 20;//max character
            txtContrasena.MaxLength = 16;
            txtContrasena.PasswordChar = '*';//type
        }

        private void Button1_Click(object sender, EventArgs e){//para el login
            string usuario = txtUsuario.Text;
            string contrasena = txtContrasena.Text;
            SqlConnection con = new SqlConnection("stringChain");
            SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM usuariosProvidus WHERE usuario='" + usuario + "' AND contrasena='" + contrasena + "'", con);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            if (dt.Rows[0][0].ToString() == "1"){
                this.Hide();
                new Inicio().Show();
            } else{
                MessageBox.Show("Wrong user o password.","Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Providus_Load(object sender, EventArgs e){ }

        private void TxtContrasena_KeyPress_1(object sender, KeyPressEventArgs e){//manejar el enter para el login en contraseña
            if ((int)e.KeyChar == (int)Keys.Enter){
                string usuario = txtUsuario.Text;
                string contrasena = txtContrasena.Text;
                SqlConnection con = new SqlConnection("Data Source=PC-HELP;Initial Catalog=apiTiny;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM usuariosProvidus WHERE usuario='" + usuario + "' AND contrasena='" + contrasena + "'", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows[0][0].ToString() == "1"){
                    this.Hide();
                    new Inicio().Show();
                }else if(txtUsuario.Text==String.Empty){
                    lblMensaje.BackColor = Color.FromArgb(255, 0, 0);
                    lblMensaje.Text = "Wrong user.";
                    txtUsuario.Focus();
                }else if (txtContrasena.Text == String.Empty){
                    lblMensaje.BackColor = Color.FromArgb(255, 0, 0);
                    lblMensaje.Text = "Wrong password.";
                    txtContrasena.Focus();
                }
            }
        }

        private void TxtUsuario_KeyPress(object sender, KeyPressEventArgs e){//enter para el txt usuario
            if ((int)e.KeyChar == (int)Keys.Enter){
                string usuario = txtUsuario.Text;
                string contrasena = txtContrasena.Text;
                SqlConnection con = new SqlConnection("Data Source=PC-HELP;Initial Catalog=apiTiny;Integrated Security=True");
                SqlDataAdapter sda = new SqlDataAdapter("SELECT COUNT(*) FROM usuariosProvidus WHERE usuario='" + usuario + "' AND contrasena='" + contrasena + "'", con);
                DataTable dt = new DataTable();
                sda.Fill(dt);
                if (dt.Rows[0][0].ToString() == "1"){
                    this.Hide();
                    new Inicio().Show();
                }
                else if (txtUsuario.Text == String.Empty){
                    lblMensaje.BackColor = Color.FromArgb(255, 0, 0);
                    lblMensaje.Text = "Worng user.";
                    txtUsuario.Focus();
                }
                else if (txtContrasena.Text == String.Empty){
                    lblMensaje.BackColor = Color.FromArgb(255, 0, 0);
                    lblMensaje.Text = "Worng password.";
                    txtContrasena.Focus();
                }
            };
        }

        private void TxtUsuario_KeyDown(object sender, KeyEventArgs e){
            if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.ControlKey){
                e.Handled = true;
                txtUsuario.Text = "";
            }
        }

        private void TxtContrasena_KeyDown(object sender, KeyEventArgs e){
            if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control){
                e.Handled = true;
                txtContrasena.Text = "";
            }
        }
    }
}

有人知道我该怎么做吗?因为当我按下键 ctrl + delete 这会添加一个字符而不是清除文本框。 我使用 keydown 事件来实现这一点,但它对我不起作用

在 gif 中:

绑定到文本框的KeyDown事件方法:

private void txtContrasena_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Control && e.KeyCode == Keys.Delete)
      txtContrasena.Text = "";
}

你可以查看修饰符,也可以看这里:KeyDown : recognizing multiple keys

private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control)
    {
        //do stuff
    }
}  

伙计,你只是用错了钥匙,and/or说的是错误的钥匙。

你说的是 Backspace key 事件,尽管你称它为 "delete"。

Delete 键是一个不同的键,实际上可以使用!

如果您真的想使用退格键,您还需要在代码中检查它:

if (e.Control && e.KeyCode == Keys.Back)
{
    textBox1.Text = "";
    e.Handled = true;
}

不幸的是,这将在文本框中留下最后一个 0x7FDEL ascii 字符。我还没有想出如何完全摆脱它:)

我建议使用您的代码并按 real del 按钮