L' indice parte da 0:
stringa: pippo
indice: p = 0, i = 1, p = 2, ecc.
Comunque io in vb uso un usercontrol per evitare l'inserimento di caratteri non consentiti e per convertire la data nel formato "dd/mm/yyyy".
Nell' esempio sotto sono accettati solo i caratteri da 0 a 9, /(shift+7), il punto e il tasto DEL. Le date inserite nel formato "ddMMyyyy", "ddMMyy", "dd.MM.yy", "dd.MM.yyyy" vengono convertite nel formato "dd/mm/yyyy" con l' evento Validating.
Vedi tu se può esserti utile:
codice:
Public Class tbox_mod
Inherits System.Windows.Forms.TextBox
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean
Console.WriteLine(keyData)
Select Case keyData
Case 48 To 57, 8, 190, 65591
'aggiungere gestione tooltip
Case Else
'aggiungere gestione tooltip
msg = Nothing
End Select
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub validazione(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles Me.Validating
If Me.Text = "" Then
'aggiungere gestione tooltip
Return
End If
Dim dataconv As String = Me.Text
Try
Dim formats As String() = New String() {"ddMMyyyy", "ddMMyy", "dd.MM.yy", "dd.MM.yyyy"}
Dim result = DateTime.ParseExact(dataconv, formats, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None)
Me.Text = CStr(result)
'aggiungere gestione tooltip
Catch ex As System.Exception
'aggiungere gestione tooltip
Console.WriteLine(ex.Message)
e.Cancel = True
End Try
End Sub
End Class