Click here to Skip to main content
16,011,905 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all
i have 2 updown numeric controls in my form .i want to make a relate value between those controls,so that if i increase value of control1, control2 value increase too and inverse .
i use this code it's ok but when i decrease value of control1, the control2 increase value to instead of decrease .
VB
Private Sub updown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles updown1.ValueChanged
 
        Dim initialval1 As Integer = 1000
       If updown1.Value > initialval1 Then
           updown2.Value += 1
       End If
 
               If updown1.Value > initialval1 Then
           Exit Sub
       Else
           updown2.Value -= 1
       End If
   End Sub

please correct my code.
thanks
Posted
Comments
Philippe Mori 14-May-15 12:14pm    
Given that user can write number in the control, that code does not make any sence. What if the user erase first control and then enter an arbitrary number.

Um.
Had you noticed that your two tests are identical?
So your code basically amounts to:
if updown1.Value > 1000 
   increase updown2.Value
else
   decrease updown2.Value
So every time you change the value of the first updown and it's below 1000 you will reduce updown2.

It's possible that you want to reference updown2 in the second test - but I'm not sure what this code is meant to do.

And I suspect that you don't want increment or decrement at all - what if I paste the value 20000 into the first updown? Shouldn't the second change by more than one?
 
Share this answer
 
Comments
Philippe Mori 14-May-15 12:09pm    
Exactly.
I suggest the following :
Inside your "ValueChanged"-Script you check first the difference of the made changes. This could be done by comparing the actual value with a saved value. After the Compare you store the actual value inside the saved value.
Now you could increase or decrease the corresponding second value with the calculated changed instead of the fire Event which gives no Information about the amount of the Value-Change.

...
 
Share this answer
 
thank's for your reply
do you run my code?
it's works good but i can not find a way to synchronize value of 2 controls in decrease mode .
assume when i click to increase value of control 1 it ok

control1.value =100 + 101 + 102 ...
control2.value= 200 + 201 + 203 ...

but when i decrease value of control1 , control2 going to increase

control1.value = 102 - 101 - 100
control2.value = 204 + 205 - 206

sorry for my poor english
 
Share this answer
 
Comments
Philippe Mori 14-May-15 12:07pm    
This is not an answer. Update the question instead.

By the way your problem is clearly explained in solution 1.
unforgiven747 14-May-15 12:25pm    
oh,sorry. you are right, can you write code to solve my problem ?
Philippe Mori 14-May-15 13:00pm    
We cannot write code if you don't fix the specification. One way, that would works would be to write control2.Value = control1.Value + 100; Without other details, we cannot help you more.

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