Share via


MaskedTextBox.TypeValidationCompleted Kejadian

Definisi

Terjadi ketika MaskedTextBox telah selesai mengurai nilai saat ini menggunakan ValidatingType properti .

public:
 event System::Windows::Forms::TypeValidationEventHandler ^ TypeValidationCompleted;
public event System.Windows.Forms.TypeValidationEventHandler TypeValidationCompleted;
public event System.Windows.Forms.TypeValidationEventHandler? TypeValidationCompleted;
member this.TypeValidationCompleted : System.Windows.Forms.TypeValidationEventHandler 
Public Custom Event TypeValidationCompleted As TypeValidationEventHandler 

Jenis Acara

Contoh

Contoh kode berikut mencoba mengurai input pengguna sebagai objek yang valid DateTime . Jika gagal, penanganan TypeValidationCompleted aktivitas menampilkan pesan kesalahan kepada pengguna. Jika nilainya valid DateTime, kode memverifikasi bahwa tanggal yang diberikan tidak sebelum tanggal hari ini. Contoh kode ini mengharuskan proyek Formulir Windows Anda berisi MaskedTextBox kontrol bernama MaskedTextBox1 dan ToolTip kontrol bernama ToolTip1.

private void Form1_Load(object sender, EventArgs e)
{
    maskedTextBox1.Mask = "00/00/0000";
    maskedTextBox1.ValidatingType = typeof(System.DateTime);
    maskedTextBox1.TypeValidationCompleted += new TypeValidationEventHandler(maskedTextBox1_TypeValidationCompleted);
    maskedTextBox1.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);

    toolTip1.IsBalloon = true;
}

void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
    if (!e.IsValidInput)
    {
        toolTip1.ToolTipTitle = "Invalid Date";
        toolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", maskedTextBox1, 0, -20, 5000);
    }
    else
    {
        //Now that the type has passed basic type validation, enforce more specific type rules.
        DateTime userDate = (DateTime)e.ReturnValue;
        if (userDate < DateTime.Now)
        {
            toolTip1.ToolTipTitle = "Invalid Date";
            toolTip1.Show("The date in this field must be greater than today's date.", maskedTextBox1, 0, -20, 5000);
            e.Cancel = true;
        }
    }
}

// Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires.
void maskedTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    toolTip1.Hide(maskedTextBox1);
}
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.MaskedTextBox1.Mask = "00/00/0000"
    Me.MaskedTextBox1.ValidatingType = GetType(System.DateTime)

    Me.ToolTip1.IsBalloon = True
End Sub

Private Sub MaskedTextBox1_TypeValidationCompleted(ByVal sender As Object, ByVal e As TypeValidationEventArgs) Handles MaskedTextBox1.TypeValidationCompleted
    If (Not e.IsValidInput) Then
        Me.ToolTip1.ToolTipTitle = "Invalid Date"
        Me.ToolTip1.Show("The data you supplied must be a valid date in the format mm/dd/yyyy.", Me.MaskedTextBox1, 0, -20, 5000)
    Else
        ' Now that the type has passed basic type validation, enforce more specific type rules.
        Dim UserDate As DateTime = CDate(e.ReturnValue)
        If (UserDate < DateTime.Now) Then
            Me.ToolTip1.ToolTipTitle = "Invalid Date"
            Me.ToolTip1.Show("The date in this field must be greater than today's date.", Me.MaskedTextBox1, 0, -20, 5000)
            e.Cancel = True
        End If
    End If
End Sub

' Hide the tooltip if the user starts typing again before the five-second display limit on the tooltip expires.
Private Sub MaskedTextBox1_KeyDown(ByVal sender As Object, ByVal e As KeyEventArgs) Handles MaskedTextBox1.KeyDown
    Me.ToolTip1.Hide(Me.MaskedTextBox1)
End Sub

Keterangan

MaskedTextBox Kontrol akan secara opsional memvalidasi input pengguna terhadap jenis yang ditentukan oleh propertinyaMaskedTextBox.ValidatingType. Ketika properti ini bukan null, rangkaian peristiwa berikut terjadi:

  1. Urutan validasi dimulai ketika salah satu hal berikut ini terjadi:

  2. Salah satu peristiwa ini menghasilkan panggilan ke Parse metode jenis yang ditentukan dengan ValidatingType properti . Parse bertanggung jawab atas konversi string input yang diformat ke jenis target. Konversi yang berhasil sama dengan validasi yang berhasil.

  3. Setelah Parse kembali, TypeValidationCompleted peristiwa dinaikkan. Penanganan aktivitas untuk kejadian ini paling sering diimplementasikan untuk melakukan pemrosesan validasi jenis atau masker. Ini menerima parameter yang TypeValidationEventArgs berisi informasi tentang konversi; misalnya, IsValidInput anggota menunjukkan apakah konversi berhasil.

  4. Setelah penanganan aktivitas untuk TypeValidationCompleted peristiwa kembali, peristiwa validasi standar, Validating, dinaikkan. Handler dapat diimplementasikan untuk melakukan validasi standar, mungkin termasuk membatalkan acara.

  5. Jika peristiwa tidak dibatalkan di langkah 3, peristiwa Validated validasi kontrol standar akan dinaikkan.

Cancel Jika properti diatur ke true di TypeValidationCompleted penanganan aktivitas, peristiwa akan dibatalkan dan MaskedTextBox kontrol mempertahankan fokus, kecuali peristiwa berikutnya Validating mengatur versi CancelEventArgs.Cancel propertinya kembali ke false.

Berlaku untuk

Lihat juga