3 Replies - 345 Views - Last Post: 26 April 2019 - 06:18 AM Rate Topic: -----

#1 VeX85   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-March 19

Two NumericUpDown - average value

Posted 25 April 2019 - 04:02 AM

Hi,
I need help, how I can calculate the average value of two NumericUPDown and show it in Label ?

Public Class mail_premia
    Dim fPreviousValue As Single = 0

    Private Sub m_p_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        With NumericUpDown1
            .DecimalPlaces = 1
            .Increment = 0.5
            .Maximum = 20
            .Minimum = 0
            fPreviousValue = .Value
        End With
        With NumericUpDown2
            .DecimalPlaces = 1
            .Increment = 0.5
            .Maximum = 20
            .Minimum = 0
            fPreviousValue = .Value
        End With
    End Sub

    Private Sub Label1_Click(sender As System.Object, e As System.EventArgs) Handles Label1.TextChanged

    'here the average value

    End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Two NumericUpDown - average value

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1733
  • View blog
  • Posts: 5,710
  • Joined: 25-September 09

Re: Two NumericUpDown - average value

Posted 25 April 2019 - 05:27 AM

You should be able to perform the math against the value of each selected number in the NUD boxes and divide by the number of boxes involved. What are you trying to do with fPreviousValue?

I would also remove the sub you're showing and combine the handlers for the ValueChanged event of the NumericUpDown control. Then inside that set the label's .Text property to the calculated result. This way, as you change one or the other, the label will update with the new value.

This post has been edited by CharlieMay: 25 April 2019 - 05:30 AM

Was This Post Helpful? 2
  • +
  • -

#3 andrewsw   User is offline

  • quantum multiprover
  • member icon

Reputation: 6792
  • View blog
  • Posts: 28,050
  • Joined: 12-December 12

Re: Two NumericUpDown - average value

Posted 25 April 2019 - 05:28 AM

Handles Label1.TextChanged This doesn't seem right.

In whichever event you will use you first need to read the two values from the controls, have you discovered how to read a value from a numericupdown?
Was This Post Helpful? 1
  • +
  • -

#4 VeX85   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 4
  • Joined: 15-March 19

Re: Two NumericUpDown - average value

Posted 26 April 2019 - 06:18 AM

fPreviousValue I needed to test the old code, it is unnecessary.

Thank you for your hints. Good code below.

Public Class mail_premia
    Dim a, b As Integer

Private Sub m_p_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        With NumericUpDown1
            .DecimalPlaces = 1
            .Increment = 0.5
            .Maximum = 20
            .Minimum = 0
        End With
        With NumericUpDown2
            .DecimalPlaces = 1
            .Increment = 0.5
            .Maximum = 20
            .Minimum = 0
        End With

        AddHandler NumericUpDown1.ValueChanged, AddressOf Average
        AddHandler NumericUpDown2.ValueChanged, AddressOf Average

    End Sub

Private Sub Average(sender As System.Object, e As System.EventArgs)
        Label1.Text = ((Val(a) + Val(B)/>) / 2)
    End Sub

    Private Sub NumericUpDown1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NumericUpDown1.ValueChanged
        a = NumericUpDown1.Value
    End Sub

    Private Sub NumericUpDown2_ValueChanged(sender As System.Object, e As System.EventArgs) Handles NumericUpDown2.ValueChanged
        b = NumericUpDown2.Value
    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1