VB.net Problem

vb.net Kenya
  • 13 years ago
    I am currently studying to be an ICT secondary teacher and as part of my training we have to complete a computer programming module. We have been using Visual Basic.net to solve problems. I have a problem which i can not solve and would be glad of an advice anybody could give.Problem (simple enough) - A disco can hold 500 people.  Allow the user to keep entering the number of people in each group as the group comes through the door.  Display the running total and how many people are allowed in before it becomes full.  When the running total reaches exactly 500, display a message that the disco is full, or if 500 would be exceeded, a message that the current group of people cannot go in.Analysis:Input:Ask user to enter Number - Store number (variable called EnterNumb)Process:Subtract EnterNum from 500 (MaxNumber)The remaining number of spaces is displayed in NumSpacesleftMessage to appear – Disco Full if = 50 or if number >500, message to appear current group can not go in, to keep looping until total = 500 or no more can fit in.Print Answer.So the code i have so far is: (it does not work - anybody help?)

     

    Dim Number, Result, Maxnumber As Short

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

    Number = 0 'written by Theresa Russell Dec 07

    Result = 0 'sets Number and Results to zero to start with

    Maxnumber = 500 'maximum number allowed in disco

     

    Do

    Number = txtEnterNumb.Text 'number entered by user

    Result = Number + Result 'Result = current total

    txtCurrentTotal.Text = Result

    txtNumSpacesLeft.Text = Maxnumber - Result ' 500 - total number of people enter disco

    Loop Until Result >= 500

    If Result = 500 Then

    MsgBox("Disco Full - no more allowed")

     

     

    ElseIf Result + Number > 500 Then

    MsgBox("Too many in the group")

    Result = Result - Number

    txtCurrentTotal.Text = (Maxnumber - Result)

    txtNumSpacesLeft.Text = Result

    End If

    End Sub

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

    Dim Message As String = "Are you sure you want to exit?"

    Dim Caption As String = "Disco"

    Dim Buttons As MessageBoxButtons = MessageBoxButtons.YesNo

    Dim Result As DialogResult Result = MessageBox.Show(Me, Message, Caption, Buttons)

    If Result = DialogResult.Yes Then

    Me.Close()

    End If

    End Sub

    End Class

     

     

  • 13 years ago

    You have a DO...LOOP in the first button click.

    So once you click, it keeps using the same input and goes straight up to 500 without allowing any new input.

    Your process map says (I paraphrase)  'take an input, store it, see what the effect would be if you add it to the current running total, do something in response.'

    So you need an input text box , a text box or label to show 'running total', and a way of saying 'sorry folks, no entry' if processing the input would get you over 500 people.

    ----------------------------------------------------------------------------

    In the Form Load event (so this happens once per run)

     set the RunningTotal variable to 0

    ---------------------------------------------------------------------------

    In the Button Click event:

    , take the value from the text box [....use Val(TextBox1.text)]

    and store it in a variable called (eg) nThisPartySize

     

    If RunningTotal + nThisPartySize > 500 then

    'Display a message refusing entry

    Else

    RunningTotal = RunningTotal + nThisPartySize

    end if

    ----------------------------------------

    ...and thats probably all you need...

     

  • 13 years ago

    Hi

     It doesnt seem to matter what number you add  - it says the disco is full and populates the current total as 500 and number of places left as 0.

    Obviously something wrong and i can not work it out - have had another reply - will have a look and see if it help.

     

    Thanks for your time

     Theresa

  • 13 years ago

    It works perfectly - i was advised to use a DO...LOOP and it just kept looping until it reached 500.

    Many thanks for this - my lecturer couldn't solve the problem and was trying to bypass it!

     Have a great Christmas and New Year

    I can finally get this portfolio finished and move onto Web Authoring and Networking revision.

     Many thanks again to everyone who took the time to help

     

    Theresa

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity.” - Dennis Ritchie