Advertisement
NelloRizzo

[VBNET] Form inserimento Person (con validazione)

Feb 15th, 2017
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 3.49 KB | None | 0 0
  1. ' http://pastebin.com/HiF6rp7b
  2. Imports CodiceFiscale.Model
  3.  
  4. Public Class MainForm
  5.  
  6. #Region "Proprietà per l'accesso ai controlli"
  7.     ' il form dovrà gestire le info di una persona
  8.     ' nome
  9.     Property FirstName As String
  10.         Get
  11.             Return txtFirstName.Text.Trim
  12.         End Get
  13.         Set(value As String)
  14.             txtFirstName.Text = value
  15.         End Set
  16.     End Property
  17.     ' cognome
  18.     Property LastName As String
  19.         Get
  20.             Return txtLastName.Text.Trim
  21.         End Get
  22.         Set(value As String)
  23.             txtLastName.Text = value
  24.         End Set
  25.     End Property
  26.     ' sesso
  27.     Property Sex As SexType
  28.         Get
  29.             If rbFemale.Checked Then
  30.                 Return SexType.Female
  31.             End If
  32.             Return SexType.Male
  33.         End Get
  34.         Set(value As SexType)
  35.             If value = SexType.Male Then
  36.                 rbMale.Checked = True
  37.             Else
  38.                 rbFemale.Checked = True
  39.             End If
  40.         End Set
  41.     End Property
  42.     ' data di nascita
  43.     Property Birthday As DateTime
  44.         Get
  45.             Return dateBirth.Value
  46.         End Get
  47.         Set(value As DateTime)
  48.             dateBirth.Value = value
  49.         End Set
  50.     End Property
  51.     ' città di nascita
  52.     Property CityOfBirth As String
  53.         Get
  54.             Return txtCity.Text.Trim
  55.         End Get
  56.         Set(value As String)
  57.             txtCity.Text = value
  58.         End Set
  59.     End Property
  60. #End Region
  61.  
  62.     Private Function ValidationRule() As Boolean
  63.         Return Not String.IsNullOrEmpty(FirstName) AndAlso
  64.             Not String.IsNullOrEmpty(LastName) AndAlso
  65.             Not String.IsNullOrEmpty(CityOfBirth)
  66.     End Function
  67.  
  68.     Private Sub CalcFC(sender As Object, e As EventArgs) Handles btnCalc.Click
  69.         If Not ValidationRule() Then
  70.             MessageBox.Show("Attenzione, mancano dei dati!")
  71.         Else
  72.             Dim p As New Person() With {
  73.             .FirstName = FirstName,
  74.             .LastName = LastName,
  75.             .Birthday = Birthday,
  76.             .CityOfBirth = CityOfBirth,
  77.             .Sex = Sex
  78.             }
  79.             lblFiscalCode.Text = p.GetFiscalCode()
  80.         End If
  81.     End Sub
  82.  
  83.     Private Sub ValidationCheck(sender As Object, e As EventArgs) Handles txtLastName.TextChanged, txtFirstName.TextChanged, txtCity.TextChanged
  84.         btnCalc.Enabled = ValidationRule()
  85.     End Sub
  86.  
  87.     Private Sub txtFirstName_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtFirstName.Validating
  88.         If String.IsNullOrEmpty(FirstName) Then
  89.             ep.SetError(txtFirstName, "Inserire il nome.")
  90.             e.Cancel = True
  91.         Else
  92.             ep.SetError(txtFirstName, "")
  93.         End If
  94.     End Sub
  95.  
  96.     Private Sub txtLastName_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtLastName.Validating
  97.         If String.IsNullOrEmpty(LastName) Then
  98.             ep.SetError(txtLastName, "Inserire il cognome.")
  99.             e.Cancel = True
  100.         Else
  101.             ep.SetError(txtLastName, "")
  102.         End If
  103.     End Sub
  104.  
  105.     Private Sub txtCity_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtCity.Validating
  106.         If String.IsNullOrEmpty(CityOfBirth) Then
  107.             ep.SetError(txtCity, "Inserire la città di nascita.")
  108.             e.Cancel = True
  109.         Else
  110.             ep.SetError(txtCity, "")
  111.         End If
  112.     End Sub
  113.  
  114. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement