radio buttons

creating an independent sub procedure

Page 1 of 1

7 Replies - 3018 Views - Last Post: 03 November 2007 - 09:50 PM Rate Topic: -----

#1 raeNet  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 01-November 07

radio buttons

Post icon  Posted 03 November 2007 - 01:00 PM

[font=Garamond][size=7]

Following is my code for a VB.Net project using (2) groups of radio keys: 1st group lists the states and the 2nd group lists the capitals.

Object: when one state and one capital from each group is matched up, either you receive a message stating you are correct or you are incorrect.

My code works as written, but I need some feedback as to how I can use less code to accomplish the same task. I know there has to be a less wordy way to make this work.

Thanks in advance for any recommendations.

raeNet


[Option Explicit On
Option Strict On

Public Class MainForm

	' module-level string variables
	Private capital As String
	Private choice As String

	Private Sub xExitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xExitButton.Click
		Me.Close()
	End Sub

	Private Sub SelectStateAndCapital()
		' select capital for each state

		If Me.xArkRadioButton.Checked And Me.xLittleRockRadioButton.Checked Then
			choice = capital
		ElseIf Me.xIllRadioButton.Checked And Me.xSpringfieldRadioButton.Checked Then
			choice = capital
		ElseIf Me.xKenRadioButton.Checked And Me.xFrankfortRadioButton.Checked Then
			choice = capital
		ElseIf Me.xOreRadioButton.Checked And Me.xSalemRadioButton.Checked Then
			choice = capital
		ElseIf Me.xWisRadioButton.Checked And Me.xMadisonRadioButton.Checked Then
			choice = capital
		End If
	   
		' select state for each capital
		If Me.xFrankfortRadioButton.Checked And Me.xKenRadioButton.Checked Then
			capital = choice
		ElseIf Me.xLittleRockRadioButton.Checked And Me.xArkRadioButton.Checked Then
			capital = CStr(True)
		ElseIf Me.xMadisonRadioButton.Checked And Me.xWisRadioButton.Checked Then
			capital = choice
		ElseIf Me.xSalemRadioButton.Checked And Me.xOreRadioButton.Checked Then
			capital = choice
		ElseIf Me.xSpringfieldRadioButton.Checked And Me.xIllRadioButton.Checked Then
			capital = choice
		End If
	   
	End Sub

	Private Sub ProcessStateRadioButtons(ByVal sender As Object, ByVal e As System.EventArgs) _
			Handles xArkRadioButton.Click, xIllRadioButton.Click, xKenRadioButton.Click, xOreRadioButton.Click, xWisRadioButton.Click
		Call SelectStateAndCapital()
		' clear message box
		Me.xMsgLabel.Text = String.Empty

	End Sub

	Private Sub ProcessCapitalRadioButtons(ByVal sender As Object, ByVal e As System.EventArgs) _
			Handles xFrankfortRadioButton.Click, xLittleRockRadioButton.Click, xMadisonRadioButton.Click, xSalemRadioButton.Click, xSpringfieldRadioButton.Click
		Call SelectStateAndCapital()
		'clear message box
		Me.xMsgLabel.Text = String.Empty

	End Sub

	Private Sub xVerifyButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles xVerifyButton.Click
		Call SelectStateAndCapital()


		If Me.xArkRadioButton.Checked And Me.xLittleRockRadioButton.Checked Then
			Me.xMsgLabel.Text = "Correct"
		Else
			Me.xMsgLabel.Text = "Incorrect"

			If Me.xIllRadioButton.Checked And Me.xSpringfieldRadioButton.Checked Then
				Me.xMsgLabel.Text = "Correct"
			Else
				Me.xMsgLabel.Text = "Incorrect"

				If Me.xKenRadioButton.Checked And Me.xFrankfortRadioButton.Checked Then
					Me.xMsgLabel.Text = "Correct"
				Else
					Me.xMsgLabel.Text = "Incorrect"
					If Me.xOreRadioButton.Checked And Me.xSalemRadioButton.Checked Then
						Me.xMsgLabel.Text = "Correct"
					Else
						Me.xMsgLabel.Text = "Incorrect"
						If Me.xWisRadioButton.Checked And Me.xMadisonRadioButton.Checked Then
							Me.xMsgLabel.Text = "Correct"
						Else
							Me.xMsgLabel.Text = "Incorrect"
						End If
					End If

				End If
			End If
		End If

	End Sub
End Class]



[b]*Always use code blocks => :code:

This post has been edited by PsychoCoder: 03 November 2007 - 07:19 PM


Is This A Good Question/Topic? 0
  • +

Replies To: radio buttons

#2 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: radio buttons

Posted 03 November 2007 - 02:50 PM

There are a couple ways you could have tackled this. First being instead of using radio buttons to use something like two drop downs. One for states, one for capitals and then use the button to verify.

The second choice is to use something like the "tag" property of your radio button controls. This property is used for general information. In this property you could use something like a number (1 - 50 for your states) and then the same number for the state's capital (1-50 for the capitals). Then what you would do is during the verify event of your button read in the two selected controls and compare their tag values looking for the match.

If you wanted to even go shorter, you could create your radio buttons using a loop which creates the two radio buttons at a time (one state and one capital), assigns the counter to their tag properties, and then add them to the form in any order you chose.

But those are some ideas for you. Hope that helps! :)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: radio buttons

Posted 03 November 2007 - 07:20 PM

Moved to VB.Net Forum :)
Was This Post Helpful? 0
  • +
  • -

#4 raeNet  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 01-November 07

Re: radio buttons

Posted 03 November 2007 - 08:38 PM

View PostMartyr2, on 3 Nov, 2007 - 02:50 PM, said:

There are a couple ways you could have tackled this. First being instead of using radio buttons to use something like two drop downs. One for states, one for capitals and then use the button to verify.

The second choice is to use something like the "tag" property of your radio button controls. This property is used for general information. In this property you could use something like a number (1 - 50 for your states) and then the same number for the state's capital (1-50 for the capitals). Then what you would do is during the verify event of your button read in the two selected controls and compare their tag values looking for the match.

If you wanted to even go shorter, you could create your radio buttons using a loop which creates the two radio buttons at a time (one state and one capital), assigns the counter to their tag properties, and then add them to the form in any order you chose.

But those are some ideas for you. Hope that helps! :)

Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1639
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: radio buttons

Posted 03 November 2007 - 08:52 PM

Is there a statement or question in there somewhere??
Was This Post Helpful? 0
  • +
  • -

#6 raeNet  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 01-November 07

Re: radio buttons

Posted 03 November 2007 - 08:53 PM

Thank you for the suggestions; however, I'm quite the novice where VB.NET is concerned and haven't used or learned the tag property yet, so I'm not sure how receptive my prof. would be. This is an assignment, but as usual the textbook example doesn't resemble the assigned work, thus the reason for my excessive coding. If you have any other suggestions, I'm all ears. Don't get me wrong, I don't want someone to do the work for me, I just need some guidance.

I thought that in the verify control, I could somehow use the coding such as:

[If choice = capital then]
[message = "Correct"]
[else message = "Incorrect"]

But, this makes all selections correct???

I don't believe I was supposed to have so much code in the verify section.

THX!
Was This Post Helpful? 0
  • +
  • -

#7 Martyr2  Icon User is online

  • Programming Theoretician
  • member icon

Reputation: 4319
  • View blog
  • Posts: 12,100
  • Joined: 18-April 07

Re: radio buttons

Posted 03 November 2007 - 09:16 PM

Well the way you have designed it you have essentially 100 radio buttons, that is a lot of checking. I have told you ways of simplifying the code by dynamically making the controls and such. The loop to make your controls would be roughly 10 to 20 lines.

:)
Was This Post Helpful? 0
  • +
  • -

#8 raeNet  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 136
  • Joined: 01-November 07

Re: radio buttons

Posted 03 November 2007 - 09:50 PM

View PostMartyr2, on 3 Nov, 2007 - 09:16 PM, said:

Well the way you have designed it you have essentially 100 radio buttons, that is a lot of checking. I have told you ways of simplifying the code by dynamically making the controls and such. The loop to make your controls would be roughly 10 to 20 lines.

:)



WOW! I am way off then. Actually, the assignment has (2) radio groups: one with (5) states and one with (5) capitals.

After designating the first radio button in each group as the default for each group, we were to enter the code to invoke the Click event for the two default radio buttons when the form is read into the computer's internal memory.

Do you know how to go about that?

THX!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1