Question Use two radio buttons to show picture.

gw73

Member
Joined
Dec 15, 2009
Messages
7
Programming Experience
Beginner
hi
I have a form with two group boxes. The first one has two radio buttons and the second has four. The user chooses one button from the first group box and one button from the second which gives me eight selections and I would like a different picture for each selection. I think I could use a select case statement but I can't figure out which event would trigger this. Any ideas. I'm still pretty new at this.

Thanks
 
Something like this:
VB.NET:
Private group1Options As RadioButton()
Private group2Options As RadioButton()
Private group1Index As Integer = -1
Private group2Index As Integer = -1
Private images As Image(,)

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.group1Options = New RadioButton() {Me.group1Option1, Me.group1Option2}
    Me.group2Options = New RadioButton() {Me.group2Option1, _
                                          Me.group2Option2, _
                                          Me.group2Option3, _
                                          Me.group2Option4}
    Me.images = New Image(,) {{Image.FromFile("image11.jpg"), _
                               Image.FromFile("image12.jpg"), _
                               Image.FromFile("image13.jpg"), _
                               Image.FromFile("image14.jpg")}, _
                              {Image.FromFile("image21.jpg"), _
                               Image.FromFile("image22.jpg"), _
                               Image.FromFile("image23.jpg"), _
                               Image.FromFile("image24.jpg")}}
End Sub

Private Sub group1_CheckedChanged(ByVal sender As Object, _
                                  ByVal e As EventArgs) Handles group1Option2.CheckedChanged, _
                                                                group1Option1.CheckedChanged
    Me.group1Index = Array.IndexOf(Me.group1Options, sender)
    Me.DisplaySelectedImage()
End Sub

Private Sub group2_CheckedChanged(ByVal sender As Object, _
                                  ByVal e As EventArgs) Handles group2Option4.CheckedChanged, _
                                                                group2Option3.CheckedChanged, _
                                                                group2Option2.CheckedChanged, _
                                                                group2Option1.CheckedChanged
    Me.group2Index = Array.IndexOf(Me.group2Options, sender)
    Me.DisplaySelectedImage()
End Sub

Private Sub DisplaySelectedImage()
    If Me.group1Index <> -1 AndAlso Me.group2Index <> -1 Then
        Me.PictureBox1.Image = Me.images(Me.group1Index, Me.group2Index)
    End If
End Sub
 
Problem

Hi
thanks again for your reply. I have adapted your code to my form, but I have made some kind of error. As I step through it I get an error at this line.

VB.NET:
Me.group1Index = Array.IndexOf(Me.group1Options, sender)
The error is Value cannot be null. Parameter name: array
When I click on a radio button the group1 or group2_checked Changed is triggered but no value is passed. So I have done something wrong but I can't figure it out. Maybe you could see what I am doing wrong.

VB.NET:
Public Class Form1
    Private group1Options As RadioButton()
    Private group2Options As RadioButton()
    Private group1Index As Integer = -1
    Private group2Index As Integer = -1
    Private images As Image(,)
    

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Me.group1Options = New RadioButton() {Me.RadioButton1, Me.RadioButton2}
        Me.group2Options = New RadioButton() {Me.RadioButton3, _
                                              Me.RadioButton4, _
                                              Me.RadioButton5, _
                                              Me.RadioButton6}
        Me.images = New Image(,) {{Image.FromFile("C:\Documents and Settings\NEew.gif"), _
                                   Image.FromFile("C:\Documents and Settings\NEns.gif"), _
                                   Image.FromFile("C:\Documents and Settings\NWew.gif"), _
                                   Image.FromFile("C:\Documents and Settings\NWns.gif")}, _
                                  {Image.FromFile("C:\Documents and Settings\SEew.gif"), _
                                   Image.FromFile("C:\Documents and Settings\SEns.gif"), _
                                   Image.FromFile("C:\Documents and Settings\SWew.gif"), _
                                   Image.FromFile("C:\Documents and Settings\SWns.gif")}}
    End Sub

    Private Sub group1_CheckedChanged(ByVal sender As Object, _
                                      ByVal e As EventArgs) Handles RadioButton2.CheckedChanged, _
                                                                    RadioButton1.CheckedChanged
        Me.group1Index = Array.IndexOf(Me.group1Options, sender)
        Me.DisplaySelectedImage()
    End Sub

    Private Sub group2_CheckedChanged(ByVal sender As Object, _
      ByVal e As EventArgs) Handles RadioButton4.CheckedChanged, _
         RadioButton3.CheckedChanged, _
          RadioButton2.CheckedChanged, _
          RadioButton1.CheckedChanged
        Me.group2Index = Array.IndexOf(Me.group2Options, sender)
        Me.DisplaySelectedImage()
    End Sub

    Private Sub DisplaySelectedImage()
        If Me.group1Index <> -1 AndAlso Me.group2Index <> -1 Then
            Me.PictureBox1.Image = Me.images(Me.group1Index, Me.group2Index)
        End If
    End Sub

End Class


Thanks for your time
 
That sounds like the CheckedChanged event is being raised during creation of the form because a RadioButton is checked by default, but the arrays haven't been created at that point. Try setting the Checked property of each RadioButton to False in the designer.
 
Back
Top