Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Help me how can I Calculate the average of combobox.Text...

VB
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
Comments
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

1 solution

Instead of just adding them together an hoping, do explicit conversions:
VB
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:
VB
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.
SQL
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:
SQL
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:
VB
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.
 
Share this answer
 
v3
Comments
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

MsgBox("your average is:" + average.ToString())
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    
Use "/" instead of "\"
San Dra 20-Jan-13 7:53am    
Ok, thank you!

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900