ClarusDignus

Consolidating repitious event handling.

Oct 29th, 2014
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.    'Check for correct input.
  2.    Public Sub Validate_Realtime(txtObj As Object, lblObj As Object, countText As Boolean, numCheck As Boolean, txtEvent As String)
  3.  
  4.         'Output Sub paramaters as message box. For debugging only.
  5.        'Dim outputParamTest1 As String = "Text Object = " + Convert.ToString(txtObj.Name) + " | Label Object =" + Convert.ToString(lblObj.Name) + " | Count text? = " + Convert.ToString(countText) + " | Check for numeric? = " + Convert.ToString(numCheck) + " | Text Event = " + txtEvent
  6.        'MessageBox.Show(outputParamTest1)
  7.  
  8.         If txtEvent = "Enter" Or txtEvent = "Changed" Then
  9.             If numCheck = True Then
  10.                 'Check for numeric input.
  11.                If Not IsNumeric(txtObj.Text) And txtObj.Text.Length > 0 Then
  12.                     ErrorAppPIN.SetError(txtObj, "Numeric input only!")
  13.                 Else
  14.                     ErrorAppPIN.SetError(txtObj, "")
  15.                 End If
  16.                 'Check for correct input.
  17.                If IsNumeric(txtObj.Text) And txtObj.Text.Length = txtObj.MaxLength Then
  18.                     SuccessAppPIN.SetError(txtObj, "Acceptable!")
  19.                 Else
  20.                     SuccessAppPIN.SetError(txtObj, "")
  21.                 End If
  22.             Else
  23.                 'Check for correct input.
  24.                If txtObj.Text.Length = txtObj.MaxLength Then
  25.                     SuccessAppPIN.SetError(txtObj, "Acceptable!")
  26.                 Else
  27.                     SuccessAppPIN.SetError(txtObj, "")
  28.                 End If
  29.             End If
  30.             If countText = True Then
  31.                 'Show count of text length and max length.
  32.                'Text length.
  33.                Dim txtObjCount As String = txtObj.text.length
  34.                 'Max length.
  35.                Dim txtObjMax As String = txtObj.maxlength
  36.                 'String for label.
  37.                Dim lblObjString As String = "[ " + txtObjCount + " / " + txtObjMax + " ]"
  38.                 lblObj.text = lblObjString
  39.                 'Display label.
  40.                lblObj.Visible = True
  41.             End If
  42.         ElseIf txtEvent = "Leave" Then
  43.             'Hide label.
  44.            lblObj.Visible = False
  45.             If txtObj.Text.Length < txtObj.MaxLength And txtObj.Text.Length > 0 Then
  46.                 ErrorAppPIN.SetError(txtObj, "Not enough digits!")
  47.             ElseIf txtObj.Text.Length = 0 Then
  48.                 ErrorAppPIN.SetError(txtObj, "Input required!")
  49.             Else
  50.                 ErrorAppPIN.SetError(txtObj, "")
  51.             End If
  52.         End If
  53.     End Sub
  54.  
  55.     'When a text box is entered (i.e. tabbed/clicked into).
  56.    Private Sub TextBox_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAppNum.Enter, txtPIN.Enter
  57.         Dim txtevent As String = "Enter"
  58.         'Determine active text box.
  59.        Dim txtactive As TextBox
  60.         txtactive = CType(sender, TextBox)
  61.         If (txtactive Is txtAppNum) Then
  62.             Validate_Realtime(txtAppNum, lblCountAppNum, True, True, txtevent)
  63.         ElseIf (txtactive Is txtPIN) Then
  64.             Validate_Realtime(txtPIN, lblCountPIN, True, True, txtevent)
  65.         End If
  66.     End Sub
  67.  
  68.     'When a text box's text is changed.
  69.    Private Sub textbox_textchanged(sender As Object, e As EventArgs) Handles txtAppNum.TextChanged, txtPIN.TextChanged
  70.         Dim txtevent As String = "changed"
  71.         'Determine active text box.
  72.        Dim txtactive As TextBox
  73.         txtactive = CType(sender, TextBox)
  74.         If (txtactive Is txtAppNum) Then
  75.             Validate_Realtime(txtAppNum, lblCountAppNum, True, True, txtevent)
  76.         ElseIf (txtactive Is txtPIN) Then
  77.             Validate_Realtime(txtPIN, lblCountPIN, True, True, txtevent)
  78.         End If
  79.     End Sub
  80.  
  81.     'When Application # text box left (i.e. tabbed/clicked out of).
  82.    Private Sub TxtAppNum_Leave(sender As Object, e As EventArgs) Handles txtAppNum.Leave, txtPIN.Leave
  83.         Dim txtEvent As String = "Leave"
  84.         'Determine active text box.
  85.        Dim txtActive As TextBox
  86.         txtActive = CType(sender, TextBox)
  87.         If (txtActive Is txtAppNum) Then
  88.             Validate_Realtime(txtAppNum, lblCountAppNum, True, True, txtEvent)
  89.         ElseIf (txtActive Is txtPIN) Then
  90.             Validate_Realtime(txtPIN, lblCountPIN, True, True, txtEvent)
  91.         End If
  92.     End Sub
Advertisement
Add Comment
Please, Sign In to add comment