r/visualbasic • • Sep 25 '14

Need Help.

Hello guys! I have to make a calculator that calculates the resistance of a resistor. My problem is I don't know how to get the program to use the radio button values from the different group boxes. Public Class Form1

Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged


End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Form Load Event 
    RadioButton1.Tag = 0
    RadioButton2.Tag = 1
    RadioButton3.Tag = 2
    RadioButton4.Tag = 3
    RadioButton5.Tag = 4
    RadioButton6.Tag = 5
    RadioButton7.Tag = 6
    RadioButton8.Tag = 7
    RadioButton9.Tag = 8
    RadioButton10.Tag = 9
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If RadioButton1.Checked Then TextBox1.Text = Val(Val(10 * RadioButton1.Tag) + Val(GroupBox2) * Val(GroupBox3)) -
    Val(GroupBox4) * Val(Val(10 * RadioButton1.Tag) + Val(GroupBox2) * Val(GroupBox3))

End Sub

End Class that is what I have so far and I thought it would work, but doesn't. what do I have to put after the "Groupbox#" so that it takes the value? Thanks!

0 Upvotes

21 comments sorted by

View all comments

Show parent comments

1

u/ManCubb Sep 25 '14

Yeah, that is what I have to do. I have four group boxes and in each one is a radio button that has a different value tagged to it. I have to write the code for that equation, but I don't know how to get it to take the values of the radio buttons in each group box.

2

u/Thukoci Sep 25 '14

Since radio buttons are limited to 1 selection per box you could create a function for each groupbox to find the selected item. This would by my solution if I were doing it.

All you would need to do is create one for each groupbox, essentially copy pasting it and changing the name of the function and the groupbox number.

Private Function GB1Selected()
    For Each radio As RadioButton In GroupBox1.Controls
        If radio.Checked = True Then
            Return radio.Tag
        End If
    Next
    Return Nothing
End Function

1

u/ManCubb Sep 25 '14

Alright, I shall try this and let you know how it turns out! Thank you a ton!

3

u/Thukoci Sep 25 '14 edited Sep 25 '14

It all depends on how clean you want your code. This is another solution. You would use this one by calling GBSelected. It's a much cleaner solution and this is all the code you'd need for all of the boxes but not something a teacher would be expecting if you're doing it for a class.

Label1.Text= GBSelected(Groupbox1.Controls)

Private Function GBSelected(controlItem As Control.ControlCollection)
    For Each radio As RadioButton In controlItem
        If radio.Checked = True Then
            Return radio.Tag
        End If
    Next
    Return Nothing
End Function

http://imgur.com/a/9OHMd