none
Radio Button+Picture Box: How to make them work together

    Question

  • Hey,

    I'm doing a project in which I have 5 radio buttons. I want each of them to show me a different image when I click on them... My guess is that I have to usea Picture Box. Am I wrong?

    How do I make them work together?

    PS: When I click on another radio button I want the other one to disappear and show me the new one.

     

    Thanks!

    Friday, October 29, 2010 3:29 AM

Answers

  • Hi 

    The easiest way is to click on "Project" -> "windowsApplication Properties"
    Then click the "resources" tab on the left.
    Then click the "add resource" tab on the top then choose "add existing file"
    Navigate to your pictures eg: pic1, pic2. pic3, pic4, pic5
    Select them all and click "open"
    Then go to your main form, drop 5 radio buttons and 1 picturebox onto the form.
    Double click your first radiobutton and you should now see the code for the CheckChanged event.
     Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        PictureBox1.BackgroundImage = My.Resources._THE NAME OF YOUR FILE HERE
     End Sub
    
    Do this for each radio button and it will work fine.

    Let us know if you have any more questions.

    Good luck,

    Rich.
    • Marked as answer by andresmdiaz Sunday, October 31, 2010 6:39 PM
    Friday, October 29, 2010 9:47 PM

All replies

  • put the code to display each image in each Radiobutton checkchanged event

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Friday, October 29, 2010 4:00 PM
  • Hi 

    The easiest way is to click on "Project" -> "windowsApplication Properties"
    Then click the "resources" tab on the left.
    Then click the "add resource" tab on the top then choose "add existing file"
    Navigate to your pictures eg: pic1, pic2. pic3, pic4, pic5
    Select them all and click "open"
    Then go to your main form, drop 5 radio buttons and 1 picturebox onto the form.
    Double click your first radiobutton and you should now see the code for the CheckChanged event.
     Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
        PictureBox1.BackgroundImage = My.Resources._THE NAME OF YOUR FILE HERE
     End Sub
    
    Do this for each radio button and it will work fine.

    Let us know if you have any more questions.

    Good luck,

    Rich.
    • Marked as answer by andresmdiaz Sunday, October 31, 2010 6:39 PM
    Friday, October 29, 2010 9:47 PM
  • Hey Richard,

    Thank you for replying.

    What if I want to use one PictureBox for each radio button? How would that work?

    I am able to make them show me the pciture when I click on them but then I do I make the one showing before disappear?

    Same thing when I go back a click on the one showing before...

    I have 11 RadioButtons so I want something simple.. I was using something like this:

    Private Sub radBat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles radBat.Click
    
        '-----------------------------------------------
        'Shows the picture of bat on PictureBox1
        '------------------------------------------------
    
        PictureBox1.Visible = True
    
        PictureBox1.BackgroundImage = My.Resources.BlackBats
    
    
      End Sub
    
    
      Private Sub radBlackCat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles radBlackCat.Click
    
        '----------------------------------------------------------------
        'Shows the picture of a black cat on PictureBox1
        '----------------------------------------------------------------
    
        PictureBox1.Visible = False
        PictureBox2.Visible = True
    
        PictureBox2.Image = My.Resources.BlackCat
    
    
    
      End Sub
    

    But then I figured, it is probably not the easiest way to do it with 11 Radio Buttons.

     

    Sunday, October 31, 2010 7:06 PM
  • Hey Richard,

    Thank you for replying.

    What if I want to use one PictureBox for each radio button? How would that work?

    I am able to make them show me the pciture when I click on them but then I do I make the one showing before disappear?

    Same thing when I go back a click on the one showing before...

    I have 11 RadioButtons so I want something simple.. I was using something like this:

    Private Sub radBat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles radBat.Click
    
      '-----------------------------------------------
      'Shows the picture of bat on PictureBox1
      '------------------------------------------------
    
      PictureBox1.Visible = True
    
      PictureBox1.BackgroundImage = My.Resources.BlackBats
    
    
     End Sub
    
    
     Private Sub radBlackCat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles radBlackCat.Click
    
      '----------------------------------------------------------------
      'Shows the picture of a black cat on PictureBox1
      '----------------------------------------------------------------
    
      PictureBox1.Visible = False
      PictureBox2.Visible = True
    
      PictureBox2.Image = My.Resources.BlackCat
    
    
    
     End Sub
    

    But then I figured, it is probably not the easiest way to do it with 11 Radio Buttons.

     

    Do you have to use radioButtons? The reason I ask is because with radio buttons, you "check it" and then that's it. You can't uncheck it.

    If you must use radioButtons then you will need to drop all 11 onto your form, double click on each of them separately - that will add the "CheckedChanged" Event to each of them in the code section. 
    Next, you will need to do as I mentioned earlier and add all the images you require to your resources.

    Finally, you will need to copy the code >  "PictureBox1.BackgroundImage = My.Resources.BlackBats" in each of the CheckedChanged Events.

    EG:

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
      PictureBox1.BackgroundImage = My.Resources.BlackCat
    End Sub
    
    Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
      PictureBox2.BackgroundImage = My.Resources.BlackCat1
    End Sub
    
    Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
      PictureBox3.BackgroundImage = My.Resources.BlackCat2
    End Sub
    
    Private Sub RadioButton4_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton4.CheckedChanged
      PictureBox4.BackgroundImage = My.Resources.BlackCat3
    End Sub
    
    Private Sub RadioButton5_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton5.CheckedChanged
      PictureBox5.BackgroundImage = My.Resources.BlackCat4
    End Sub
    

    I noticed that you seem to be controlling the image which is displayed via your .click event of the button. I don't think you need that.

    My suggestion is to use only CheckBoxes. The reason is because with a checkbox you can "UnCheck" it. This will allow you to make the pictureBox 1 image when the checkBox is checked and another image when NO checkBoxes are checked.

    Let me know if you need some more help.

    Regards, 

    Rich.

    Sunday, October 31, 2010 9:57 PM