|
-
Oct 19th, 2009, 08:36 PM
#1
Thread Starter
Junior Member
VB 2008 Question
Okay so I need some help with radio buttons.
Basically what I am looking to do is make an "If...Then" statement but it needs to know which radio buttons were selected to finish the "then" part of the statement. What code would I use to do this?
I don't really want a spoiler, more of an explanation of how it actually works also.
I would Google it, but I don't know what I am looking for.
-
Oct 19th, 2009, 09:54 PM
#2
Re: VB 2008 Question
You've really got two choices:
1. When you need to know, use an If...ElseIf...EndIf statement to test the Checked property of each RadioButton to find the one that's True.
2. On the CheckedChanged event of each RadioButton, set a variable to a value that indicates which RadioButton is Checked. When you need to know, test that value.
By the way, in future can you please provide meaningful titles for your threads. With titles like "VB 2008 Question" how can we know which threads are relevant without opening and reading every single one? The title should describe the topic so people can decide whether or not they can help or be helped by the thread.
Last edited by jmcilhinney; Oct 19th, 2009 at 09:58 PM.
-
Oct 20th, 2009, 05:38 PM
#3
Thread Starter
Junior Member
Re: VB 2008 Question
Okay I understand what you were saying. But a classmate gave me the extremely simple answer that i really should have thought of...
if whateveryourradiobuttonis.checked = true then whatever
-
Oct 20th, 2009, 05:53 PM
#4
Re: VB 2008 Question
 Originally Posted by CollinMcGuire
Okay I understand what you were saying. But a classmate gave me the extremely simple answer that i really should have thought of...
if whateveryourradiobuttonis.checked = true then whatever
Which is exactly what I said in option 1. If you only have two RadioButtons then just an If...Else will be enough but if you have three or more then you'll need at least one ElseIf in there too.
-
Oct 20th, 2009, 07:16 PM
#5
Thread Starter
Junior Member
Re: VB 2008 Question
Okay so I ran into another problem.
The app I am trying to create for a class is this...
HTML Code:
A pizza business app.
You will select the radio button that will select the type of pizza you want.
then you can select it to be delivered or not.
next you will select any other toppings.
then the amount of pies you want.
then hit the final cost button and get your final price.
so the problem i am having with it is, the math functions. I tried declaring a variable as integer set to 0 and then using the if then statements to add up the cost.
so here is an example.
HTML Code:
dim intCost as integer
intCost=0
if CheesePizza.checked=true then intCost + 12
but all i get is syntax build errors. So what exactly is wrong?
-
Oct 20th, 2009, 07:25 PM
#6
Addicted Member
Re: VB 2008 Question
Code:
Dim intCost As Integer = 0
If rdoCheesePizza.Checked = True Then intCost += 12
-
Oct 20th, 2009, 07:28 PM
#7
Re: VB 2008 Question
First up, if you have a new question that isn't directly related to the topic of the thread, please create a new thread. The number of threads doesn't matter; what matters is keeping the forums organised. Keeping each thread to one topic, with a descriptive title, and each topic to one thread will help to do that.
Secondly, if you get errors then you get error messages. They are provided as an aid to diagnosing the problem. If you want us to diagnose the problem then you should pass those messages on to us. In this case it's easy to see what the issue is but that will not always be the case, so ALWAYS provide ALL the relevant information.
As for the question, you're letting the fact that you're writing code blind you to things that you already know and already do. Think about what you would do if you were performing the maths with a pen and paper. If you wanted to take the current value of intCost and increase it by 12 would you do it like this:No, you wouldn't. That doesn't change intCost at all. That just takes the current value of intCost and adds 12 to it. That produces a result but doesn't do anything with it. If you want to change the value of intCost then what have you got to do with that result?
-
Oct 20th, 2009, 07:31 PM
#8
Thread Starter
Junior Member
Re: VB 2008 Question
Well I'm guessing you would have to let the new cost be shown
-
Oct 20th, 2009, 07:31 PM
#9
Re: VB 2008 Question
 Originally Posted by Tom.Net
Code:
Dim intCost As Integer = 0
If rdoCheesePizza.Checked = True Then intCost += 12
It appears that Tom's decided to do your thinking for you without providing any explanation so I'll expand a bit. What I was getting at it that you don't do just this:because that only produces a result. You have to assign that result back to intCost to change its value, i.e.
Code:
intCost = intCost + 12
What Tom has done is used a shorthand supported by VB for setting the value of a variable based on its current value. The code above can be shortened to:Various other operators can be shortened that way to including -, /, * and &.
-
Oct 20th, 2009, 07:44 PM
#10
Thread Starter
Junior Member
Re: VB 2008 Question
oh okay I get what you did with
HTML Code:
intCost = intCost +12
it's the same thing that i learned with counters. I really should have realized that it would be the same type of math.
-
Oct 21st, 2009, 08:43 AM
#11
Re: VB 2008 Question
 Originally Posted by jmcilhinney
You've really got two choices:
1. When you need to know, use an If...ElseIf...EndIf statement to test the Checked property of each RadioButton to find the one that's True.
2. On the CheckedChanged event of each RadioButton, set a variable to a value that indicates which RadioButton is Checked. When you need to know, test that value.
By the way, in future can you please provide meaningful titles for your threads. With titles like "VB 2008 Question" how can we know which threads are relevant without opening and reading every single one? The title should describe the topic so people can decide whether or not they can help or be helped by the thread.
Kinda behind on this but you could do:
Code:
Select Case True
Case RadioButton1.Checked
Case RadioButton2.Checked
'.....
End Select
Much cleaner than a bunch of ElseIf's
-
Oct 21st, 2009, 09:00 AM
#12
Re: VB 2008 Question
lol lets not get into the whole "Select Case vs If statement" thing again... there is already another thread for that
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|