Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB VisualStudio2012
Help me how can I Calculate the average of combobox.Text...
 
ComboBox1.Items.Add("4")
        ComboBox1.Items.Add("5")
        ComboBox1.Items.Add("6")
        ComboBox1.Items.Add("7")
        ComboBox1.Items.Add("8")
        ComboBox1.Items.Add("9")
        ComboBox1.Items.Add("10")
        ComboBox1.SelectedItem = 0
 
        ComboBox2.Items.Add("4")
        ComboBox2.Items.Add("5")
        ComboBox2.Items.Add("6")
        ComboBox2.Items.Add("7")
        ComboBox2.Items.Add("8")
        ComboBox2.Items.Add("9")
        ComboBox2.Items.Add("10")
        ComboBox2.SelectedItem = 0
 
        ComboBox3.Items.Add("4")
        ComboBox3.Items.Add("5")
        ComboBox3.Items.Add("6")
        ComboBox3.Items.Add("7")
        ComboBox3.Items.Add("8")
        ComboBox3.Items.Add("9")
        ComboBox3.Items.Add("10")
        ComboBox3.SelectedItem = 0
 

Dim average as string
average = combobox1.Text + combobox2.Text + combobox3.Text ) / 3
MsgBox(average)
Posted 20-Jan-13 1:56am
San Dra391
Comments
Sandeep Mewara at 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 at 20-Jan-13 7:41am
   
the last thing in button click...it displays the wrong average even that math formule is right

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Instead of just adding them together an hoping, do explicit conversions:
Dim average As Single
average = (Integer.Parse(combobox1.Text) + Integer.Parse(combobox2.Text) + Integer.Parse(combobox3.Text)) \ 3
MsgBox(average.ToString())
Alternatively, don't add strings - add the actual values:
        ComboBox1.Items.Add(4)
        ComboBox1.Items.Add(5)
        ComboBox1.Items.Add(6)
        ComboBox1.Items.Add(7)
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
 

"Help me on this please..
 
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?
Reply
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.
Reply
San Dra - 55 mins ago
Can You help me with an example plss...
Reply
San Dra - 30 mins ago
Please Help me one more time please
Reply
OriginalGriff - 13 mins ago
You know how to create a class, yes?
Reply
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.
  Permalink  
v3
Comments
San Dra at 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
 
MsgBox("your average is:" + average.ToString())
OriginalGriff at 20-Jan-13 7:47am
   
What error, and why didn't you use SelectedValue as I suggested?
San Dra at 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 at 20-Jan-13 7:52am
   
Use "/" instead of "\"
San Dra at 20-Jan-13 7:53am
   
Ok, thank you!
San Dra at 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 at 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 at 22-Jan-13 14:06pm
   
I'm not sure if is like:
 
Dim i as integer
For(i=0, i++, i<10)
then?
OriginalGriff at 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 at 22-Jan-13 15:06pm
   
help me with an examples of a few combobox pls
OriginalGriff at 22-Jan-13 15:27pm
   
Answer updated
San Dra at 30-Jan-13 11:10am
   
Help me on this please..
 
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 at 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 at 30-Jan-13 11:27am
   
Can You help me with an example plss...
San Dra at 30-Jan-13 11:52am
   
Please Help me one more time please
OriginalGriff at 30-Jan-13 12:09pm
   
You know how to create a class, yes?
San Dra at 30-Jan-13 12:18pm
   
No i haven't really create one...But help me pls
OriginalGriff at 30-Jan-13 12:39pm
   
Answer updated
DinoRondelly at 22-Jan-13 15:37pm
   
My 5 ... above and beyond for sure

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,569
1 Sergey Alexandrovich Kryukov 6,168
2 DamithSL 5,228
3 Manas Bhardwaj 4,717
4 Maciej Los 4,150


Advertise | Privacy | Mobile
Web04 | 2.8.1411022.1 | Last Updated 30 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100