Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Numeric Updown Control and a label it is connected to a label. What I wanted to do is when the label reaches 0 it should display a message like "You don't have enough points" and the NUD shouldn't be able to increment or decrement.. I got this part working but the message is popping twice. I know it's just a minor bug but I'm already trying to figure this out for hours.

What I have tried:

Below is my code to display a message when the label reaches 0 but the display message is popping twice which is not right.

VB
Private Sub numCategory_ValueChanged(sender As Object, e As EventArgs) Handles numCategory.ValueChanged


       If num <> 0 Then

           lblpoints.Text = (num - numCategory.Value).ToString

       Else

           numCategory.Value = 0
           MsgBox("No more points to allocate")


       End If

   End Sub
Posted
Updated 12-May-20 20:34pm

As RickZeeland pointed it out... below line is redundant and raises numCategory_ValueChanged event again!
VB
numCategory.Value = 0


As to the code... I'd change the body of event this way:
VB
num = numCategory.Value 'set value of NUD to "num" variable
If num = 0 Then MsgBox("No more points to allocate") 'display message
numCategory.Enabled = (num > 0) 'enable/disable NUD
lblpoints.Text = num.ToString()'display current value of NUD
 
Share this answer
 
v2
in the case zero is reached, either:

1. hide the numCategory control

or

2. set the Enabled Property of the numCategory control to 'false
 
Share this answer
 
Comments
Maciej Los 13-May-20 2:14am    
5ed!
The message comes twice because you change the value:
numCategory.Value = 0
So comment out this line, or use some construction with a boolean variable to skip the message.
 
Share this answer
 
Comments
Maciej Los 13-May-20 2:34am    
Good point!
lelouch_vi 2 13-May-20 6:58am    
Hi RickZeeland,

Yes I think that's because of that line which causes the ValuChange event to be fired twice, But without setting the NUD to zero and the user clicks up the label misbehaves. I guess the best solution for this is to set the NUD to false when the label reaches 0. Thanks anyway.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900