Beginner VB Select Case Question

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
I need to make two select case statements that will check radio buttons/checkboxes. the first select case will check the first set of radio buttons. these radio buttons refer to a user selection; each have their own const price associated.

the second is check boxes which are also associated with their own const prices that will be added together.

these will be added together for a subtotal.

if anybody could help me setting up a select case to search for checked button. i could do if then else but would like to try a different way
 

SoundTheSurrender

Diamond Member
Mar 13, 2005
3,126
0
0
Create a RadioButtonList, make up 3 entrys (click the little play button on the radiobuttonlist and click EDIT ITEMS), add a button, and add a label.

Double click the button and put this logic in the button.


Select Case (RadioButtonList1.SelectedIndex)
Case 1
Label1.Text = "One"

Case 2

Label1.Text = "Two"

Case 3
Label1.Text = "Three"



End Select
 

SoundTheSurrender

Diamond Member
Mar 13, 2005
3,126
0
0
I think you'd have to use a if statement for that on each check box. A case statement doesn't make sense but I could be wrong.
 

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
i dont understand exactly how to use the select case method because my teacher didnt really spend time on it, instead we have used if-then-else statements like crazy. then she decided to randomly throw it on a quiz. i have been trying to understand how u would do it but havent been able to come up wit it, so i just said forget select case and did..

if glossypaintcheckbox.checked then
salesPriceDecimal += GLOSSY_PRICE_Decimal
...
so on and so on
 

SoundTheSurrender

Diamond Member
Mar 13, 2005
3,126
0
0
Well if you think about the logic behind select case, it's like criteria. A check box is either check or not. So I guess you could do a 2 way case where if it's check then it does this and if it isn't check do nothing. I guess it could be possible.
 

formulav8

Diamond Member
Sep 18, 2000
7,004
523
126
I'm not 100% sure what you want. But I will give it a try.

Try using a If statement to check each of the controls.


If RadioButton1.Checked Then

MsgBox("RadioButton1 is checked!")

ElseIf RadioButton2.Checked Then

MsgBox("RadioButton2 is checked!")

'
'and so on and so forth....
'
End If


Again, I am not completely sure on what you want to accomplish...


Jason
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Select Case True

Case Button1.checked
Foo()
Case Button2.checked
Bar()
Case Button3.checked
Bat()

End Select
 

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
Originally posted by: formulav8
I'm not 100% sure what you want. But I will give it a try.

Try using a If statement to check each of the controls.


If RadioButton1.Checked Then

MsgBox("RadioButton1 is checked!")

ElseIf RadioButton2.Checked Then

MsgBox("RadioButton2 is checked!")

'
'and so on and so forth....
'
End If


Again, I am not completely sure on what you want to accomplish...


Jason
Jason.. for example this applications allows you to configure a computer picking which components (radiobutton). towards the bottom there are optional services (checkboxes) warrenty, extra tests, software, etc. which all have assigned price.
i need the select case to add the price of ones that user has selected to subtotalDecimal
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Select case is inappropriate for that use, or other uses where multiple cases can and will be true. There is not fall through - after the first true case executes it's block, the select is exited.

Use multiple IFs for situations where more then one can be true, or a setup collection or array of structures with a loop and test.
 

jsedlak

Senior member
Mar 2, 2008
278
0
71
Ew Ew Ew!

Want to wow your teacher?? Do this... handle the event and pass the data in using Object Oriented Design!

Public Class Form1

Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
Dim btn As RadioButton
btn = CType(sender, RadioButton)

If Not btn Is Nothing And btn.Checked Then
Label1.Text = btn.Tag.ToString()
End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
RadioButton1.Tag = 3
RadioButton2.Tag = 4
RadioButton3.Tag = 5
End Sub
End Class
 

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
Originally posted by: PhatoseAlpha
Select case is inappropriate for that use, or other uses where multiple cases can and will be true. There is not fall through - after the first true case executes it's block, the select is exited.

Use multiple IFs for situations where more then one can be true, or a setup collection or array of structures with a loop and test.
I thought it made no sense but im a beginner (obviously) so I wasnt sure...but that is basicly what she wanted.. multiple checkboxes use select case to add prices.

so i could use select case for cpu radiobutton choice since they can only select one in the groupbox??

edit:group box not groubo lol
 

drebo

Diamond Member
Feb 24, 2006
7,034
1
81
Well, you could potentially use Select Case in that instance, but it would require some intermediary steps.

For instance, when a checkbox is checked, you would add an identifier to an array or other variable. For instance:

components(1) = "This CPU"
components(2) = "That RAM"
components(3) = "This Optional Do-hickey"

Then, at the end, you could loop through that array or other variable with a Select Case inside to add up the price...

For i = 1 to UBound(components)
Select Case components(i)
Case "This CPU"
'add this price
Case "That RAM"
'add that price
End Select
Next i

And so on. Depending on exactly what you're looking at doing, this may or may not be the appropriate solution.
 

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
ok this professor must have been smoking crack.. because she stated we couldnt use stuff we havent learned in class. And arrays is something we havent learned yet so how could we use select case on checkboxes. i think stuff like this is the reason administration has told this will be her last semester.
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Well, you could possibly do it the stupidly hard way.....

SELECT CASE TRUE
case NOT(Box1.checked OR Box2.checked OR Box3.checked)
case Box1.checked AND NOT (Box2.checked OR Box3.checked)
case Box2.checked AND NOT (Box1.checked OR Box3.checked)
case Box3.checked AND NOT (Box1.checked OR Box2.checked)
case (Box1.checked AND Box2.checked) AND NOT Box3.checked
case (Box2.checked AND Box3.checked) AND NOT Box1.checked
case (Box1.checked AND Box3.checked) AND NOT Box2.checked
case Box1.checked AND box2.checked AND box3.checked
END SELECT


Which indeed seems, and in fact is, completely inane. But I've known a few teachers in my day who give assignments like this simply because it drives home the next lesson, where she teaches you how to do it a better way. Living in squalor makes you appreciate opulence that much more, and all that.
 

jae

Golden Member
Jul 31, 2001
1,034
0
76
www.facebook.com
Originally posted by: PhatoseAlpha
Well, you could possibly do it the stupidly hard way.....

SELECT CASE TRUE
case NOT(Box1.checked OR Box2.checked OR Box3.checked)
case Box1.checked AND NOT (Box2.checked OR Box3.checked)
case Box2.checked AND NOT (Box1.checked OR Box3.checked)
case Box3.checked AND NOT (Box1.checked OR Box2.checked)
case (Box1.checked AND Box2.checked) AND NOT Box3.checked
case (Box2.checked AND Box3.checked) AND NOT Box1.checked
case (Box1.checked AND Box3.checked) AND NOT Box2.checked
case Box1.checked AND box2.checked AND box3.checked
END SELECT


Which indeed seems, and in fact is, completely inane. But I've known a few teachers in my day who give assignments like this simply because it drives home the next lesson, where she teaches you how to do it a better way. Living in squalor makes you appreciate opulence that much more, and all that.

yes that sounds exactly like my teacher, i believe next chapter was array.. which i suspect will would be alot more simpler than what you suggested. but i wasnt in class last week because i had my appendix removed and this week is spring break. thanks alot guys for your help