Reputation: 223
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:
Upvotes: 1
Views: 1058
Reputation: 23732
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
Upvotes: 1
Reputation: 17964
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
}
}
Upvotes: 1
Reputation: 389
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 = "";
}
Upvotes: 0