|
-
Jul 27th, 2023, 03:09 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Boolean setting on a checkbox, How it performs checked event?
Consider there's a boolean setting called "unit" which true = imperial and false = metric, already set on a checkbox ApplicationSettings: checked property.
The app follows its value once app runs. I set true, closed, opened it is still true. Make it false, it keeps false. But neither checked = true nor checked = false code won't perform. Why? What's missing?
Should I add a sort of performclick recalls or should I change the way I use settings?
All links and videourls are welcome here.
-
Jul 27th, 2023, 05:55 AM
#2
Re: Boolean setting on a checkbox, How it performs checked event?
 Originally Posted by pourkascheff
Consider there's a boolean setting called "unit" which true = imperial and false = metric, already set on a checkbox ApplicationSettings: checked property.
The app follows its value once app runs. I set true, closed, opened it is still true. Make it false, it keeps false. But neither checked = true nor checked = false code won't perform. Why? What's missing?
Should I add a sort of performclick recalls or should I change the way I use settings?
All links and videourls are welcome here.
I'm having trouble following that. Could you post the relevant code?
Please remember next time...elections matter!
-
Jul 27th, 2023, 06:27 AM
#3
Thread Starter
Hyperactive Member
Re: Boolean setting on a checkbox, How it performs checked event?
-
Jul 27th, 2023, 07:09 AM
#4
Re: Boolean setting on a checkbox, How it performs checked event?
air code, untested, use at own risk, no warranties ....
Code:
Public Class Form2
Private Sub Form2_Load() Handels Form2.Load
SetUnits()
End Sub
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
SetUnits()
End Sub
Private Sub SetUnits()
If CheckBox1.Checked = True Then
Label1.Text = "METRIC"
Else
Label1.Text = "IMPERIAL"
End If
End Sub
End Class
-tg
-
Jul 27th, 2023, 08:09 AM
#5
Re: Boolean setting on a checkbox, How it performs checked event?
This should work,
Code:
'in the Form Shown Event
'CheckBox1.Checked = Not CheckBox1.Checked
'CheckBox1.Checked = Not CheckBox1.Checked
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
'don't need the equals true
If CheckBox1.Checked Then
Label1.Text = "METRIC"
Else
Label1.Text = "IMPERIAL"
End If
End Sub
-
Jul 27th, 2023, 09:06 AM
#6
Thread Starter
Hyperactive Member
Re: Boolean setting on a checkbox, How it performs checked event?
Oh, good old "function"ing way and older "toggling twice" friends. Thanks to you two. These are my final solutions in case no more standard ways were available out there.
-
Jul 27th, 2023, 09:12 AM
#7
Re: Boolean setting on a checkbox, How it performs checked event?
 Originally Posted by pourkascheff
Oh, good old "function"ing way and older "toggling twice" friends. Thanks to you two. These are my final solutions in case no more standard ways were available out there.
I think you have to look at this in the same way as if you set the default value directly while in the Designer. If you set it to Checked in the designer, the CheckedChanged event doesn't fire when you start the program to indicate that the box was checked. It is "born" checked.
So, if you need to take some action based on the initial state of the CheckBox when the form is loaded, then you need to perform that check in the Load event.
-
Jul 27th, 2023, 03:26 PM
#8
Re: Boolean setting on a checkbox, How it performs checked event?
 Originally Posted by OptionBase1
I think you have to look at this in the same way as if you set the default value directly while in the Designer. If you set it to Checked in the designer, the CheckedChanged event doesn't fire when you start the program to indicate that the box was checked. It is "born" checked.
So, if you need to take some action based on the initial state of the CheckBox when the form is loaded, then you need to perform that check in the Load event.
Precisely.... that's why I factored it out into a sub and then just called it from form load and checkstate change
-tg
-
Jul 27th, 2023, 08:54 PM
#9
Re: Boolean setting on a checkbox, How it performs checked event?
This doesn't change the principle involved but, for brevity, I would change this:
vb.net Code:
If CheckBox1.Checked Then
Label1.Text = "METRIC"
Else
Label1.Text = "IMPERIAL"
End If
to this:
vb.net Code:
Label1.Text = If(CheckBox1.Checked, "METRIC", "IMPERIAL")
Tags for this Thread
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
|