1

I am trying to configure the TextBox so that when the ctrl + delete keys are pressed the TextBox is cleaned.

This is my entire code:

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 = "";
            }
        }
    }
}

Does anyone know how I can do it? Cause when i press the keys ctrl + delete this adds a character instead of clearing the textbox. I use the keydown event to realize this but it is not working for me

In a gif:

enter image description here

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Franqo Balsamo
  • 171
  • 1
  • 15

3 Answers3

1

You can check the modifier, also see here: KeyDown : recognizing multiple keys

private void MyTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control)
    {
        //do stuff
    }
}  
Carra
  • 17,808
  • 7
  • 62
  • 75
1

Mate you are simply using the wrong key, and/or are talking about the wrong key.

You are talking about the Backspace key event though you call it "delete".

The Delete key is a different one and will actually work!

If you really want to use the backspace key you would need to check also for it in the code:

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

unfortunately this will leave one last 0x7F or DEL ascii character remaining in the textbox. I haven't figured it out how to get rid of it entirely yet :)

I would advise to use your code and press the real del button

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

Bind to the event KeyDown of the textbox the method :

private void txtContrasena_KeyDown(object sender, KeyEventArgs e)
{
   if (e.Control && e.KeyCode == Keys.Delete)
      txtContrasena.Text = "";
}
nellowl
  • 339
  • 3
  • 16
  • But, when i compile the aplication and i press ctrl+delete this add a character. I need to clear the TextBox – Franqo Balsamo Oct 08 '19 at 14:29
  • 4
    I suggest adding `e.Handled = true;` for `txtContrasena` do not process `Delete` – Dmitry Bychenko Oct 08 '19 at 14:29
  • Hmm, not woriking. – Franqo Balsamo Oct 08 '19 at 14:35
  • 2
    "Hmm, not woriking." what does that exactly mean? does not delete? throws exception? how did you register the event? – Mong Zhu Oct 08 '19 at 14:37
  • 1
    When i press keys Delete and Control, the TextBox add a character instead the clear – Franqo Balsamo Oct 08 '19 at 14:38
  • 2
    @Franqo Balsamo: what character has been added? – Dmitry Bychenko Oct 08 '19 at 14:39
  • 2
    @Franqo Balsamo: do you have *anything* with `TextChanged`, `KeyUp`, `KeyPress`? – Dmitry Bychenko Oct 08 '19 at 14:41
  • 1
    @Franqo Balsamo: put `e.Handled = true;` *within* `if` - it's only delete we want to handle: `if (e.KeyCode == Keys.Delete && e.Modifiers == Keys.Control){ e.Handled = true; txtContrasena.Text = ""; }` – Dmitry Bychenko Oct 08 '19 at 14:46
  • character has been added is like a rectangle – Franqo Balsamo Oct 08 '19 at 14:48
  • 1
    @FranqoBalsamo sorry mate, but I cannot reproduce your problem. There seems something to be missing. – Mong Zhu Oct 08 '19 at 14:57
  • 1
    @FranqoBalsamo you are actually writing a `DEL` ascii character hex code is `0x7F` [source](https://www.rapidtables.com/convert/number/ascii-to-hex.html). And your programm seems to suffer from the same stuff as does the property windows in the visual studio designer, when you try to use this key combination in the Name field. Only there it writes those rectangles when using Cntrl + Back – Mong Zhu Oct 08 '19 at 15:23
  • 1
    @FranqoBalsamo damit! I got it :D you are using the wrong key! you are actually talking about the backspace key! which is in the upper right corner. and your code is waiting for the key where `del` is written – Mong Zhu Oct 08 '19 at 15:26