question

Hekzdaddy-3952 avatar image
0 Votes"
Hekzdaddy-3952 asked karenpayneoregon edited

Input Validation

Question when writing the function below, I am not getting my MessageBox.Show doesnt show. However if one box is checked it will show. and if both are selected the lblLabor will display properly. What am I doing wrong? Also, is this "validating a function"? Do I need the '"DoInputValidation()"?


Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If chkBoxOilChange.Checked And chkBoxLubeJob.Checked = False Then

         MessageBox.Show("you must select an item")

     ElseIf chkBoxOilChange.Checked Or chkBoxLubeJob.Checked = True Then
         DoInputValidation()
         lblLabor.Text = (dblLubeJob + dblOilChange).ToString("c")

     End If
     'Create ValidateInputFields function here.
 End Sub

Thanks,
Hector B.

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered Hekzdaddy-3952 commented

If the objective is to persist data to a database as an example and there is a Save button, then you could do all validation in that event as an example before the save of data is done.

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

the objective is to make the user have to pick items, if not picked, an error MessageBox will appear.

0 Votes 0 ·
Viorel-1 avatar image
0 Votes"
Viorel-1 answered Viorel-1 edited

Maybe you need something like this:

 Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
    
    If Not chkBoxOilChange.Checked AndAlso Not chkBoxLubeJob.Checked Then
    
       MessageBox.Show("you must select an item")
     
       Return
    
    End If
    
    lblLabor.Text = (dblLubeJob + dblOilChange).ToString("c")
    
  End Sub

And probably dblLubeJob and dblOilChange are calculated based on these checkboxes. Show some details.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JorgeTolaba-7552 avatar image
0 Votes"
JorgeTolaba-7552 answered

@Hekzdaddy-3952 The message does not appear because the "checkBox Change.Checked" has a value of true by default. You should assign it a false for the condition to be met.

  Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
             If chkBoxLubeJob.Checked = False And chkBoxOilChange.Checked = False Then
                 MessageBox.Show("You must select an Item.", "Informacion")
             ElseIf chkBoxOilChange.Checked Or chkBoxLubeJob.Checked Then
                 lblLabor.Text = (chkBoxLubeJob.Text + " " + chkBoxOilChange.Text).ToString()
             End If
     End Sub

79013-1.png



1.png (5.2 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered karenpayneoregon edited

Hello,

Another approach is to use the CheckedChange event as done in this mock-up. In DoValidation you can if needed use Double.TryParse for validating a string can be converted to a Double.

Note I didn't put a MessageBox in but you can replace the Debug.WriteLine with a messageBox.

 Public Class Form1
     Private _checkboxList As List(Of CheckBox) = New List(Of CheckBox)
     Private dblLubeJob As Double = 10
     Private dblOilChange As Double = 5
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
      
         _checkboxList = Controls.OfType(Of CheckBox).ToList()
    
         For Each checkBox As CheckBox In _checkboxList
             AddHandler checkBox.CheckedChanged, AddressOf chk_CheckedChanged
         Next
    
     End Sub
    
     Private Sub chk_CheckedChanged(sender As Object, e As EventArgs)
    
         lblLabor.Text = ""
    
         If _checkboxList.Any(Function(cb) cb.Checked) Then
             DoInputValidation()
         Else
             Debug.WriteLine("Nothing checked")
         End If
    
     End Sub
     Private Sub DoInputValidation()
         lblLabor.Text = (dblLubeJob + dblOilChange).ToString("c")
     End Sub
 End Class

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.