Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello i got a question if someone can help me out.

I have 1 checkbox named "enablelogs"
And i want to make log file when there is changed option to save it to the file.
And my problem is this.
When the form opens and the checkbox is already checked then when i press the button save to save information to file , to skip it because its already checked.

What i want to to?
- If checkbox is already checked when press button to skip
- If checkbox was changed from checked to unchecked and press button to save the information
- If checkbox was changed to unchecked and again checked to save it again.

Here is my code:
VB
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
        If enableLogs.Checked = True Then
        Logger.log("[Admin Name: " & lblGetName.Text & "]" & " Changed Option" & " " & "Enable Logs")
        Else
        Logger.log("[Admin Name: " & lblGetName.Text & "]" & " Changed Option" & " " & "Disable Logs")
        End If


and here is what it saves everytime in file:
[10:21:56][Admin Name: admin] Changed Option Enable Logs


How to do it if everytime is changed to save, not to dublicate the information in save.
For example if the checkbox is already checked when form load and then i press the button not to display me in log this:
[10:21:56][Admin Name: admin] Changed Option Enable Logs


Any ideas i will appreciate

What I have tried:

i had tryed with button to make saves and also in
enableLogs_CheckedChanged

The results is the same
Posted
Updated 20-Oct-19 9:52am

Use the CheckedChanged Event, see: CheckBox.CheckedChanged Event (System.Windows.Forms) | Microsoft Docs[^]
Also see example here: CheckBox Control in VB.NET[^]

In the CheckedChanged function you can use a boolean variable, set it to true so you know that logging must be turned on.

To detect whether it is the form load:
Public Class Form1

    Public skipCheckedChanged As Boolean

    Private Sub enableLogs_CheckedChanged(sender As Object, e As EventArgs) Handles enableLogs.CheckedChanged
        If skipCheckedChanged Then
            skipCheckedChanged = False
            Return
        End If

        ' Your code here
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        skipCheckedChanged = True
        enableLogs.Checked = True
    End Sub
End Class
 
Share this answer
 
v4
Comments
diablo22 21-Oct-19 3:05am    
i try like this:
Private Sub enableLogs_CheckedChanged(sender As Object, e As EventArgs) Handles enableLogs.CheckedChanged
'If enableLogs.Checked = False Then
' Logger.log("[Admin Name: " & lblGetName.Text & "]" & " Changed Option" & " " & "Disable Logs")
'End If
If enableLogs.CheckState = CheckState.Checked Then
Logger.log("[Admin Name: " & lblGetName.Text & "]" & " Changed Option" & " " & "Enable Logs")
ElseIf enableLogs.CheckState = CheckState.Unchecked Then
Logger.log("[Admin Name: " & lblGetName.Text & "]" & " Changed Option" & " " & "Disable Logs")
Else
'
End If
End Sub
and in my log when form loads it records the information when form loads
[10:03:24][Admin Name: admin] Changed Option Enable Logs
any ideas?
RickZeeland 21-Oct-19 3:23am    
Then maybe you are setting the checkbox somewhere in the initialization ?
diablo22 21-Oct-19 3:35am    
in form loads i have this:
If My.Settings.Logs = False Then
enableLogs.Checked = False
Else
enableLogs.Checked = True
End If
RickZeeland 21-Oct-19 3:44am    
Then maybe you should set a boolean e.g. skipCheckedChanged = True to skip the CheckedChanged event on start of the application.
diablo22 21-Oct-19 3:59am    
any example because i do not understand how should it be?
already try there in my first post i already comment this that i try it.
But the result is the same if the checkbox is already checked it again save log to the file

i cannot make this steps:
1) if form loads and checkbox is already checked - do not log anything
2) if form loads and checkbox is already unchecked - do not log anything

3)if checkbox was checked when form load and i switch it to unchecked - to log in file
4)if checkbox was unchecked when form load and i switch it to checked - to log in file
 
Share this answer
 
Comments
RickZeeland 20-Oct-19 15:59pm    
Please use the "Have a Question or Comment" button to reply.
I will update the solution and hope things will be a bit more clear for you :)
diablo22 21-Oct-19 2:50am    
Okay thank you i didn't noticed how was it, but thank you for the remark will know now for sure.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900