Question how do I get my do/while to repeat once with each button click

Nice_Guy

New member
Joined
Oct 17, 2009
Messages
1
Programming Experience
Beginner
I have a simple program that takes the number of students and calc's the avg. It is suppose to add one student,(female, male,all) with each button click and update the avg.

I have a loop set up to do this but I don't know how to get my do/while to work once, each time I press my calc button or can't it be done this way?

I'll try to past my code so far:

Public Class frmMain

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'calculate the grade average
Dim dblGPA As Double

Dim dblFemaleGPA As Double
Dim dblCounterFemale As Double
Dim dblAccumulatorFemaleAVG As Double
Dim dblMaleGPA As Double
Dim dblCounterMale As Double
Dim dblAccumulatorMaleAVG As Double
Dim dblCounterStudents As Double

'for GPA boxes
Dim dblAllStudentsGPA As Double
Dim dblMaleStudentsGPA As Double
Dim dblFemaleStudentsGPA As Double
Double.TryParse(lblAllStudentsGPA.Text, dblAllStudentsGPA)
Double.TryParse(lblMaleStudentsGPA.Text, dblMaleStudentsGPA)
Double.TryParse(lblFemaleStudentsGPA.Text, dblFemaleStudentsGPA)

'for GPA total counter boxes
Dim dblAllStudentsGPAcounter As Double
Dim dblMaleGPAcounter As Double
Dim dblFemaleGPAcounter As Double
Double.TryParse(lblAllStudentsGPAcounter.Text, dblAllStudentsGPAcounter)
Double.TryParse(lblMaleGPAcounter.Text, dblMaleGPAcounter)
Double.TryParse(lblFemaleGPAcounter.Text, dblFemaleGPAcounter)

'for total student counter boexs
Dim dblAllStudentsCounter As Double
Dim dblMaleCounter As Double
Dim dblFemaleCounter As Double
Double.TryParse(lblAllStudentsCounter.Text, dblAllStudentsCounter)
Double.TryParse(lblMaleCounter.Text, dblMaleCounter)
Double.TryParse(lblFemaleCounter.Text, dblFemaleCounter)


do while '(condition???????????????????)
Select Case True

Case radFemale.Checked()
dblGPA = Convert.ToDouble(lstGPA.SelectedItem)
dblFemaleGPA = dblGPA
dblCounterFemale = dblCounterFemale + 1
dblAccumulatorFemaleAVG = dblAccumulatorFemaleAVG + dblGPA

Case radMale.Checked()
dblGPA = Convert.ToDouble(lstGPA.SelectedItem)
dblMaleGPA = dblGPA
dblCounterMale = dblCounterMale + 1
dblAccumulatorMaleAVG = dblAccumulatorMaleAVG + dblGPA

End Select

dblCounterStudents = dblCounterFemale + dblCounterMale
dblAllStudentsGPA = dblAccumulatorFemaleAVG + dblAccumulatorMaleAVG

'display amount in GPA boxes
lblAllStudentsGPA.Text = (dblAllStudentsGPA / dblCounterStudents).ToString("N1")
lblMaleStudentsGPA.Text = (dblAccumulatorMaleAVG / dblCounterMale).ToString("N1")
lblFemaleStudentsGPA.Text = (dblAccumulatorFemaleAVG / dblCounterFemale).ToString("N1")

'calculate & display amount in total GPA counter boxes
dblAllStudentsGPAcounter = dblAllStudentsGPA
dblMaleGPAcounter = dblAccumulatorMaleAVG
dblFemaleGPAcounter = dblAccumulatorFemaleAVG

lblAllStudentsGPAcounter.Text = dblAllStudentsGPAcounter.ToString("N1")
lblMaleGPAcounter.Text = dblMaleGPAcounter.ToString("N1")
lblFemaleGPAcounter.Text = dblFemaleGPAcounter.ToString("N1")

'calculate & display amount in total students counter boxes
dblAllStudentsCounter = dblCounterStudents
dblMaleCounter = dblCounterMale
dblFemaleCounter = dblCounterFemale

lblAllStudentsCounter.Text = dblAllStudentsCounter.ToString
lblMaleCounter.Text = dblMaleCounter.ToString
lblFemaleCounter.Text = dblFemaleCounter.ToString


loop'(condition???????????????????)

End Sub

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radFemale.CheckedChanged

End Sub

Private Sub lblMaleStudentsGPA_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblMaleStudentsGPA.Click

End Sub

Private Sub lstGPA_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub

Private Sub lstAVG_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstGPA.SelectedIndexChanged
'clear the calculated results

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' fill in the list box with values
radFemale.PerformClick()
For dblRate As Double = 1.0 To 4.1 Step 0.1
lstGPA.Items.Add(dblRate.ToString("N1"))
Next dblRate

lstGPA.SelectedIndex = 0
End Sub
End Class
 
The answer is that you don't. A loop loops. It doesn't do something and then wait for the user to click a button to do something else. You don't use a loop for that. You want one item added each button click so that's exactly what you do: add one item on the button click. If the user clicks the button 10 times 10 items will get added. Get rid of the loop altogether.

Here's a simple example. Create a new WinForms project and add a Button to the form, then add this code:
VB.NET:
Dim times As New List(Of Date)

Private Sub Button1_Click(ByVal sender As System.Object, _
                          ByVal e As System.EventArgs) Handles Button1.Click
    Me.times.Add(Date.Now)

    MessageBox.Show(Me.times.Count.ToString())
End Sub
Run the project and click the Button a few times. As you can see, an item is added to the collection each time the Button is clicked and there's no loop.
 
Back
Top