mardi 19 octobre 2021

CheckBox Data to ListBox (VBNet)

the problem with my code is that when i initially select a particular checkbox it works well and displays selected item in listbox, but when selected again it creates another entry within the listbox and when i remove it i have to uncheck the same checkbox the times it has been displayed in the listbox when i click the btnSubmit.

Can someone tell me what's wrong with the code, thank you so much

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    If CheckBox1.Checked Then
        ListBox1.Items.Add(CheckBox1.Text)
    Else
        ListBox1.Items.Remove(CheckBox1.Text)
    End If
    If CheckBox2.Checked Then
        ListBox1.Items.Add(CheckBox2.Text)
    Else
        ListBox1.Items.Remove(CheckBox2.Text)
    End If
    If CheckBox3.Checked Then
        ListBox1.Items.Add(CheckBox3.Text)
    Else
        ListBox1.Items.Remove(CheckBox3.Text)
    End If
    If CheckBox4.Checked Then
        ListBox1.Items.Add(CheckBox4.Text)
    Else
        ListBox1.Items.Remove(CheckBox4.Text)
    End If
    If CheckBox5.Checked Then
        ListBox1.Items.Add(CheckBox5.Text)
    Else
        ListBox1.Items.Remove(CheckBox5.Text)
    End If
    If CheckBox6.Checked Then
        ListBox1.Items.Add(CheckBox6.Text)
    Else
        ListBox1.Items.Remove(CheckBox6.Text)
    End If
End Sub



Aucun commentaire:

Enregistrer un commentaire