12,457,493 members (58,437 online)
Rate this:
See more:
Help me how can I Calculate the average of combobox.Text...

```ComboBox1.Items.Add("4")
ComboBox1.SelectedItem = 0

ComboBox2.SelectedItem = 0

ComboBox3.SelectedItem = 0```

Dim average as string
average = combobox1.Text + combobox2.Text + combobox3.Text ) / 3
MsgBox(average)
Posted 20-Jan-13 0:56am
San Dra391
Sandeep Mewara 20-Jan-13 7:13am

Your question title and content is confusing. Not clear on what are you trying to do and where are you stuck.
San Dra 20-Jan-13 7:41am

the last thing in button click...it displays the wrong average even that math formule is right

Rate this:

## Solution 1

```Dim average As Single
average = (Integer.Parse(combobox1.Text) + Integer.Parse(combobox2.Text) + Integer.Parse(combobox3.Text)) \ 3
MsgBox(average.ToString())```
```ComboBox1.Items.Add(4)
```
And then add the SelectedValue properties together instead of the Text property

"help me with an examples of a few combobox pls"

You have got to start thinking for yourself.
I can't do your whole job for you! This isn't difficult stuff - there is a lot more complexity to come, so it is time you start to think about things and work them out for yourself.
But, I'll give you this bit.
```Dim valuesTotal As Integer = 0
Dim valuesCount As Integer = 0
If comboBox1.SelectedIndex >= 0 Then
valuesCount += 1
valuesTotal += Integer.Parse(DirectCast(comboBox1.SelectedItem, String))
End If
If comboBox2.SelectedIndex >= 0 Then
valuesCount += 1
valuesTotal += Integer.Parse(DirectCast(comboBox2.SelectedItem, String))
End If
If valuesCount > 0 Then
Dim average As Integer = valuesTotal \ valuesCount
End If```

Was that so difficult? It's even easier if you use numeric values instead of strings:
```Dim valuesTotal As Integer = 0
Dim valuesCount As Integer = 0
If comboBox1.SelectedIndex >= 0 Then
valuesCount += 1
valuesTotal += CInt(comboBox1.SelectedItem)
End If
If comboBox2.SelectedIndex >= 0 Then
valuesCount += 1
valuesTotal += CInt(comboBox2.SelectedItem)
End If
If valuesCount > 0 Then
Dim average As Integer = valuesTotal \ valuesCount
End If```

I have 3 comboboxes, each with coffe bar items, like: Water, Capuchino, Etc.
Now How can I Display in bill how much they cost?
water is 8\$, how can I count that the users has selected the items in comboboxes and want to know how much it cost?
OriginalGriff - 1 hr ago
How would I do it?
I'd create a new class, called "Product" or similar, which had a text string and a price. I'd override the ToString method to generate either the name, or the name and the price depending on what I wanted to display.
I'd then set the class instance into the comboboxes (which calls the ToString method to display a string) and just use them directly when I wanted to add value yup.
San Dra - 55 mins ago
Can You help me with an example plss...
San Dra - 30 mins ago
OriginalGriff - 13 mins ago
You know how to create a class, yes?
San Dra - 4 mins ago
No i haven't really create one...But help me pls"

A class:
```Public Class Product
Private _ProductName As String
Private _Cost As Double

Property ProductName As String
Get
Return _ProductName
End Get
Set(value As String)
_ProductName = value
End Set
End Property
Property Cost As Double
Get
Return _Cost
End Get
Set(value As Double)
_Cost = value
End Set
End Property
Public Overrides Function ToString() As String
Return _ProductName & " @ \$" & _Cost
End Function
End Class```
The easiest way to add it is to right click your project and select "add...class..." and name it "Product" before pressing OK - that will create a blank template file for you. Then all you have to do is create the instances, load them into your comboboxes and get the values back - and you should know how to do all that already from your previous stuff.
v3
San Dra 20-Jan-13 7:40am

it shows an error and does not display the right average

Dim average As Single
average = (Integer.Parse(ComboBox1.SelectedItem) + Integer.Parse(ComboBox2.SelectedItem) + Integer.Parse(ComboBox3.SelectedItem) + Integer.Parse(ComboBox4.SelectedItem) + Integer.Parse(ComboBox5.SelectedItem) + Integer.Parse(ComboBox6.SelectedItem) + Integer.Parse(ComboBox7.SelectedItem) + Integer.Parse(ComboBox8.SelectedItem)) \ 8

OriginalGriff 20-Jan-13 7:47am

What error, and why didn't you use SelectedValue as I suggested?
San Dra 20-Jan-13 7:49am

no i fix it with .Text, that was so annoying ..
Now one last thing is that for example: it displays 7, when in real should displays 7.5, how can i d that?
OriginalGriff 20-Jan-13 7:52am

San Dra 20-Jan-13 7:53am

Ok, thank you!
San Dra 22-Jan-13 13:49pm

#OriginalGriff
I have 10 combobox-es .. To calculate average I divined it with num 10, only if the user choose 10 number, but what if he choose only 3 or 4 or 5 and not 10, how can i do to calculate only numbers that has been chosen by the users?

something like I should create an variable how?
OriginalGriff 22-Jan-13 14:00pm

Yes - create a variable and count the number of selections the user has made (adding the totals as you go). You then divide the total by the count.
Easy! :laugh:
San Dra 22-Jan-13 14:06pm

I'm not sure if is like:

Dim i as integer
For(i=0, i++, i<10)
then?
OriginalGriff 22-Jan-13 14:17pm

No, because you can't access the combo boxes by number (unless you set them up in an array to start with) and you don't want an integer that always runs from 0 to 10, because it will always end up with the same number!
What you want to do is:
1) Declare a variable (call it valuesCount) and set it to zero. Do the same with a valuesTotal variable
2) Check the first combobox. If it has a value, convert it and add it to the valuesTotal. Add one to valuesCount
3) Check the second combobox. If it has a value, convert it and add it to the valuesTotal. Add one to valuesCount
4) Repeat for all comboboxes ...you get the idea! :laugh:
n) At the end, you check the values count - if it is non-zero, then you can work out the average by valuesTotal / valuesCount. Ifv it isn't either remind the user he needs a value, or ignore it.
San Dra 22-Jan-13 15:06pm

help me with an examples of a few combobox pls
OriginalGriff 22-Jan-13 15:27pm

San Dra 30-Jan-13 11:10am

I have 3 comboboxes, each with coffe bar items, like: Water, Capuchino, Etc.
Now How can I Display in bill how much they cost?
water is 8\$, how can I count that the users has selected the items in comboboxes and want to know how much it cost?
OriginalGriff 30-Jan-13 11:18am

How would I do it?
I'd create a new class, called "Product" or similar, which had a text string and a price. I'd override the ToString method to generate either the name, or the name and the price depending on what I wanted to display.
I'd then set the class instance into the comboboxes (which calls the ToString method to display a string) and just use them directly when I wanted to add value yup.
San Dra 30-Jan-13 11:27am

Can You help me with an example plss...
San Dra 30-Jan-13 11:52am

OriginalGriff 30-Jan-13 12:09pm

You know how to create a class, yes?
San Dra 30-Jan-13 12:18pm

No i haven't really create one...But help me pls
OriginalGriff 30-Jan-13 12:39pm

DinoRondelly 22-Jan-13 15:37pm

My 5 ... above and beyond for sure

Top Experts
Last 24hrsThis month
 OriginalGriff 348 Maciej Los 238 Dave Kreskowiak 173 ppolymorphe 151 Mehdi Gholam 145
 OriginalGriff 6,916 ppolymorphe 4,101 Maciej Los 3,943 Karthik Bangalore 3,665 Richard Deeming 2,915