Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is my code: Public Class frmDepartment ? ? ? Private Sub btnComp_Click(send

ID: 3547866 • Letter: T

Question

This is my code:

Public Class frmDepartment

?

     

?

?

    Private Sub btnComp_Click(sender As Object, e As EventArgs) Handles btnComp.Click

        If btnComp.Enabled Then

            txtSpecific.Text = CStr("Computer Science")

            txtNumber.Text = CStr("Number of Majors:8")

        End If

        If btnComp.Enabled Then

            lstChosen.Items.Add("Barbara He")

            lstChosen.Items.Add("Michael Dorn")

            lstChosen.Items.Add("Julia White")

            lstChosen.Items.Add("Jonathan Rubin")

            lstChosen.Items.Add("Matthew Singh")

            lstChosen.Items.Add("Xiang Fu")

            lstChosen.Items.Add("Peter Rosenbaum")

            lstChosen.Items.Add("Joann Greenberg")

?

?

        End If

      

?

?

?

?

?

?

    End Sub

?

?

?

?

?

?

    Private Sub btnIT_Click(sender As Object, e As EventArgs) Handles btnIT.Click

        If btnIT.Enabled Then

            txtSpecific.Text = CStr("Informational Technology")

            txtNumber.Text = CStr("Number of Majors:8")

        End If

        If btnIT.Enabled Then

            lstChosen.Items.Add("John Alexander")

            lstChosen.Items.Add("Nicholas Johnson")

            lstChosen.Items.Add("Chantel Davidson")

            lstChosen.Items.Add("Alex Kaplan")

            lstChosen.Items.Add("Vivian Lin")

            lstChosen.Items.Add("Amanda Jones")

            lstChosen.Items.Add("Elizabeth Lu")

            lstChosen.Items.Add("Lauren DeLusso")

        Else

            If btnComp.Enabled Or btnLiberal.Enabled Then

                lstChosen.ClearSelected()

            End If

        End If

    End Sub

    Private Sub btnLiberal_Click(sender As Object, e As EventArgs) Handles btnLiberal.Click

        If btnLiberal.Enabled Then

            txtSpecific.Text = CStr("Liberal Arts")

            txtNumber.Text = CStr("Number of Majors:8")

        End If

        If btnLiberal.Enabled Then

            lstChosen.Items.Add("Marilyn Montgomery")

            lstChosen.Items.Add("James Martin")

            lstChosen.Items.Add("Susan DeLillo")

            lstChosen.Items.Add("John Nelson")

            lstChosen.Items.Add("Jeri Heckman")

            lstChosen.Items.Add("John Page")

            lstChosen.Items.Add("Janes Padavano")

            lstChosen.Items.Add("Vinny Batista")

        Else

            If btnComp.Enabled Or btnIT.Enabled Then

                lstChosen.ClearSelected()

            End If

        End If

    End Sub

What I need to be able to do is clear the listbox when clicking other buttons without adding another button specifically to reset it. When btnComp is clicked then the List Box should display what i have in code and etc with other buttons. i know the code to clear the listbox is "lstchosen.items.clear" but it doesnt clear and when i click the other buttons or the same one it just keeps going down the list. without erasing so it may repeat over and over again a numerous amount of times. Please if someone could help me I'd really appreciate it!!!

Explanation / Answer

try this //


lstChosen.Items.Clear() should clear the listbox unless it was filled in the first place using data-binding rather than adding objects 'manually' to the Items collection.

If that's the case, then lstChosen.DataSource = Nothing should clear it the first time but, thereafter, you'll have to use lstChosen.Items.Clear(). So - in other words - use both of these lines together.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote