[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.
Re: Boolean setting on a checkbox, How it performs checked event?
Quote:
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?
3 Attachment(s)
Re: Boolean setting on a checkbox, How it performs checked event?
Of course.
1) Consider following form:
Attachment 188354
2) Already made a boolean setting:
Attachment 188353
2) Made sure that setting is linked to "checked" property of a checkbox:
Attachment 188352
3) Here's the code:
Code:
Public Class Form2
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked = True Then
Label1.Text = "METRIC"
Else
Label1.Text = "IMPERIAL"
End If
End Sub
End Class
4) Result: When the form opens in runtime, it keeps the value I changed before and save in application shutdown (Works fine) but the label - and possible other controls to be triggered relied to situation of UNIT boolean thing - is not working. The string "Label1" (Default text) is on label.
5) What is missing? How can I trigger the checkbox to see what does it contain in itself? Should I change the way I use vb.net applicationsettings?
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
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
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.
Re: Boolean setting on a checkbox, How it performs checked event?
Quote:
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.
Re: Boolean setting on a checkbox, How it performs checked event?
Quote:
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
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")