Basic Calculation

solfinker

Well-known member
Joined
Aug 6, 2013
Messages
71
Programming Experience
Beginner
Hello.
I want to perform the same lines of code whenever a control is modified in a form.
V.gr.:
VB.NET:
Public Class Form1
    Dim Inival As Double = NumUpDownInitial.Value
    Dim Finval As Double = NumUpDownFinal.Value
    Dim Result As Double  




Private Sub NumUpDownInitial_ValueChanged(sender As Object, e As EventArgs) Handles NumUpDownInitial.ValueChanged
sthchanged
End Sub
Private Sub NumUpDownFinal_ValueChanged(sender As Object, e As EventArgs) Handles NumUpDownFinal.ValueChanged
sthchanged
End Sub
...
sthchanged:
Inival = NumUpDownInitial.Value
Finval = NumUpDownFinal.Value
Result = Finval - Inival
...


Am I supposed to write the subroutine as a class, as a function, bellow a label?
Sth similar to this?
VB.NET:
Public Function sthchanged()
        Inival = NumUpDownInitial.Value
        Finval = NumUpDownFinal.Value
        Result = Finval - InivalEnd Function
End Function
Thank you for your patience.
 
Last edited:
You would put the code into a method that gets executed when any of the controls in question changes. You have two choices as to how to proceed. You can either write your own method and then call that from each individual event handler, or you can just write one event handler for all the controls.

Option 1:
Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    DoSomething()
End Sub

Private Sub NumericUpDown2_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown2.ValueChanged
    DoSomething()
End Sub

Private Sub DoSomething()
    'Do something here.
End Sub

Option 2:
Private Sub NumericUpDowns_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged,
                                                                                  NumericUpDown2.ValueChanged
    'Do something here.
End Sub

The second option obviously means less code and is probably the better option if you want to do exactly the same thing regardless of the control raising the event. If there's any difference between what you want to do for each control then you should go for the first option.

Note that you can still use the second option if you want to handle different events and/or events of different types of controls. You will just have to be sure to use the least derived common type for the 'e' parameter. That may well mean using EventArgs even if one or more of the events usually use a more derived type.

If you choose option 2, you can write the common event handler by hand if you want but you can also use the designer. In the Properties window, you can click the Events button to see events instead of properties. You can select one or more controls and then double-click an event to generate an event handler or select an existing event handler from the drop-down list.
 
If you choose option 2, you can write the common event handler by hand if you want but you can also use the designer. In the Properties window, you can click the Events button to see events instead of properties. You can select one or more controls and then double-click an event to generate an event handler or select an existing event handler from the drop-down list.
I had never seen this: thanks a lot!
 
Back
Top