So I currently have a form where I have 4 teams, each with 4 teammates. This cannot change.
Initially when the program starts, it automatically selects the “current value” radio button which displays labels with blank data, you can then select “edit values” which can be saved by pressing the big green apply button. This stores the data under a 2d array named stTeamName, which the labels reference whenever you select “current value” again.
There are 4 radio buttons which allow you to switch between the teams.
My problem is, when I change the radio buttons, any data put into the text labels that has not been saved will be deleted.
I solved this problem by adding a message box which will stop the data in the labels from changing unless the user gives confirmation to do so.
However, my only problem is that after I cancel this, the radio button stays at the current selection which does not help.
I solved this issue by using
rbTeamName1.Checked = True (rbTeamname 1 to 4 being the radio buttons named team 1-4) to switch back to the previous state.
This of course only goes back to rbTeamname1 no matter what, so I employed an 1d array and select case to help solve this issue.
Dim RadioCounter(1) As Integer
the code below simply gives the default Radio button value on load.`
Sub DeclareRadio(RadioButton) RadioButton(1) = 1
Whenever a radio button is successfully selected, RadioCounter(1) is set to the number 1-4 respectively. Whenever a new radio button is selected, the value of RadioCounter(1) is moved to RadioCounter(0) and the current radio button’s value is to RadioCounter(1).
If the user selects no, RadioCounter(0)’s value is referenced and a select case selects the radio button.
(Please note, RadioTeam() is used as a counter to store Team name and Teammate name string variables correctly within the 2d array: stTeamName. I will most likely merge this with RadioCounter as soon as I get this problem fixed)
Private Sub rbTeamName1_CheckedChanged(sender As Object, e As EventArgs) Handles rbTeamName1.CheckedChanged
'note txtTeamName starts at 0, however RadioTeam starts at 1, RadioTeam = 0 has no team designated to it. 'this function is for swapping teams via radio button, each time, a confirmation is given to make sure the user saves the current team, or risk deletion of the inputted data. RadioCounter(1) = RadioCounter(0) stTeamname(0, RadioTeam) = txtTeamName.Text stTeamname(1, RadioTeam) = txtMate1.Text stTeamname(2, RadioTeam) = txtMate2.Text stTeamname(3, RadioTeam) = txtMate3.Text stTeamname(4, RadioTeam) = txtMate4.Text Dim Dialog As DialogResult Dialog = MessageBox.Show("Are you sure you want to change team? Any unsaved changes will be lost. If this box shows on starting the program, just press no", "Change team?", MessageBoxButtons.YesNo) If Dialog = DialogResult.No Then 'need to run function to check previous result of radio button' Select Case RadioCounter(0) Case 1 rbTeamName1.Checked = True Case 2 rbTeamName2.Checked = True Case 3 rbTeamName3.Checked = True Case 4 rbTeamName4.Checked = True End Select ElseIf Dialog = DialogResult.Yes Then RadioCounter(1) = 1 'this equals the respective number for each radio button' RadioTeam = 1 txtTeamName.Text = stTeamname(0, RadioTeam) txtMate1.Text = stTeamname(1, RadioTeam) txtMate2.Text = stTeamname(2, RadioTeam) txtMate3.Text = stTeamname(3, RadioTeam) txtMate4.Text = stTeamname(4, RadioTeam) lblTeamName.Text = stTeamname(0, RadioTeam) lblMate1.Text = stTeamname(1, RadioTeam) lblMate2.Text = stTeamname(2, RadioTeam) lblMate3.Text = stTeamname(3, RadioTeam) lblMate4.Text = stTeamname(4, RadioTeam) End If End Sub
….Or it should do that. But it doesnt. Im not sure why, but when I force it to check a specific radio button, by removing the select case and only running
rbTeamName4.Checked = True it will work perfectly fine, theres something about the select case that simply doesnt run the code.
stTeamname(0, RadioTeam) = txtTeamName.Text etc. being given before the messagebox shows, if I do not press the apply button to save, press another team button, then press no, it will still not save those parameters, which I find extremely weird.
What is the ideal solution to this?
If a user writes team names and teammates and does not press the apply button to save and then changes team by selecting another radio button, a text box should appear asking whether they want to switch team and lose the data (Yes) or stay on the previous radio button to edit and apply the data (No).
Upon pressing no, the program automatically switches the current radio button to the previous selection to make it seem as if no radio button was selected in the first place.
And im not sure if I have to make it obvious but im new to both stackoverflow and VB.net in general. Thank you in advance.
Leave an answer