none
How to untick a checkbox RRS feed

  • Question

  • Good day,

    Currently when i click the button "Next" or "Previous", the checkbox will still remain the same. 

    I would like to know how to un check the checkbox, "Bring Employee Card" and "Bring Invitation Card" when i click the button "Next".

    Tuesday, March 3, 2020 6:10 AM

Answers

  • Hi Zetsubo69,

    According to your description, I make a test on my side.

    Here the code you can refer to.

        Private dt As DataTable = New DataTable()
        Private number As Integer = 1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("ID", GetType(Integer))
            dt.Columns.Add("Check1", GetType(Boolean))
            dt.Columns.Add("Check2", GetType(Boolean))
        End Sub
        Private Sub AddNew_Click(sender As Object, e As EventArgs) Handles AddNew.Click
            Dim dr As DataRow = dt.NewRow()
            Dim ID As Integer = Convert.ToInt32(TextBox1.Text)
            Dim Check1 As Boolean = CheckBox1.Checked
            Dim Check2 As Boolean = CheckBox2.Checked
            dr("ID") = ID
            dr("Check1") = Check1
            dr("Check2") = Check2
            dt.Rows.Add(dr)
            MessageBox.Show("Save!")
        End Sub
        Private Sub Previous_Click(sender As Object, e As EventArgs) Handles Previous.Click
            If dt.Rows.Count > 0 Then
                number = Convert.ToInt32(TextBox1.Text)
    
                If number > 1 Then
                    Dim dr As DataRow = dt.AsEnumerable().SingleOrDefault(Function(r) r.Field(Of Integer)("ID") = (number - 1))
                    TextBox1.Text = dr("ID").ToString()
                    CheckBox1.Checked = CBool(dr("Check1"))
                    CheckBox2.Checked = CBool(dr("Check2"))
                End If
            End If
        End Sub
    
        Private Sub Next_Click(sender As Object, e As EventArgs) Handles Button3.Click
            If dt.Rows.Count > 0 Then
                number = Convert.ToInt32(TextBox1.Text)
    
                If number < Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) Then
                    Dim dr As DataRow = dt.AsEnumerable().SingleOrDefault(Function(r) r.Field(Of Integer)("ID") = (number + 1))
                    TextBox1.Text = dr("ID").ToString()
                    CheckBox1.Checked = CBool(dr("Check1"))
                    CheckBox2.Checked = CBool(dr("Check2"))
                End If
            End If
        End Sub

    Result of my test:

    Add data to my datatable.

    Check the previous data:

    Check next data:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 4, 2020 2:46 AM
    Moderator

All replies

  • Hi,

    I'm not certain if I can understand your intention.

    If you want to know the checked status of CheckBox when it is changed, you can write like this:
    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) _
    	Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
    	' ---
    	If (Me.CheckBox1.Checked) Then
    		MsgBox("CheckeBox1 = checked")
    	End If
    	If (Me.CheckBox2.Checked = False) Then
    		MsgBox("CheckeBox2 = unchecked")
    	End If
    End Sub
    If you want to know it, when a button is clicked, do like this:
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	If (Me.CheckBox1.Checked) Then
    		MsgBox("CheckeBox1 = checked")
    	End If
    	If (Me.CheckBox2.Checked = False) Then
    		MsgBox("CheckeBox2 = unchecked")
    	End If
    End Sub
    If you want change Checked state, or make it true (checked), do like this:
    ' --- change checked state
    Me.CheckBox1.Checked = True	' -- make it checked
    Me.CheckBox2.Checked = False	' -- make it unchecked
    
    ' --- reverse checked state
    If (Me.CheckeBox1.checed) then
        Me.CheckeBox1.checed = False
    Else
        Me.CheckeBox1.checed = True
    End if

    Regards

    Ashidacchi -- http://hokusosha.com


    • Edited by Ashidacchi Tuesday, March 3, 2020 6:54 AM
    Tuesday, March 3, 2020 6:48 AM
  • The textbox are all data bind to a dataset including the check box.

    When i click next or previous, the checkbox still remain the same as previous data while the other textbox is following the dataset.

    Example, 

    Guy A has his "Bring Employee Card" checked, when i click next or previous.

    The checkbox for that label still remain the same even though Guy B is un check for that label.

    Tuesday, March 3, 2020 6:52 AM
  • Hi,
    the easiest way is to bind CheckBoxes and set/reset property value in bound properties. try following demo:

    Public Class Form86
    
      Private cb1 As New CheckBox With {.Text = "Bring Employee Card", .Top = 10, .Left = 10}
      Private cb2 As New CheckBox With {.Text = "Bring Invitation Card", .Top = 40, .Left = 10}
      Private WithEvents btn1 As New Button With {.Text = "Previous", .Top = 70, .Left = 10}
      Private WithEvents btn2 As New Button With {.Text = "Next", .Top = 70, .Left = 100}
    
      Private bs As New BindingSource
    
      Private Sub Form86_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Controls.AddRange(New Control() {btn2, btn1, cb2, cb1})
        bs.DataSource = GetData()
        cb1.DataBindings.Add("Checked", bs, "Bring_Employee_Card")
        cb2.DataBindings.Add("Checked", bs, "Bring_Invitation_Card")
      End Sub
    
      Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
        bs.MovePrevious()
        ' reset value
        CType(bs.Current, DataRowView)("Bring_Employee_Card") = False
        CType(bs.Current, DataRowView)("Bring_Invitation_Card") = False
        bs.EndEdit()
      End Sub
    
      Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
        bs.MoveNext()
        ' reset value
        CType(bs.Current, DataRowView)("Bring_Employee_Card") = False
        CType(bs.Current, DataRowView)("Bring_Invitation_Card") = False
        bs.EndEdit()
      End Sub
    
      Private Function GetData() As DataTable
        Dim rnd As New Random
        Dim dt As New DataTable
        With dt
          With .Columns
            .Add("Bring_Employee_Card", GetType(Boolean))
            .Add("Bring_Invitation_Card", GetType(Boolean))
          End With
          With .Rows
            For i = 1 To 100
              .Add(rnd.NextDouble > 0.5, rnd.NextDouble > 0.5)
            Next
          End With
        End With
        Return dt
      End Function
    
    End Class


    --
    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Tuesday, March 3, 2020 7:18 AM
  • Hi Zetsubo69,

    According to your description, I make a test on my side.

    Here the code you can refer to.

        Private dt As DataTable = New DataTable()
        Private number As Integer = 1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            dt.Columns.Add("ID", GetType(Integer))
            dt.Columns.Add("Check1", GetType(Boolean))
            dt.Columns.Add("Check2", GetType(Boolean))
        End Sub
        Private Sub AddNew_Click(sender As Object, e As EventArgs) Handles AddNew.Click
            Dim dr As DataRow = dt.NewRow()
            Dim ID As Integer = Convert.ToInt32(TextBox1.Text)
            Dim Check1 As Boolean = CheckBox1.Checked
            Dim Check2 As Boolean = CheckBox2.Checked
            dr("ID") = ID
            dr("Check1") = Check1
            dr("Check2") = Check2
            dt.Rows.Add(dr)
            MessageBox.Show("Save!")
        End Sub
        Private Sub Previous_Click(sender As Object, e As EventArgs) Handles Previous.Click
            If dt.Rows.Count > 0 Then
                number = Convert.ToInt32(TextBox1.Text)
    
                If number > 1 Then
                    Dim dr As DataRow = dt.AsEnumerable().SingleOrDefault(Function(r) r.Field(Of Integer)("ID") = (number - 1))
                    TextBox1.Text = dr("ID").ToString()
                    CheckBox1.Checked = CBool(dr("Check1"))
                    CheckBox2.Checked = CBool(dr("Check2"))
                End If
            End If
        End Sub
    
        Private Sub Next_Click(sender As Object, e As EventArgs) Handles Button3.Click
            If dt.Rows.Count > 0 Then
                number = Convert.ToInt32(TextBox1.Text)
    
                If number < Convert.ToInt32(dt.Rows(dt.Rows.Count - 1)(0)) Then
                    Dim dr As DataRow = dt.AsEnumerable().SingleOrDefault(Function(r) r.Field(Of Integer)("ID") = (number + 1))
                    TextBox1.Text = dr("ID").ToString()
                    CheckBox1.Checked = CBool(dr("Check1"))
                    CheckBox2.Checked = CBool(dr("Check2"))
                End If
            End If
        End Sub

    Result of my test:

    Add data to my datatable.

    Check the previous data:

    Check next data:

    Hope it can help you.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 4, 2020 2:46 AM
    Moderator
  • Hi Zetsubo69,

    Did you solve your problem? If your question has been answered then please click the "Mark as Answer" Link at the bottom of the correct post(s), so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, March 11, 2020 5:45 AM
    Moderator