當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET ErrorProvider.GetError方法代碼示例

本文整理匯總了VB.NET中System.Windows.Forms.ErrorProvider.GetError方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET ErrorProvider.GetError方法的具體用法?VB.NET ErrorProvider.GetError怎麽用?VB.NET ErrorProvider.GetError使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Windows.Forms.ErrorProvider的用法示例。


在下文中一共展示了ErrorProvider.GetError方法的2個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1:

Private Sub TextBox1_Validating(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs) _
Handles TextBox1.Validating

    ' If nothing is entered,
    ' an ArgumentException is caught; if an invalid directory is entered, 
    ' a DirectoryNotFoundException is caught. An appropriate error message 
    ' is displayed in either case.
    Try
        Dim directory As New System.IO.DirectoryInfo(TextBox1.Text)
        directory.GetFiles()
        ErrorProvider1.SetError(TextBox1, "")

    Catch ex1 As System.ArgumentException
        ErrorProvider1.SetError(TextBox1, "Please enter a directory")

    Catch ex2 As System.IO.DirectoryNotFoundException
        ErrorProvider1.SetError(TextBox1, _
        "The directory does not exist." & _
        "Try again with a different directory.")
    End Try

End Sub

' This method handles the LostFocus event for TextBox1 by setting the 
' dialog's InitialDirectory property to the text in TextBox1.
Private Sub TextBox1_LostFocus(ByVal sender As Object, _
    ByVal e As System.EventArgs) Handles TextBox1.LostFocus
    OpenFileDialog1.InitialDirectory = TextBox1.Text
End Sub


' This method demonstrates using the ErrorProvider.GetError method 
' to check for an error before opening the dialog box.
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click

    'If there is no error, then open the dialog box.
    If ErrorProvider1.GetError(TextBox1) = "" Then
        Dim dialogResult As DialogResult = OpenFileDialog1.ShowDialog()
    End If

End Sub
開發者ID:VB.NET開發者,項目名稱:System.Windows.Forms,代碼行數:43,代碼來源:ErrorProvider.GetError

示例2: DeferredValidation

' 導入命名空間
Imports System.Windows.Forms

public class DeferredValidation
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class


Public Class Form1
    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating, TextBox2.Validating, TextBox3.Validating
        If (TextBox1.Text.Length <> 0) And Not (TextBox1.Text Like "##") _
        Then
            ErrorProvider1.SetError(TextBox1, TextBox1.Name & " must contain exactly two digits")
        Else
            ErrorProvider1.SetError(TextBox1, "")
        End If

    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        e.Cancel = True
        If IsInvalidField(TextBox1) Then Exit Sub
        If IsInvalidField(TextBox3) Then Exit Sub
        e.Cancel = False
    End Sub

    Private Function IsInvalidField(ByVal ctl As Control) As Boolean
        If ErrorProvider1.GetError(ctl).Length = 0 Then
            Return False
        Else
            MessageBox.Show(ErrorProvider1.GetError(ctl))
            ctl.Focus()
            Return True
        End If
    End Function
End Class

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Public Class Form1
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Me.Label1 = New System.Windows.Forms.Label
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Label2 = New System.Windows.Forms.Label
        Me.TextBox3 = New System.Windows.Forms.TextBox
        Me.Label3 = New System.Windows.Forms.Label
        Me.ErrorProvider1 = New System.Windows.Forms.ErrorProvider(Me.components)
        CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(8, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(56, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Five Digits"
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(72, 8)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(80, 20)
        Me.TextBox1.TabIndex = 1
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(72, 32)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(80, 20)
        Me.TextBox2.TabIndex = 3
        '
        'Label2
        '
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(8, 32)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(56, 13)
        Me.Label2.TabIndex = 2
        Me.Label2.Text = "Five Digits"
        '
        'TextBox3
        '
        Me.TextBox3.Location = New System.Drawing.Point(72, 56)
        Me.TextBox3.Name = "TextBox3"
        Me.TextBox3.Size = New System.Drawing.Size(80, 20)
        Me.TextBox3.TabIndex = 5
        '
        'Label3
        '
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(8, 56)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(56, 13)
        Me.Label3.TabIndex = 4
        Me.Label3.Text = "Five Digits"
        '
        'ErrorProvider1
        '
        Me.ErrorProvider1.ContainerControl = Me
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(210, 83)
        Me.Controls.Add(Me.TextBox3)
        Me.Controls.Add(Me.Label3)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.Label1)
        Me.Name = "Form1"
        Me.Text = "Deferred Validation"
        CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents TextBox3 As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    Friend WithEvents ErrorProvider1 As System.Windows.Forms.ErrorProvider

End Class
開發者ID:VB程序員,項目名稱:System.Windows.Forms,代碼行數:150,代碼來源:ErrorProvider.GetError


注:本文中的System.Windows.Forms.ErrorProvider.GetError方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。