Con VB.NET 2003 ho riscontrato tuttavia un problema con questa soluzione: sembrerebbe che se il tasto premuto è backspace VB.NET ignori la proprietà Handled e proceda comunque con la cancellazione del carattere.codice:Private Sub txt_dataInizioContratto_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_dataInizioContratto.KeyDown e.Handled = (e.KeyData = Keys.Back) Or (e.KeyData = Keys.Left) Or (e.KeyData = Keys.Delete) Or (e.KeyData = Keys.Home) 'ed eventuali altri tasti "scottanti" End Sub
Una possibile soluzione, che ha anche il vantaggio di mostrare graficamente che l'azione è da considerarsi vietata, può essere questa:
codice:Private Sub txt_dataInizioContratto_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_dataInizioContratto.KeyDown e.Handled = (e.KeyData = Keys.Back) Or (e.KeyData = Keys.Left) Or (e.KeyData = Keys.Delete) Or (e.KeyData = Keys.Home) 'ed eventuali altri tasti "scottanti" DirectCast(sender, TextBox).ReadOnly = e.Handled End Sub Private Sub txt_dataInizioContratto_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txt_dataInizioContratto.KeyUp DirectCast(sender, TextBox).ReadOnly = False End Sub

Rispondi quotando