Sovrascrivere in una TextBox


La modalità' di sovrascrittura del testo non e' supportata nelle TextBox, il cui funzionamento è solo in modalità' inserimento.

Ecco come simulare tale modalità:

Dichiarare la seguente variabile a livello di Form

Dim ModalitaSovrascrittura as Boolean

Inserire il seguente codice nell'evento KeyDown della TextBox

Private Sub Text1_KeyDown(KeyCode As Integer, Shift As Integer)
    'Imposto la variabile in base alla pressione del tasto INS
    If KeyCode = 45 And Shift = 0 Then
        ModalitaSovrascrittura = Not ModalitaSovrascrittura
    End If
End Sub

Inserire il seguente codice nell'evento KeyPress della TextBox

Private Sub Text1_KeyPress(KeyAscii As Integer)
    If ModalitaSovrascrittura = True And KeyAscii >= 32 And Text1.SelLength = 0 Then
        If Mid$(Text1.Text, Text1.SelStart + 1, 1) <> Chr$(13) Then
            Text1.SelLength = 1
        End If
    End If
End Sub

Testato su: Windows 98, Windows Me, Windows 2000 Professional