Results 1 to 21 of 21

Thread: VB Radio button code help

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    VB Radio button code help

    Hi all,
    Im new to VB and am using the 2008 express edition. So heres my issue.

    Lets say i have two radio buttons to set up. when each independent RADB is selected it will show its own picture. how do i write the code for this? Appreciate any help.

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

    Re: VB Radio button code help

    You handle the CheckedChanged event for each RadioButton and, in the event handler, you test whether the RadioButton is checked and, if it is, you display your picture.

    CheckedChanged is the default event for a RadioButton so you just double-click it in the designer to create the event handler.
    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
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    I understand how to do the double click thing. its when Im in the event handler. I am completely lost on how to link radbut1 to the picturebox, the same for radbut2. rather, i cant figure out the right code.

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

    Re: VB Radio button code help

    There is no link. It's two separate operations.

    1.
    test whether the RadioButton is checked
    2.
    if it is, you display your picture
    Note the highlighted word, which should be your clue.
    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
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: VB Radio button code help

    Think about what it is that you actually want to do before you try to write code to do it. If you try to write the code before you really even know what you're trying to achieve then you're doomed to fail. The algorithm, i.e. the steps you need to perform, come first and then the code to implement those steps comes after.

    So, what is it that you want to do?
    If the box is checked, display the picture.
    Does that sound logical? Now, can you write code to implement that?
    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    Damn...this is kicking my ass. I guess thats expected for first timers. So here is what I came up with for radbut1;

    If RadButton1.Checked Then PictureBox1.Show()
    RadButton1.Checked = True

    when I run it my radbut2 completely disappears and the radbut1 isnt checked.

    how do I hide the image that isnt checked?

    how do I get the radbut1 to show checked?

    how do I get the radbut2 to stay showing?

    Again, I appreciate whom ever might help.

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

    Re: VB Radio button code help

    Why are you setting the Checked property to True? If you do that then every time you uncheck it your RadioButton it will get checked again.

    There's nothing in that code that would cause the other RadioButton to disappear.

    Are you using two different PictureBoxes? Normally you'd just use one PictureBox that was visible all the time and set its Image property each time you wanted to change the picture.
    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
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    I was just trying to follow some kind of examples that were kinda close to what I needed. I am using two different picture boxes as I dont know how to use one. Sorry if im a bit ignorant in this area. i have never used this program before.

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    was I even close with that code?

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

    Re: VB Radio button code help

    Have a look at what I said earlier:
    If the box is checked, display the picture.
    Now look at your first line of code:
    Code:
    If RadButton1.Checked Then PictureBox1.Show()
    They look a pretty close match, don't they? Now, what is the second line supposed to be achieving? Each time the Checked property of the RadioButton changes, set it to True? Does that sound like something you want to do?
    Last edited by jmcilhinney; Nov 16th, 2009 at 11:46 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

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    Yes, I think they say the same for line one. So I think Im good on this line.

    for line two im not sure what I want. do I need a second line code? i guess I would need a second line to tell it what picture to show? am I tracking with that?

    I deleted my second picture box. How do I assign two pictures to the same box?
    I noticed theres an error image, initial image and main. is that where I attach the two photos? how do I segregate them between the radbuttons?

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

    Re: VB Radio button code help

    You don't assign two pictures to the same box. Just like you can change the photo that's displayed in a picture frame, so you can change the Image that's displayed in a PictureBox. You need to create two Image objects to begin with, then can assign either one to the PictureBox's Image property whenever appropriate, e.g.
    vb.net Code:
    1. If Me.RadioButton1.Checked Then
    2.     Me.PictureBox1.Image = image1
    3. End If
    Where exactly are the images coming from in the first place? Are they files or have you added them to your project resources?
    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

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    they are pics that are in my project folder.

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

    Re: VB Radio button code help

    Quote Originally Posted by Deepseeman View Post
    they are pics that are in my project folder.
    Your project folder doesn't exist when you deploy your app so that's no good. They need to either be resources or else files in a specific location, e.g. the same folder as the EXE. Do they need to be edited at all after deployment? If not then they should be resources. In that case you can add them on the Resources page of the project properties and access them in code via My.Resources.
    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

  15. #15

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    figured out how to add to my resources. this is the code that I have input;

    If Me.RadioButton2.Checked Then PictureBox1.Image
    PictureBox1.Image = My.Resources.unhappy

    this keeps returning errors...help

    Also, my darn radbut2 keeps disappearing after I run the debug! why? Im so frustrated. my moms getting on me too. arrgghhh1

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

    Re: VB Radio button code help

    Code:
    If Me.RadioButton2.Checked Then PictureBox1.Image
    PictureBox1.Image = My.Resources.unhappy
    PictureBox.Image is a property, not a method. You can't just call it; you have to either get its value and use it or else assign a new value to it. I'd do it like like this:
    vb.net Code:
    1. If Me.RadioButton2.Checked Then
    2.     PictureBox1.Image = My.Resources.unhappy
    3. End If
    but you can condense it into one line if you prefer:
    vb.net Code:
    1. If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy
    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

  17. #17
    Member Nicelydone's Avatar
    Join Date
    Nov 2009
    Location
    Arizona
    Posts
    61

    Re: VB Radio button code help

    If your getting errors your not posting your whole code.

    Aaaaaaannnd.. dont listen to a damn word I have to say. JMC will usually point you in the right direction.
    Last edited by Nicelydone; Nov 17th, 2009 at 12:34 AM. Reason: Responded too quickly.
    NicelyDone: The Visual Studio 2008 Newb
    There are 10 types of people in this world...Those who know binary and those who don't.
    *cough* Asimov *cough*
    I used to be a pro at HTML...Wait, nooooo! Epic fail

  18. #18

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Angry Re: VB Radio button code help

    I input your recommendation and debugged it. no errors but no pictures showed up and my radbut2 disappeared again. this is my code for the radiobuttons;

    Private Sub RadButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.CheckedChanged
    If Me.RadButton1.Checked Then PictureBox1.Image = My.Resources.happy1
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy



    End Sub

  19. #19

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    what am I doing wrong? im so mad that I cant get it!

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

    Re: VB Radio button code help

    I would suggest that you start a new project and see if the same thing happens. Either there's something you're not telling us or else either your project or your IDE are corrupted in some way.
    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

  21. #21

    Thread Starter
    New Member
    Join Date
    Nov 2009
    Posts
    11

    Re: VB Radio button code help

    got it. moms kicking me off the computer and I need to go to bed. I will start over in the am. So heres a couple of questions;

    in the picturebox, are there supposed to be any specially assigned settings or pics?
    I noticed a visible tab, is that supposed to be true or false?

    here is my whole stupid assignment codes. if you guys have the time, maybe you can find my stupid mistakes.

    thank you for you patience and help.

    Public Class SendAMessageToAFriendByEddieCulver16Nov09

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    'end the application by closing the window
    Me.Close()
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFriendA.CheckedChanged
    txtFriendA.Text = txtMessage.Text
    chkFriendA.Enabled = False

    End Sub

    Private Sub CheckBox4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFriendB.CheckedChanged
    txtFriendB.Text = txtMessage.Text
    chkFriendB.Enabled = False

    End Sub

    Private Sub CheckBox5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFriendC.CheckedChanged
    txtFriendC.Text = txtMessage.Text
    chkFriendC.Enabled = False

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    'Clear the input and output text boxes
    txtMessage.Text = ""

    txtFriendA.Text = "" 'not necessary, see comments below
    txtFriendB.Text = ""
    txtFriendC.Text = ""

    'Set back color to white
    radwhte.Checked = True 'sets BackColor to white for all output text boxes

    'Reset and enable the "send to" check boxes
    chkFriendA.Checked = False
    chkFriendB.Checked = False
    chkFriendC.Checked = False
    chkFriendA.Enabled = True
    chkFriendB.Enabled = True
    chkFriendC.Enabled = True

    'Set focus
    txtMessage.Focus() 'puts the focus on the txtMessage text box

    End Sub
    Private Sub RadGreen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadGreen.CheckedChanged
    txtFriendA.BackColor = Color.Green
    txtFriendB.BackColor = Color.Green
    txtFriendC.BackColor = Color.Green

    End Sub

    Private Sub RadRed_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadRed.CheckedChanged
    txtFriendA.BackColor = Color.Red
    txtFriendB.BackColor = Color.Red
    txtFriendC.BackColor = Color.Red

    End Sub

    Private Sub radwhte_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radwhte.CheckedChanged
    txtFriendA.BackColor = Color.White
    txtFriendB.BackColor = Color.White
    txtFriendC.BackColor = Color.White

    End Sub

    Private Sub RadButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadButton1.CheckedChanged
    If Me.RadButton1.Checked Then PictureBox1.Image = My.Resources.happy1
    End Sub

    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
    If Me.RadioButton2.Checked Then PictureBox1.Image = My.Resources.unhappy



    End Sub


    End Class

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