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.