none
Checkstate in setting RRS feed

  • Question

  • I want to use Settings for a checkbox

    My head is spinning so much examples i tryed.

    It saved check but not uncheck.

    Can't find the problem. I think it is a small thing what i am doing wrong

    In Settings 

    Checkbox2 , Boolean, User

    Formload
    
     CheckBox2.Checked = My.Settings.Checkbox2

     Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
            Try
                If CheckBox2.CheckState = 1 Then
                    Me.DataGridView2.Columns("Id").Visible = False
                    My.Settings.Checkbox2 = CheckBox2.Checked
    
                    My.Settings.Save()
                End If
                If CheckBox2.CheckState = 0 Then
                    Me.DataGridView2.Columns("Id").Visible = True
                    My.Settings.Checkbox2 = CheckBox2.Checked
    
                    My.Settings.Save()
                End If
            Catch ex As Exception
    
            End Try
           
        End Sub

    Thursday, June 28, 2018 8:05 PM

Answers

  • "

    In Settings 

    Checkbox2 , Boolean, User"

    Perhaps you should use the CheckState enumeration not Boolean in setting declarations..

    Oh and here it is Boolean:

         My.Settings.Checkbox2 = CheckBox2.Checked

    but here it is checkedstate

         If CheckBox2.CheckState = 1

    So pick one or the other.

    In fact why use checkstate at all?

    Describe what it is you are wanting to do exactly?

    More like this??

           

    If CheckBox2.Checked Then Me.DataGridView2.Columns("Id").Visible = False Else Me.DataGridView2.Columns("Id").Visible = True End If        My.Settings.Checkbox2 = CheckBox2.Checked

    My.Settings.Save()


    no this?

            Me.DataGridView2.Columns("Id").Visible = CheckBox2.Checked
    My.Settings.Checkbox2 = CheckBox2.Checked My.Settings.Save()

    PS Edit Changed Checkbox1 to 2.

    Plus, you may need to deal with form load as Viorel has pointed out.


    Thursday, June 28, 2018 8:13 PM

All replies

  • "

    In Settings 

    Checkbox2 , Boolean, User"

    Perhaps you should use the CheckState enumeration not Boolean in setting declarations..

    Oh and here it is Boolean:

         My.Settings.Checkbox2 = CheckBox2.Checked

    but here it is checkedstate

         If CheckBox2.CheckState = 1

    So pick one or the other.

    In fact why use checkstate at all?

    Describe what it is you are wanting to do exactly?

    More like this??

           

    If CheckBox2.Checked Then Me.DataGridView2.Columns("Id").Visible = False Else Me.DataGridView2.Columns("Id").Visible = True End If        My.Settings.Checkbox2 = CheckBox2.Checked

    My.Settings.Save()


    no this?

            Me.DataGridView2.Columns("Id").Visible = CheckBox2.Checked
    My.Settings.Checkbox2 = CheckBox2.Checked My.Settings.Save()

    PS Edit Changed Checkbox1 to 2.

    Plus, you may need to deal with form load as Viorel has pointed out.


    Thursday, June 28, 2018 8:13 PM
  • Maybe you should also execute ‘Me.DataGridView2.Columns("Id").Visible = Not My.Settings.Checkbox2’ inside Form_Load?



    • Edited by Viorel_MVP Thursday, June 28, 2018 8:31 PM
    Thursday, June 28, 2018 8:30 PM
  • Hello,

    There is no need to write "any" code to set or save the Checked setting via My.Settings. The following is for Checked but the same applies for CheckState

    Image denotes by magenta icon property binding.

    1. Select the Checkbox
    2. Select the property window for the CheckBox
    3. Select (ApplicationSettings), click the + to expand
    4. Select "Checked"
    5. Press down arrow
    6. Select "New..."
    7. Provide a name and default value (true or false)
    8. Under project properties, Application tab make sure "Save My.Settings on Shutdown" is checked.
    9. Done


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Thursday, June 28, 2018 9:03 PM
    Moderator
  • Thanks,

    This is the one i use

     If CheckBox2.Checked Then
                Me.DataGridView2.Columns("Id").Visible = False
            Else
                Me.DataGridView2.Columns("Id").Visible = True
           End If
    
           My.Settings.Checkbox2 = CheckBox2.Checked
           My.Settings.Save()

    Thursday, June 28, 2018 9:04 PM
  • Thanks,

    This is the one i use

     If CheckBox2.Checked Then
                Me.DataGridView2.Columns("Id").Visible = False
            Else
                Me.DataGridView2.Columns("Id").Visible = True
           End If
    
           My.Settings.Checkbox2 = CheckBox2.Checked
           My.Settings.Save()


    As I mentioned if you set the checkbox in form load then it may trigger the event sub routine which is probably ok for now but you may want to clean that up.

    Either a variable you set in form load and check in the checked event:  if formload started then save the settings or etc.

    Or Karen shows a more automatic way.

    Thursday, June 28, 2018 9:14 PM