Adding previously selected items from listbox for a total? - vb.net

I'm doing this program for a class; I have a listbox with values from 0-10 which are 'scores' which get recorded every time the user selects a new score (and clicks the button of course). I need to do the following;
an average of the scores, which I get through an accumulator and the counter
a number of scores, which I get through a counter
get the total scores (this is what's giving me trouble)
here's the code I have for this so far :
intScore = Convert.ToDecimal(lstScores.SelectedItem)
'the counter and accumulator
intTotal = intTotal + 1
decScoreAccumulator += intScore
' here's what calculates the average
If intScore > 0 Then
decScorAvg = decScoreAccumulator / intTotal
End If
Here's an image of it : http://i.stack.imgur.com/aomQO.png
The number and average I can get to work, since its just using the accumulator and the counter. But no matter what I do, I can't add the values selected for the total. So does anyone know how I can get it to give me total of all the selected scores?

You're code looks good, and you look like your doing everything correctly. You even already have the total in your current code:
decScoreAccumulator += intScore
Because you're adding the score each time to decScoreAccumulator, this variable has the total you're looking for (assuming it's not confined to the scope of the button click). HTH. Good luck!

Related

Sum of the first 100 even and odd number?

I need some help getting this code to work. I need the code to calculate the first 100 even and odd numbers. The code below shows the even portion, but I assume they both will work the same way, just with different numbers. I know that the first 100 even numbers are 2 to 200 or 0 to 200?, and the first 100 odd numbers are 1 to 199. (I also need to use while loops, would I use a separate while loop to determine how the code calculate the sum?)
here is the code.
Dim sum, count As Integer 'the sum and count are changing and they must be a whole number.
Const num As Integer = 2
'now we must deveolp a while loop/equation that adds every number lower or equal to 100 together.
While count Mod 2 <= 200 'while the count is smaller or equal to 100,
count =
sum += count 'The sum will be add or equaled to the count
count += 1 'The count will be added to equaled to 1
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sum) 'This is the text the user sees, which tells them that the sum of the first 100 numbers will be 5050.
Console.ReadLine()
End Sub
As usual the math portion is giving me trouble. I feel like I have the right idea, but I cant execute it properly.
Dim sumEven, sumOdd As Integer
Dim even = 2
Dim odd = 1
While even <= 200 AndAlso odd <= 200
sumEven += even
sumOdd += odd
even += 2
odd += 2
End While
Console.WriteLine("The Sum of the first 100 even numbers is: {0}!", sumEven)
Console.WriteLine("The Sum of the first 100 odd numbers is: {0}!", sumOdd)

Adding items in a function to get a sum

I'm trying to build a pizza application and for the sides, based on what's selected they need to be added up and put into a total. I'm using a function to do this, but its only returning the value from the first selection I make in the combo box, and doesn't go through the if to continue to add the remaining values, I'm confused as to why its doing this. My code for the function is below.
Function sides() As Decimal
Dim total As Decimal
If cmboSides.SelectedIndex.Equals(0) Then
total = total + 4.99
ElseIf cmboSides.SelectedIndex.Equals(1) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(2) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(3) Then
total = total + 5.99
ElseIf cmboSides.SelectedIndex.Equals(4) Then
total = total + 6.99
ElseIf cmboSides.SelectedIndex.Equals(5) Then
total = total + 7.99
End If
Return total
End Function
The specific issue here is that you are declaring total inside that method. As a result, it starts at zero every time you call the method so you only get the most recent value. If you expect the running total to be used each time then its value needs to persist between calls to that method, which means that it must be declared outside that method:
Private total As Decimal
Function sides() As Decimal
If cmboSides.SelectedIndex.Equals(0) Then
total += 4.99D
ElseIf cmboSides.SelectedIndex.Equals(1) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(2) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(3) Then
total += 5.99D
ElseIf cmboSides.SelectedIndex.Equals(4) Then
total += 6.99D
ElseIf cmboSides.SelectedIndex.Equals(5) Then
total += 7.99D
End If
Return total
End Function
There are various other things you could do to improve that code too but this addresses the specific issue that you're asking about.
Store each price as a value then use
Dim total As Decimal
For Each item In lstSides.SelectedItems
total+= CDbl(lstSides.GetItemValue(item))
Next
The most basic solution, assuming you are actually using a ListBox (rather than a ComboBox which can't be set to multiselect), is this:
Function sides() As Decimal
Dim total As Decimal
If lstSides.SelectedIndices.Contains(0) Then total += 4.99
If lstSides.SelectedIndices.Contains(1) Then total += 6.99
Etc
Return total
End Function
Your current code will never move to a second ElseIf after one has been evaluated as true, similar to a Case Select statement.
A much better solution would be to use data binding to a class of Pizza with properties such a Price, Availability, Size, etc.

How Do I Count When Numbers Do Not Show Up

I have numbers from 1 to 25, four numbers will show up daily. I need to put a +1 on each of the four numbers and need to put a -1 on each of the 21 numbers didn't show up.
The four numbers that come up daily will be inputted in four different text boxes. The count being positive or negative needs to go on 25 separate text boxes labeled 1 thru 25.
I have tried "if textbox <> number, then count -= 1" but I get a count of -4 because it doesn't see the number in any of the four text boxes.
I only need a daily count not a textbox count. Sorry I don't have any code started and would greatly appreciate if someone can point me in the right direction. I'm doing this on Visual Studio 2012.
Thank you all for responding. Here is some code I've started but the count is not correct. My four input text boxes are in GroupBox2. Four numbers from 1 to 25 will draw daily like a lottery. The four numbers drawn will have a value of +1 each all others -1. I need to find the age of each number 1 thru 25. If a number has a +3 then that means that number has drawn 3 consecutive days. If a number has a -15 then that means that number has not drawn for the past 15 days.
Dim tb As New TextBox
Dim ctrl As Control
Dim Counter As Integer
For Each ctrl In GroupBox2.Controls
tb = ctrl
If tb.Text = 1 Then
Counter += 1
ElseIf tb.Text <> 1 Then
Counter -= 1
TextBox464.Text = Counter
End If
If tb.Text = 2 Then
Counter += 1
ElseIf tb.Text <> 2 Then
Counter -= 1
TextBox463.Text = Counter
End If
If tb.Text = 3 Then
Counter += 1
ElseIf tb.Text <> 3 Then
Counter -= 1
TextBox462.Text = Counter
End If
If tb.Text = 4 Then
Counter += 1
ElseIf tb.Text <> 4 Then
Counter -= 1
TextBox461.Text = Counter
End If
Next
We will need more information about how your going to approach it to be able to help you further, but as for your problem with this If Textbox <> number Then count -= 1 you can use something like this since your only going to be having numbers on the textboxes If Cint(Textbox.Text) <> number then count -= 1 since your using just Textbox its attempting to evaluate it as a control and not the property that your looking for, you need to read from its .Text Property, However since its evaluated as a String and not an Integer it will throw an error exception, thats why the Cint() is included (This may also be used to convert it to integer Ctype(number, Integer) Not sure if there is an execution speed difference or not, however Cint() is a faster way of writing it.) it will try and convert the String into an Integer and when its converted into an integer it can be evaluated like one using <>. No one is going to write a whole solution out for you, but while you attempt to create it yourself and more information can be added we are more than happy to help you with problems along the way.

Progress bar and percent label

First and foremost: Yes I have searched and checked these:
* VB - progress bar not incrementing correct amounts?
* Progress bar and percents
Question: What is the best way to do a progress bar and a label for "percent" complete?
My Scenario: I have a check list box that I am looping thru and processing each item.
What I have tried: The first search result says to use the count as the max, as seen here:
ProgressBar1.Maximum = ListBox1.Items.Count
ProgressBar1.Increment(1)
This will work for the progress bar BUT not the text label, also if the items exceed 100, you will obviously get something like
231% Complete
etc..
My Idea was to use an IF statement and check to see if the total items is greater/less than 100 but thats where I have some issues.
This is my code, but please advise on what would be the best solution.
My Code:
Sub PercentComplete(ByVal Total As Long)
Dim Notch As Integer = 100 / Total
If Total > 100 Then
'If the total is greater than 100
Dim intPctText As Integer = Math.Round(Total / Notch, 10)
lblPct.Text = intPctText & "%"
Else
'If the total is less than 100
lblPct.Text = Notch.ToString & "%"
End If
End Sub

Visual Basic Data Grid View - Count Average

Okay, so I've got a datagrid view DataGridView1 which is something like the below example.
Name Points
Jack 15
zack 19
Cody 05
I want to be able to count the average of all of the points, However the amount of points will be dynamic and change from time to time. So my solution must be able to work even if there's just two numbers and when there's upwards of 20.
I have been looking a round for a way of doing this, but most posts are only relevant if the amount of 'points' are static. And a lot of the solutions appear to be written in C++, which isn't too much use to a newbie coder like myself on Visual Basic.
So could anyone help me out here?
I would force it to draw a nice extra line in the data grid, by using a union select and a blanks for each column in the sqldatabind. This would add a nice row to the dataset be formatted as a totals row.. You may need to number the rows so your totals row is last most because of ordering. add the term 'Total' as a control in the results or handle it another way.
After that its just a bit of tweaking when your grid draws the row..
something like ...
Protected Sub GridView1_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Dim dRow As GridViewRow = sender
If dRow.RowType = DataControlRowType.DataRow Then
If dRow.Cells(0).Text = "Total" Then
Dim rx As Integer
Dim TotalValue As Double = 0
For rx = 0 To GridView1.Rows.Count - 2
TotalValue += CDbl(GridView1.Rows(rx).Cells(1).Text)
Next
dRow.Cells(1).Text = FormatNumber(TotalValue, 2)
End If
End If
End Sub