English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Using an input box (lstInput) and a button (btnCompute) I need to click on the button and enter numbers one at a time in a listbox (lstOutput) it needs to be keeping a running total of the sum

When entering" -1", a grand total will be displayed


Heres my code
Public Class frmPositiveNumbers

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCompute.Click

Dim num, total As Double

Total = 0

Do until num = -1

Num = Me.txtInput.Text
Me.lstOutput.Items.Add(num)
total += num

Loop

Me.lstOutput.Items.Add(total)

End Sub
End Class

I keep ending in an infinite loop, I am sure its something stupid I am doing.

2007-11-28 11:22:35 · 4 answers · asked by Brian D 4 in Computers & Internet Programming & Design

4 answers

Why are you creating a loop? All you need is a simple If statement.

Something like this for the button click event.


If Me.txtInput.Text <> -1 then
Total = txtInput.text + Total
Me.lstOutput.Items.Add(txtInput.Text)
Else
Me.lstOutput.Items.Add(total)
End If

Declare total as a global variable. You don't want it resetting to 0 everytime the click event is called.

2007-11-28 11:34:59 · answer #1 · answered by Anonymous · 1 0

THREE BIG PROBLEMS:
1.) The textbox contains a string and the Num variable is a Double.

VB is attempting to convert whatever string is contained in the textbox into a number. There is no validation being done to ensure that:
1.) The user entered a number and not a letter
2.) That the user actually entered something. A null string "" will cause an error as VB will assume null is infinity and attempt to store it in a numeric variable which has size limits. A double can store a very large number but it is still a limit.

Modify your code to use the VAL function which converts strings into numbers.

Val(123A) = 123
Val(12A3) = 12
Val(A123) = 0

Val converts from left to right until it sees a text character. It returns what ever it has detected as a double data type.

2.) The Do Loop is constantly running adding what ever it sees in the textbox. The constant looping will add the same value over and over to the list box many times per second. The list box will grow non stop until the computer runs out of resources.

3.) The program is stuck in this event handler processing this non-stop DO loop. Even though the values are being added to the listbox the display is not being updated nor will you be able to enter data. This is becaue you are not allowing this event to finish and handle other events like adding text to teh list box and changing the text box.

Within the DO loop you can add a DoEvents

My.Application.DoEvents

This will allow the program to break away from your button click event and handle any other outstandings events which have fired while executing the Do loop. This will allow the form to update teh display and allow you to enter more data.



To fix these problems you need to modify your loop code so that if Num is Non zero you do the following:
1.) Add it to the lstOutput
2.) Totalize
3.) then set the contents of txtInput to zero to prevent repeated adding.

An IF/THEN/ELSE will work.
if Num <> 0 then
place code here to totalize and add to list and zero the input textbox

else
do events

end if

I assume that you are exploring how to use a do loop.

Tecnically you DO NOT need to use a loop in this application. The best way is to use the button as an accept button to add the entered value. This way the code only executes once per click.

2007-11-29 00:15:06 · answer #2 · answered by MarkG 7 · 0 0

You do not want a "do until" in there.
You will do one of the following 2 tasks.
1. check your input if not -1, add the inputted value to the listbox iterate through your listbox and add up the values.

2. check your input if -1 iterate through your listbox and add up the values. (not neccessary, it is already done above)

This should get you going in the right direction.

Actually since you always get a total after entering a value there is no need to enter a -1

2007-11-28 19:55:33 · answer #3 · answered by stratsandlespauls 6 · 0 0

take out the word loop!

2007-11-28 19:31:30 · answer #4 · answered by ? 2 · 0 0

fedest.com, questions and answers