VB.Net Tutorial/GUI/ErrorProvider — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 15:57, 26 мая 2010

ErrorProvider

<source lang="vbnet">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.ruponentModel.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.rupilerServices.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.ruponentModel.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.ruponents = New System.ruponentModel.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.ruponents)
       CType(Me.ErrorProvider1, System.ruponentModel.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.ruponentModel.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</source>

Get error from ErrorProvider

<source lang="vbnet">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.ruponentModel.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.rupilerServices.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.ruponentModel.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.ruponents = New System.ruponentModel.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.ruponents)
       CType(Me.ErrorProvider1, System.ruponentModel.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.ruponentModel.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</source>