Simultaneous update of textboxes


This example requires the information in three textboxes to be updated whenever any one is modified.

Given the diameter,D, of a circle, we could calculate the circumference,C, by the formula:
C=pi*D
and the area, A, by the formula:
A=pi*D*D/4

Conversely, given the circumference,C, we can calculate:
D=C/pi and A=C*C/(4*pi)

and finally, given the area,A, we can calculate:
D=sqrt(4*A/pi) and C=pi*sqrt(4*A/pi)

This has been accomplished by having a state-variable that informs other event-handlers that update is in progress.
    ' program Area of Circle               P.C. 2004-01-01
    ' This program calculates the related values of diameter,
    ' circumference and area as any one of the text boxes is
    ' modified.
    '
    ' Note that this program may not work perfectly where the
    ' locales parameter has reset the decimal point to a comma
    ' (,), whereby the val() function does not function properly
    '
    ' problem initially posted by Mykro on
    ' vbdotnetforums.com  Posted 12/6/2003 2:37 PM (GMT -5)
    Dim m_Busy As Boolean = False
    Private Sub txtDiameter_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDiameter.TextChanged
        If Not m_Busy Then
            m_Busy = True
            Dim dblDiameter As Double = Val(txtDiameter.Text)
            txtCircumference.Text = String.Format("{0:f6}", dblDiameter * Math.PI)
            txtArea.Text = String.Format("{0:f6}", dblDiameter * dblDiameter * Math.PI / 4)
            m_Busy = False
        End If
    End Sub

    Private Sub txtCircumference_TextChanged_x(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCircumference.TextChanged

        If Not m_Busy Then
            Dim dblCircumference As Double = Val(txtCircumference.Text)
            m_Busy = True
            txtDiameter.Text = String.Format("{0:f6}", dblCircumference / Math.PI)
            txtArea.Text = String.Format("{0:f6}", dblCircumference * dblCircumference / Math.PI / 4)
            m_Busy = False
        End If
    End Sub

    Private Sub txtArea_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtArea.TextChanged
        If Not m_Busy Then
            m_Busy = True
            Dim dblArea = Val(txtArea.Text)
            txtDiameter.Text = String.Format("{0:f6}", Math.Sqrt(dblArea / Math.PI) * 2)
            txtCircumference.Text = String.Format("{0:f6}", Math.Sqrt(dblArea / Math.PI) * 2 * Math.PI)
            m_Busy = False
        End If
    End Sub


Project Files updated 2004-01-01