I'm attempting to figure volumes for a box,cylinder, cone and sphere. I have set up a combo box with all four shapes in the combo box. I name named this combo box, "cbshape". Can someone help or advise how to write code for: when a shape is selected from combo box to perform the volume formula for that shape, and post the volume in a read-only text box names"txtvolume".
I have setup this program and reading through the text I'm not exactly sure where to start or evern how to start the real coding to figure the volume per shape selected. I know that I have to use a if, the, else and nest a few if's inside the first if to complete this program. Like I said I'm just not sure to code to get the program to read from the combo box, to perform the corrort volume formula. ANY HELP WOULD BE GREAT!!!
thanks
26-10-2006 at 10:32 PM
|
~Bean~ Level: VB Guru Registered: 07-04-2003 Posts: 488
Re: combo box in vb.net
Instead of "If"s I might use a "Select Case" because this can make for easier reading, especially if things get nested...which I am not sure why it would in this case, but anyways here's a bit to get you started...
In the code that initiates the computation (like a Command Button click event, or the Combo Box Selection Changed event)
Dim theValue As Double
MsgBox(ComputeVolume(cbshape.SelectedItem))
Then, then code a "ComputeVolume" Function to do the work...
Function ComputeVolume(ByVal pShape As String) As Double
Dim retVal As Double = 0
Select Case cbshape.SelectedItem
Case "cube"
retval = some formula...
Case "cylinder"
retval = some formula...
Case "cone"
retval = some formula...
Case "sphere"
retval = some formula...
Case Else
End Select
Return retVal
End Function
____________________________
Eggheads unite! You have nothing to lose but your yolks.
I will be doing this tonight, thank you very very much for the help. I knew I had to do something with the ".selecteditem", but like i said.. I wasn't sure where and what to do..
thanks again, and I will let you know how it goes!!
[Edited by echoes915 on 27-10-2006 at 04:53 PM GMT]
I can't write a function...I'm not advanced and the book no where talks about functions yet..however this is what i put together, and i'm not getting any output to the txtvolume box...can someone tell me my errors?
Dim txtheight As Integer
Dim txtlegnth As Integer
Dim txtwidth As Integer
Dim txtradius As Integer
Dim strvolumebox As Integer
Dim strvolumecone As Integer
Dim strvolumecylinder As Integer
Dim strvolumesphere As Integer
If cbShape.Text = "" Then
MessageBox.Show("Please select a shape from the options!", "No shape selected", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
cbShape.Focus()
End If
Select Case cbShape.SelectedIndex
Case "box"
strvolumebox = txtlegnth * txtwidth * txtheight"
Case "Cone"
strvolumecone = (3.14 * txtradius * txtradius * txtheight) / 3
Case "Cylinder"
strvolumecylinder = 3.14 * txtradius * txtradius * txtheight
Case "Sphere"
strvolumesphere = 4 * 3.14 * txtradius * txtradius * txtradius / 3
End Select
If cbShape.SelectedIndex = "box" Then
txtvolume.Text = strvolumebox
Else
If cbShape.SelectedIndex = "cone" Then
txtvolume.Text = strvolumecone
Else
If cbShape.SelectedIndex = "Cylinder" Then
txtvolume.Text = strvolumecylinder
Else
If cbShape.SelectedIndex = "Sphere" Then
txtvolume.Text = strvolumesphere
End If
End If
End If
End If
End Sub
that is where i'm at.... just let me know what i'm doing wrong, or where i have done wrong!
thanks again tons
31-10-2006 at 12:38 AM
|
Nick2k3 Level: Big Cheese Registered: 23-11-2003 Posts: 23
Re: combo box in vb.net
Hey im surprised that you didnt get any errors i think your option strict is turned off
Anyways you cant compare "SelectedIndex" to your criterias because its a number (int32). (try using the index of your items on your list)
for example if you added the following to the items property on your combo box:
0 box
1 cone
2 cylinder
3 sphere
then you should use 0 as a criteria for your box,1 for cone, and so on...
If you still would want to compare strings on combo box...follow these steps
'----Create a structure (and declare it)
Public Structure YourStructure
Private HiddenName as system.string
Private HiddenValue as System.string
Public Property DisplayName as System.string
Get
return HiddenName
End Get
Set (Byval sval as system.string)
HiddenName = sval
End Set
End Property
Public Property ValueName as System.string
Get
return HiddenValue
End Get
Set (Byval sval as system.string)
HiddenValue = sval
End Set
End Property
End Structure
Private MyShapeSelections(3) as YourStructure
'----------------write these to any intialization event
'-------Reminder
in this case you cant use the "selectedindexchanged" event...use the "selectedvaluechanged" event.
From here you can use the selectedValue for your criteria
Select Case yourcombobox.selectedvalue
Case "box"
'run your formula
Case "cone"
'run your formula
case "cylinder"
'run your formula
case "sphere"
'run your formula
case else
'go for launch
end select
nice huh? thats called databinding
or you can just follow the instructions of "bean"... its alot easier
[Edited by Nick2k3 on 02-11-2006 at 08:53 PM GMT]
____________________________
Error 404 ...File not found...!!!!