On se sert des événements de touches. Ils se produisent lorsque le TextBox a le focus dans l'ordre suivant
KeyDown : Une touche a été enfoncée
KeyPress : Déclenché si la touche enfoncée représente un caractère. (Il n'est pas déclenché pour les touches F1, F2 par exemple)
KeyUp : Une touche a été relâchée
Voici un exemple d'utilisation.
Créer un projet WinForm C#
Ajouter deux TextBox à la form principale de votre projet
Le code suivant utilise une form nommée Form1, et deux TextBox nommés textBox1 et textBox2
Toutes les touches frappées sur le textBox1 se répercutent sur textBox2.
Compiler et lancer le projet, puis entrer des données dans textBox1.
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) _
Handles TextBox1.KeyPress
If Not Char.IsControl(e.KeyChar) Then
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) + _
e.KeyChar.ToString + _
TextBox1.Text.Substring(TextBox1.SelectionStart + _
TextBox1.SelectionLength)
End If
End Sub
Private Sub textBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) _
Handles TextBox1.KeyDown
If e.KeyCode = Keys.Back AndAlso TextBox1.Text.Length > 0 Then
If TextBox1.SelectionLength > 0 Then
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) + _
TextBox1.Text.Substring(TextBox1.SelectionStart + _
TextBox1.SelectionLength)
ElseIf TextBox1.SelectionStart > 0 Then
If TextBox1.SelectionStart = TextBox1.Text.Length Then
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)
Else
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart - 1) + _
TextBox1.Text.Substring(TextBox1.SelectionStart + _
TextBox1.SelectionLength)
End If
End If
ElseIf e.KeyCode = Keys.Delete AndAlso TextBox1.Text.Length > 0 Then
If TextBox1.SelectionStart = TextBox1.Text.Length Then
If e.Shift Then
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.Text.Length - 1)
End If
Else
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart)
If Not (TextBox1.SelectionLength = 0) Then
TextBox2.AppendText(TextBox1.Text.Substring(TextBox1.SelectionStart + TextBox1.SelectionLength))
Else
If Not e.Control Then
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) + _
TextBox1.Text.Substring(TextBox1.SelectionStart + 1)
End If
End If
End If
ElseIf (e.Shift AndAlso e.KeyCode = Keys.Insert) OrElse (e.Control AndAlso e.KeyCode = Keys.V) Then
Dim cpdata As IDataObject = Clipboard.GetDataObject
If Not (cpdata Is Nothing) AndAlso cpdata.GetDataPresent(String.Empty.GetType) Then
Dim data As String = cpdata.GetData(String.Empty.GetType).ToString
Dim print As Boolean = False
For i As Integer = 0 To data.Length - 1
If Char.IsControl(data, i) AndAlso print Then
data = data.Substring(0, i)
Exit For
ElseIf Not Char.IsControl(data, i) AndAlso Not print Then
print = True
End If
System.Math.Min(System.Threading.Interlocked.Increment(i), i - 1)
Next
TextBox2.Text = TextBox1.Text.Substring(0, TextBox1.SelectionStart) + _
data + _
TextBox1.Text.Substring(TextBox1.SelectionStart + TextBox1.SelectionLength)
End If
End If
End Sub
|