[2005] Radio Buttons and Check Boxes
I will try to make this short. Again I have the form for Exercise Logs.
Attachment 60950
When the form loads the check box for the day of the week is enabled and checked. I do that like this;
Code:
' check the day of the week so that is the only check box available to the user
' declare a variable to get the day of the week
Dim DayOfWeek = Now.ToLongDateString
' declare a variable to get today
' break apart the string and get only the day
Dim todaysDate As String = DayOfWeek
Dim ThisDay As String = String.Empty
ThisDay = todaysDate.Substring(0, todaysDate.IndexOf(" "c)).Replace(",", String.Empty).Trim
Dim thisDayNow As String = ThisDay
' call the function WhatDay passing in the day of the week to
' set the check box of the weekday to enabled
WhatDay(thisDayNow)
This is the function that sets the day of the week;
Code:
Private Function WhatDay(ByVal TheDay As String) As String
If TheDay = "Sunday" Then
Me.cbxSunday.Enabled = True
ElseIf TheDay = "Monday" Then
Me.cbxMonday.Enabled = True
ElseIf TheDay = "Tuesday" Then
Me.cbxTuesday.Enabled = True
ElseIf TheDay = "Wednesday" Then
Me.cbxWednesday.Enabled = True
ElseIf TheDay = "Thursday" Then
Me.cbxThursday.Enabled = True
ElseIf TheDay = "Friday" Then
Me.cbxFriday.Enabled = True
ElseIf TheDay = "Saturday" Then
Me.cbxSaturday.Enabled = True
End If
Return TheDay
End Function
Then to enable the appropriate day I do this;
Code:
Sub checkCheckBox()
Dim ctrl As Control = Me.GetNextControl(Me, True)
While ctrl IsNot Nothing
If TypeOf ctrl Is CheckBox And ctrl.Enabled = True Then
DirectCast(ctrl, CheckBox).Checked = True
End If
ctrl = Me.GetNextControl(ctrl, True)
End While
End Sub
I check the radio button Yes or No after asking if the user has exercised today.
What I would like to do is if Yes is checked, then the day of the week is checked, if No is checked then nothing happens.
Is this not valid?
Code:
Sub checkCheckBox()
Dim ctrl As Control = Me.GetNextControl(Me, True)
While ctrl IsNot Nothing
If TypeOf ctrl Is CheckBox And ctrl.Enabled = True And Me.rbtnYes.Checked = True Then
DirectCast(ctrl, CheckBox).Checked = True
Else
DirectCast(ctrl, CheckBox).Checked = False
End If
ctrl = Me.GetNextControl(ctrl, True)
End While
End Sub
If not is there a way to correct it?
Thanks
CoachBarker
Re: [2005] Radio Buttons and Check Boxes
Basically when the user clicks the radio button 'no' hide all your checkboxes that say the days, and when they click 'yes' show them all, and when they click a checkbox, then simply use WhatDay("Day") to determine what day it is.. or whatever your doing..
Is that what you mean?
Re: [2005] Radio Buttons and Check Boxes
try this. it cuts out most of your code
vb Code:
DirectCast(me.controls("cbx" & Now.DayOfWeek.ToString), CheckBox).enabled = true
DirectCast(me.controls("cbx" & Now.DayOfWeek.ToString), CheckBox).Checked = True
vb.net Code:
Private Sub rbtnYes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbtnYes.CheckedChanged
If rbtnYes.Checked = True Then
DirectCast(Me.Controls("cbx" & Now.DayOfWeek.ToString), CheckBox).Checked = True
Else
DirectCast(Me.Controls("cbx" & Now.DayOfWeek.ToString), CheckBox).Checked = False
End If
End Sub
Re: [2005] Radio Buttons and Check Boxes
The check boxes for the days are visual aid so the user knows what days they have exercised. I also have to count the number of days exercised and if in a week it is less than four, display a message stating it is less than the required amount and some other information. All of that works fine.
I thought that there might be a way to have the checkbox checked if the Yes radio button was checked. One less step for the user.
Thanks
CoachBarker
Re: [2005] Radio Buttons and Check Boxes
Re: [2005] Radio Buttons and Check Boxes
Thanks I will give it a try. Seems like every time I get the hang of doing something and work through it the long way, someone comes along and shows a shorter more precise way of doing it. :eek:
CoachBarker
Re: [2005] Radio Buttons and Check Boxes
Oh yes, I understand what you mean now!
Code:
Public CheckBoxDayList() As CheckBox = {SundayCheckBox, MondayCheckBox, TuesdayCheckBox, WednesdayCheckBox, ThursdayCheckBox, SaturdayCheckBox}
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
For Each ExerciseCheckBox As CheckBox In CheckBoxDayList
ExerciseCheckBox.Checked = False
ExerciseCheckBox.Enabled = False
Next ExerciseCheckBox
End Sub
Private Sub ExerciseRadioBYes_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExerciseRadioBYes.Click
Dim CurrentDay As String = Now.DayOfWeek 'This returns an integer I think
CheckBoxDayList(CurrentDay).Checked = True
CheckBoxDayList(CurrentDay).Enabled = True
End Sub
Private Sub ExerciseRadioBNo_Click(ByVal sender As Object, ByVal e As EventArgs) Handles ExerciseRadioBNo.Click
For Each ExerciseCheckBox As CheckBox In CheckBoxDayList
ExerciseCheckBox.Checked = False
ExerciseCheckBox.Enabled = False
Next ExerciseCheckBox
End Sub
Re: [2005] Radio Buttons and Check Boxes
What if they log in tomorrow to fill out info for today? They won't be able to check today's box (tomorrow) if today's box is disabled tomorrow.
Wow! :eek: I think I just confused myself.
Re: [2005] Radio Buttons and Check Boxes
That's true FourBlades, but then it's their fault for missing that day, and they will have to suffer the loss :D lol...
Re: [2005] Radio Buttons and Check Boxes
what about 2 days time when tomorrows yesterday???
Re: [2005] Radio Buttons and Check Boxes
The requirements are that they log in every day, they can check their Glucose Levels multiple times in a day, but only fill in the exercise log once, they can do it anytime in that day if they choose.
COachBarker
Re: [2005] Radio Buttons and Check Boxes
Then my code, or .paul.'s works...
Re: [2005] Radio Buttons and Check Boxes
Re: [2005] Radio Buttons and Check Boxes
Yep I added Pauls, keep trying to get back in and marked this one resolved, but I keep getting replies. Thanks for all the help.
CoachBarker
Re: [2005] Radio Buttons and Check Boxes
You can mark it resolved anytime and we can still reply.
So...what are you waiting for???
:bigyello: