Activate and deactivate a button according to the selection in MS Visual Basic checklistbox?

0

Cordial greeting colleagues, it turns out that I have a CRUD in MS Visual Basic, connected to a database called quote, works perfectly, I use a checklistbox that is filled with the data of the bd and I also use it to select the records to those that I want to apply the update or delete, what I am trying to do now is some user validations and among them, that the update and delete button are deactivated when opening the form, this I do using the following statements in the load of the form:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        btnactualizar.Enabled = False
        btneliminar.Enabled = False
    End Sub

Now what I try to do is that if I select a content of the checklistbox, these buttons are enabled and if I remove this selection they are deactivated again. use this statement in the checklistbox:

Private Sub checklistestudios_SelectedIndexChanged(sender As Object, e As EventArgs) Handles checklistestudios.SelectedIndexChanged

If checklistestudios.SelectedIndex >= 0 Then


            btnactualizar.Enabled = True
            btneliminar.Enabled = True
        ElseIf checklistestudios.SelectedIndex < 0 Then
            btnactualizar.Enabled = False
            btneliminar.Enabled = False
        End If
    End Sub

It works at the moment of selecting something in the checklistbox, activates the buttons, but if I remove the selection the buttons are still active and what I'm looking for is that they deactivate if there is nothing wrong in the checklistbox, how could I do it? / p>     

asked by Kevin Burbano 13.02.2018 в 21:02
source

1 answer

1

Use the ItemCheck of your CheckListBox and make 3 comparisons:

  • if I select > 1 then activate the buttons
  • if you select = 1 and deactivate the check then deactivate the buttons
  • If you select = 0 and activate a check then activate the buttons

Example:

Private Sub CheckedListBox1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles CheckedListBox1.ItemCheck
    If CheckedListBox1.CheckedItems.Count > 1 Then
        btnactualizar.Enabled = True
        btneliminar.Enabled = True
    End If

    If CheckedListBox1.CheckedItems.Count = 1 And e.NewValue = CheckState.Unchecked Then
        btnactualizar.Enabled = False
        btneliminar.Enabled = False
    End If

    If CheckedListBox1.CheckedItems.Count = 0 And e.NewValue = CheckState.Checked Then
        btnactualizar.Enabled = True
        btneliminar.Enabled = True
    End If

End Sub

In this case you would have to change CheckedListBox1 to checklistestudios

    
answered by 13.02.2018 / 22:52
source