Results 1 to 12 of 12

Thread: VB 2008 Question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    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

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB 2008 Question

    Quote Originally Posted by CollinMcGuire View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    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?

  6. #6
    Addicted Member
    Join Date
    Oct 2008
    Posts
    152

    Re: VB 2008 Question

    Code:
    Dim intCost As Integer = 0
    If rdoCheesePizza.Checked = True Then intCost += 12

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    Code:
    intCost + 12
    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    Re: VB 2008 Question

    Well I'm guessing you would have to let the new cost be shown

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB 2008 Question

    Quote Originally Posted by Tom.Net View Post
    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:
    Code:
    intCost + 12
    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:
    Code:
    intCost += 12
    Various other operators can be shortened that way to including -, /, * and &.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Oct 2009
    Posts
    20

    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.

  11. #11
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: VB 2008 Question

    Quote Originally Posted by jmcilhinney View Post
    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
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

  12. #12
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    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
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width