0

I have a groupbox with 5 NUD controls. I am trying to figure out if there is a way to limit the TOTAL value of all of the NUD combined, and to set a restriction to where it has a minimal amount required of them all combined. I checked my book for school and cannot seem to find it in there nor is google providing me with the answer I seek. This is not a requirement of my project, I just thought the extra touch to it would be nice. Any help would be appreciated. Thanks.

EDIT:

I tried using an array but wasn't getting the results I was looking for, still searching around to find a way for people to not able to increase the value of said NUD's when limit is reached. This is what I currently am doing and just don't like the way its handled, I would prefer them not to press a button to find out if the total is more than the requirement.

        If numForest.Value + numFountain.Value + numConstruct.Value + numIntercept.Value + numWaterFall.Value > 30 Then
        MessageBox.Show("Total photo prints needs to be below 30")
    Else : lstPhotoOrder.Items.Add(txtOrderName.Text)
1
  • 1
    yes there is a way: its called writing code. Use the the Enter and ValueChanged events Commented Jan 20, 2014 at 19:04

1 Answer 1

1

As Plutonix hinted, you can use the ValueChanged event to examine the current value of the NumericUpDown controls. The problem is, it doesn't tell you what the previous value was to fall back to, so try storing those values in a dictionary:

Private oldNums As New Dictionary(Of NumericUpDown, Decimal)
Private revertingValue As Boolean = False

Private Sub num_ValueChanged(sender As Object, e As EventArgs) Handles _
                                       numForest.ValueChanged, _
                                       numFountain.ValueChanged, _
                                       numConstruct.ValueChanged, _
                                       numIntercept.ValueChanged, _
                                       numWaterFall.ValueChanged
  If Not revertingValue Then
    Dim numControl As NumericUpDown = sender
    If Not oldNums.ContainsKey(numControl) Then
      oldNums.Add(numControl, numControl.Minimum)
    End If

    If numForest.Value + numFountain.Value + numConstruct.Value + _
       numIntercept.Value + numWaterFall.Value > 30 Then
      MessageBox.Show("Total photo prints needs to be below 30")
      revertingValue = True
      Try
        numControl.Value = oldNums(numControl)
      Catch ex As Exception
      Finally
        revertingValue = False
      End Try
      numControl.Select()
    Else
      oldNums(numControl) = numControl.Value
    End If
  End If
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.