Results 1 to 9 of 9

Thread: [RESOLVED] Boolean setting on a checkbox, How it performs checked event?

  1. #1

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Resolved [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.

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Boolean setting on a checkbox, How it performs checked event?

    Quote Originally Posted by pourkascheff View Post
    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!

  3. #3

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    Re: Boolean setting on a checkbox, How it performs checked event?

    Of course.
    1) Consider following form:
    Name:  1FORM.jpg
Views: 955
Size:  90.9 KB

    2) Already made a boolean setting:
    Name:  2SETT.jpg
Views: 958
Size:  203.7 KB

    2) Made sure that setting is linked to "checked" property of a checkbox:
    Name:  3PROP.jpg
Views: 939
Size:  189.1 KB

    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?

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    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
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6

    Thread Starter
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    384

    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.

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: Boolean setting on a checkbox, How it performs checked event?

    Quote Originally Posted by pourkascheff View Post
    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.

  8. #8
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Boolean setting on a checkbox, How it performs checked event?

    Quote Originally Posted by OptionBase1 View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. If CheckBox1.Checked Then
    2.     Label1.Text = "METRIC"
    3. Else
    4.     Label1.Text = "IMPERIAL"
    5. End If
    to this:
    vb.net Code:
    1. Label1.Text = If(CheckBox1.Checked, "METRIC", "IMPERIAL")
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width