Home » VB.NET Basics » 07 - Loops
7

Loops and User Forms

Using Loops with User Forms

Awhile ago, a question was posed if it was possible to count the number of cups to fill a swimming pool. Now that you are equipped with the knowledge of loops, try making a program that can calculate the number of cups needed to fill a swimming pool. Make a user form similar to the one below.

You can make a program that can calculate this number without using loops but use loops now for exercise purposes. You will benefit from this later on.

Your code may look something like the one below.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click          'one liter is equivalent to .001 cubic meters OR m^3         'one cup is equivalent to approximately 230 milliliters OR .00023 m^3          Dim Depth As Double         Dim Length As Double         Dim Width As Double          Depth = TextBox1.Text         Length = TextBox2.Text         Width = TextBox3.Text          Dim Volume As Double          Volume = Depth * Length * Width          Dim Cups As Double         Dim Counter As Double          Counter = 0         Cups = 0          Do Until Counter > Volume              Cups = Cups + 1             Counter = Counter + 0.00023          Loop          Counter = Counter - 0.00023         Cups = Cups + (Volume - Counter) / 0.00023          TextBox4.Text = Cups      End Sub