KagaminLen

form 1 vb

Oct 24th, 2019
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 2.83 KB | None | 0 0
  1. Public Class NumericalStatistic
  2.     'Disable buttons if there is no/not enough input'
  3.     Private Sub ValueTextBox_TextChanged(sender As Object, e As EventArgs) Handles ValueTextBox.TextChanged
  4.         ConfirmButton.Enabled = True
  5.         ClearButton.Enabled = True
  6.         'Confirm button disabled if less than 2 input'
  7.         If ValueTextBox.Lines.Length < 2 Then
  8.             ConfirmButton.Enabled = False
  9.         End If
  10.         'Clear Button disabled if no input'
  11.         If ValueTextBox.Text = "" Then
  12.             ClearButton.Enabled = False
  13.         End If
  14.     End Sub
  15.  
  16.     Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.Click
  17.         Dim userReply As Integer
  18.         userReply = MsgBox("Are you sure you want to clear?", MsgBoxStyle.Exclamation + MsgBoxStyle.OkCancel, "CLEAR")
  19.         'Clears ValueTextBox if user selects Ok'
  20.         If userReply = MsgBoxResult.Ok Then
  21.             ValueTextBox.Text = ""
  22.         End If
  23.     End Sub
  24.  
  25.     Private Sub ConfirmButton_Click(sender As Object, e As EventArgs) Handles ConfirmButton.Click
  26.         'Check if input is numeric before moving to the next result form'
  27.         If CheckMultilineTextIsNumeric(ValueTextBox) Then
  28.             AnswerNumericalStatistic.Show()
  29.         End If
  30.     End Sub
  31.  
  32.     'Function to check wether the input is numeric, remove error lines if user wishes'
  33.     Function CheckMultilineTextIsNumeric(ByRef theTextBox As TextBox) As Boolean
  34.         'Checks user input for non numeric input'
  35.         For i As Integer = 0 To theTextBox.Lines.Length - 1
  36.             If Not IsNumeric(theTextBox.Lines(i).Trim) Then
  37.                 Dim userReply As Integer
  38.                 userReply = MsgBox("The value text box should contain only numbers!" & vbNewLine & "Do you want to delete the error lines?", MsgBoxStyle.Exclamation + MsgBoxStyle.OkCancel, "ERROR!")
  39.  
  40.                 If userReply = MsgBoxResult.Ok Then
  41.                     Dim dummyTextBox(theTextBox.Lines.Length) As String
  42.                     Dim count As Integer = 0
  43.                     For j As Integer = 0 To theTextBox.Lines.Length - 1
  44.                         If IsNumeric(theTextBox.Lines(j).Trim) Then
  45.                             'Creates an array of user input that doesn't include the input errors
  46.                             dummyTextBox(count) = theTextBox.Lines(j)
  47.                             count += 1
  48.                         End If
  49.                     Next
  50.                     Array.Resize(Of String)(dummyTextBox, count)
  51.                     'resize to get rid of the (will be) empty lines
  52.                     Array.Resize(Of String)(theTextBox.Lines, count)
  53.                     theTextBox.Lines = dummyTextBox
  54.                 End If
  55.                 Return False
  56.             End If
  57.         Next
  58.         'If no input error'
  59.         Return True
  60.     End Function
  61. End Class
Add Comment
Please, Sign In to add comment