-
Sep 22nd, 2014, 05:02 AM
#1
Thread Starter
Frenzied Member
CheckBox Logic
Hi,
I have the following query which is NOT working as expected;
Code:
Private Sub chkflg_CheckedChanged(sender As Object, e As EventArgs) Handles chkflg.CheckedChanged
If chkflg.Checked = True Then
If chkflg.Checked = False Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
Return
End If
End If
End Sub
On Form Load, I have a checkbox called chkflag, this can be True or False at any one time. However, if the chkfkag status is TRUE i,e., checked.
Then, the user options to uncheck the checkbox (sent it to FALSE), it should prompt the user to enter Disabled Date - by showing the message above, and set focus to the txtdisabledate.
The above code just doesnt work.. nothing happens when I uncheck the checkbox..
Thanks
-
Sep 22nd, 2014, 05:20 AM
#2
Hyperactive Member
Re: CheckBox Logic
Your code looks to see if a checkbox is ticked and then after looks to see if it is not ticked
Code:
If chkflg.Checked = False Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
Return
End If
Last edited by drawlings; Sep 22nd, 2014 at 05:24 AM.
-
Sep 22nd, 2014, 05:24 AM
#3
Re: CheckBox Logic
Something seems amiss in your logic. If chkflg.Checked = True then how can chkflg.Checked = False be true also?
Please remember next time...elections matter!
-
Sep 22nd, 2014, 05:33 AM
#4
Thread Starter
Frenzied Member
Re: CheckBox Logic
Ok - if I have it as;
Code:
If chkflg.Checked = True Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
Return
End If
At form load, if chkflg is TRUE and Disabled Date not entered. It gives the message "Please enter Disabled Date".. Why?
I ONLY want it to give the message when the checkbox is TRUE then a user options to change it to FALSE..
Thanks
-
Sep 22nd, 2014, 05:53 AM
#5
Re: CheckBox Logic
It's firing off the event during hte load because it's changing... so it's doing exactly what you told it to do. This can be maddening if you're not expecting it, but at the same time it makes sense.
There are two ways to handle this.
1) Remove the handles clause from the event handler. Then wire it up in the Load event of the form, after it's all done with defautl settings and such.
2) Create a form level flag that has a default value of True, at the end of the form load, set it to false. Then in the checked event, check the flag, if it's set, then move on and exit the method, if it isn't set, then proceed with the logic.
personally I usually use option 1. It's a lot easier to deal with than having to modify the logic in various event handlers.
-tg
-
Sep 22nd, 2014, 06:46 AM
#6
Thread Starter
Frenzied Member
Re: CheckBox Logic
Ok - Changed to;
Code:
Private Sub btnSaveEdit_Click(sender As Object, e As EventArgs) Handles btnSaveEdit.Click
If chkflg_Click(sender, e) Then
If chkflg.Checked = False Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
Return
End If
Else
Call SaveEdit()
End If
End Sub
Code:
Private Sub chkflg_Click(sender As Object, e As EventArgs) Handles chkflg.Click
If chkflg.Checked = False Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
Return
End If
End Sub
What am trying to do at btnSaveEdit is;
- Check has the chkflg been unchecked? If Yes, then throw a message to enter the Disabled Date -- and focus to the respective field.
However, if that is NOT true and the chkflg has not been unchecked (even thought Note: it might be False in other occassions like when a user is NOT set active as YET). The idea is ONLY when the chkflg is unchecked should you eneter disabled date. Secondly, there is a Save button so even if the message has been prompted and OK - and the textbox of disabled date focussed, the user might still option to click save button. I want it to check at this stage has chkflg been unchecked - if yes then request the disabledate before saving the rest of the information.
Then Call the sub
SaveEdit()..
I receive an underlined error at this line of the code;
If chkflg_Click(sender, e) Then
Please help - Thank you
Last edited by dr223; Sep 22nd, 2014 at 06:51 AM.
-
Sep 22nd, 2014, 07:45 AM
#7
Re: CheckBox Logic
Ok... you're not new here, or to programming in general... so you're gonna get the uncoffeed version of TG here... (the coffee is still brewing and I've already had to deal with my offshore team, so I'm quite the cranky one)...3.
what the heck is the call to the click event handler even doing inside the button click event? AND you're trying to treat it like a function WHICH IS IT NOT! Even if it was, what would it return? Not to mention you then perform the exact same logic a second time... so the user has a chance of being prompted twice.
annnnnnnd you totally blew off what I was saying about the flag for the load event, which I thought was the issue in the first place.
Meanwhile, you're checking the state of the checkbox on the save button (or that's what you say you want) ... and if it's false, then to prompt them for the date.... OK... but you're already doing that in the CheckChanged event (actually you're using the click event... maybe you should look at the CheckChanged event instead). So you already know if it's been unchecked and have already prompted them.
-tg
-
Sep 22nd, 2014, 08:12 AM
#8
Thread Starter
Frenzied Member
Re: CheckBox Logic
Okk--
but you're already doing that in the CheckChanged event (actually you're using the click event... maybe you should look at the CheckChanged event instead). So you already know if it's been unchecked and have already prompted them.
1) I have removed it from checkchanged event to Click event.. Reason: On Form Load if chkFlg is False - it prompts the message. This shouldn't happen, it should ONLY prompt the message if the status was TRUE and is changed to FALSE. N/B: The default status of the chkflg is FALSE.
2) I want to trigger the chkFlg event when the save button is clicked.. Reason: To check was the ChkFlg status changed from TRUE to FALSE, if YES then request disable date otherwise continue checking other validations before saving which is all in the saveedit() function.
I hope am clearer....
Thanks
-
Sep 22nd, 2014, 09:41 AM
#9
Re: CheckBox Logic
1)- I know that.... that's why I said you had TWO choices... 1 - drop the Handles clause from the handler, and then wire it up LATER in the load even of the form....or 2) Set a FLAG that indicates that you're loading and to skip the check if you're still loading.
2) - I get that too... but that's NOT what you want... just THINK about it for a moment....
The checkbox is checked.
The user clears the check box.
The user get prompted.
The user then clicks close
The user gets prompted (again)
User chucks app out the window
Scenario 2
User opens form
Check box is cleared already (from the previous run)
User clicks Close
User still gets prompted for the date
User chucks app out window
So... step 1 decide just exactly WHEN you want to prompt the user... when the clear the checkbox OR when they save... not both..... OR... you need to know something about the initial state of things.... and whether it has changed..
Here's an evolution of the process...
Code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'At this point the CheckChanged event isn't hooked up, so we can do what ever we want with it.
CheckBox1.Checked = True ' Will NOT fire the event....
'Wire up the CheckedChanged handler
AddHandler CheckBox1.CheckedChanged, AddressOf CheckBox1_CheckedChanged
End Sub
Code:
'See how there is no handles clause on it?
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
If Not CheckBox1.Checked Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
TextBox1.Focus()
'Return '''why? there's no need for this
End If
End Sub
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Not CheckBox1.Checked Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
TextBox1.Focus()
'Return '''why? there's no need for this
Else
Me.Close() ' or call save edit in your case
End If
End Sub
Ok, so now you see that you got two pieces of code that are the same... they should be refactored into a function:
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CheckDisabledDate() Then
Me.Close() ' or call save edit in your case
End If
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
CheckDisabledDate()
End Sub
Private Function CheckDisabledDate() As Boolean
Dim retValue As Boolean = True 'This is the default value, could be False depending on how you want it to react
'The default return is True, so we're assuming we're going to have a clean run
If Not CheckBox1.Checked Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
TextBox1.Focus()
retValue = False ' this isnt' a clean run
End If
Return retValue
End Function
It still has the prompt twice issue though... so we need to know if it's been handled ... best way... check for a date... so we add that to our condition:
Code:
Private Function CheckDisabledDate() As Boolean
Dim retValue As Boolean = True 'This is the default value, could be False depending on how you want it to react
'The default return is True, so we're assuming we're going to have a clean run
If ((Not CheckBox1.Checked) andalso (TextBox1.Text = String.Empty)) Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
TextBox1.Focus()
retValue = False ' this isnt' a clean run
End If
Return retValue
End Function
Could have just as easily used a form flag - in fact, since I'd probably use a DateTimePicker for the date, I would use a flag:
Code:
Private flgDisableDateSet as Boolean = False
...
Private Function CheckDisabledDate() As Boolean
Dim retValue As Boolean = True 'This is the default value, could be False depending on how you want it to react
'The default return is True, so we're assuming we're going to have a clean run
If ((Not CheckBox1.Checked) andalso (Not flgDisableDateSet )) Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
TextBox1.Focus()
flgDisableDateSet = True
retValue = False ' this isnt' a clean run
End If
Return retValue
End Function
depending on how testing then goes, the CheckChanged event might need to be dealt with:
Code:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs)
CheckDisabledDate()
if CheckBox1.Checked then
flgDisableDateSet = false
end if
End Sub
-tg
-
Sep 23rd, 2014, 05:34 AM
#10
Thread Starter
Frenzied Member
Re: CheckBox Logic
Thank you so much for the detailed analysis of the problem..
Ok - I have the following codes;
Code:
Private Sub chkflg_CheckedChanged(sender As Object, e As EventArgs) Handles chkflg.CheckedChanged
CheckDisabledDate()
End Sub
Code:
Private Function CheckDisabledDate() As Boolean
Dim retValue As Boolean = True
If ((Not chkflg.Checked) AndAlso (txtdisabledate.Text = String.Empty)) Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
retValue = True
End If
Return retValue
End Function
The ONLY problem is when when chkflg is FALSE it triggers the message 2 times before it loads the data on the form. Eventhough, it should just load the data ...
-
Sep 23rd, 2014, 05:59 AM
#11
Re: CheckBox Logic
Sigh.... yes... of course it does... because you've got the handles clause... if you remove it, then use addhandler - as i showed - then it shouldn't do that. You'll want to put the AddHandler command after you've got the data loaded.
-tg
-
Sep 23rd, 2014, 06:42 AM
#12
Thread Starter
Frenzied Member
Re: CheckBox Logic
Ok -
Added this on form load;
Code:
Private Sub FrmUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Dscountry.tblCountry' table. You can move, or remove it, as needed.
Me.TblCountryTableAdapter.FillCountry(Me.Dscountry.tblCountry)
'TODO: This line of code loads data into the 'DsOrgName.tblOrganisation' table. You can move, or remove it, as needed.
Me.TblOrganisationTableAdapter.FillOrgname(Me.DsOrgName.tblOrganisation)
'TODO: This line of code loads data into the 'Dsskeyfobno.tblKeyfob' table. You can move, or remove it, as needed.
Me.TblKeyfobTableAdapter.Fill(Me.Dsskeyfobno.tblKeyfob)
'TODO: This line of code loads data into the 'Dstraininglocation.tblTrainingLocType' table. You can move, or remove it, as needed.
Me.TblTrainingLocTypeTableAdapter.FillTrainLoc(Me.Dstraininglocation.tblTrainingLocType)
'TODO: This line of code loads data into the 'Dskeyfobflg.tblKeyfobflag' table. You can move, or remove it, as needed.
Me.TblKeyfobflagTableAdapter.FillKeyfobFlag(Me.Dskeyfobflg.tblKeyfobflag)
'TODO: This line of code loads data into the 'Dstitle.tblTitleType' table. You can move, or remove it, as needed.
Me.TblTitleTypeTableAdapter.FillTitle(Me.Dstitle.tblTitleType)
Me.MdiParent = FrmMain
Disablegroups()
Call PopulateUserDetails()
mclskyfbHistory = DatabaseAdapter.LoadkyfbHistory()
Call PLVkyfbHistory()
chkflg.Checked = True
AddHandler chkflg.CheckedChanged, AddressOf chkflg_CheckedChanged
End Sub
Code:
Private Sub btnSaveEdit_Click(sender As Object, e As EventArgs) Handles btnSaveEdit.Click
If CheckDisabledDate() Then
Call SaveEdit()
End If
End Sub
Whenever, a form is loaded, regardless if the chkflg is checked or NOT - it calls the messages twice before it uploads the data.
Secondly, when I change the chkflg from TRUE to FALSE - it works OK it triggers the message but the message appears twice before it gets focus to txtdisabledate..
Finally, when I click Save button it just Saves the work - regardless of noting entering the disabed date...
What am I doing worng?
-
Sep 23rd, 2014, 06:46 AM
#13
Re: CheckBox Logic
Originally Posted by dr223
Whenever, a form is loaded, regardless if the chkflg is checked or NOT - it calls the messages twice before it uploads the data.
Secondly, when I change the chkflg from TRUE to FALSE - it works OK it triggers the message but the message appears twice before it gets focus to txtdisabledate..
Finally, when I click Save button it just Saves the work - regardless of noting entering the disabed date...
What am I doing worng?
do you still have the handles clause on it?
Code:
Private Sub chkflg_CheckedChanged(sender As Object, e As EventArgs) Handles chkflg.CheckedChanged' REMOVE this bit
CheckDisabledDate()
End Sub
-tg
-
Sep 23rd, 2014, 06:50 AM
#14
Thread Starter
Frenzied Member
Re: CheckBox Logic
Removed --
Code:
Private Sub chkflg_CheckedChanged(sender As Object, e As EventArgs)
CheckDisabledDate()
End Sub
Still same behaviour....
-
Sep 23rd, 2014, 06:56 AM
#15
Re: CheckBox Logic
then set breakpoints and check the call stack to see what's triggering it.
-tg
-
Sep 23rd, 2014, 11:20 AM
#16
Thread Starter
Frenzied Member
Form Load - Triggers Message
Hi,
The following code is triggered on form load when chkflg is FALSE.
Code:
Private Sub chkflg_CheckedChanged(sender As Object, e As EventArgs) Handles chkflg.CheckedChanged
If ((Not chkflg.Checked) AndAlso (txtdisabledate.Text = String.Empty)) Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
lblactstatus.Text = "chkflgSetFalse"
End If
End Sub
I haven't done any checkedChange but the message is called... Why?
On Form Load, I want it NOT to show the message even if chkflg status is False.. Unless the user changes the chkflg status from True to False..
Thanks
-
Sep 23rd, 2014, 11:32 AM
#17
Re: Form Load - Triggers Message
Are you assigning a value to chkflg in the form load event or is it a bound control?
-
Sep 25th, 2014, 04:51 PM
#18
-
Sep 25th, 2014, 07:09 PM
#19
Re: CheckBox Logic
Originally Posted by dr223
Thank you so much for the detailed analysis of the problem..
Ok - I have the following codes;
Code:
Private Sub chkflg_CheckedChanged(sender As Object, e As EventArgs) Handles chkflg.CheckedChanged
CheckDisabledDate()
End Sub
Code:
Private Function CheckDisabledDate() As Boolean
Dim retValue As Boolean = True
If ((Not chkflg.Checked) AndAlso (txtdisabledate.Text = String.Empty)) Then
MessageBox.Show("Please enter Disabled Date", "Customer Tracker", MessageBoxButtons.OK)
txtdisabledate.Focus()
retValue = True
End If
Return retValue
End Function
The ONLY problem is when when chkflg is FALSE it triggers the message 2 times before it loads the data on the form. Eventhough, it should just load the data ...
whats the point of this function? No matter what you return true.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|