Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I clear and refresh a listbox? 1

Status
Not open for further replies.

emaduddeen

Programmer
Mar 22, 2007
184
US
Hi Everyone,

I have a listbox that does not clear. I issued this command while the listbox has focus:

Code:
 lstDBAData.Items.Clear()

Can you tell me what I'm missing to get the listbox to clear? Do I need to get focus away from the listbox? If so how do I do that?

Thanks in advance.

Truly,
Emad
 
Setting the focus to another control is easy. for example, to set the focus to textbox1, simply code: textbox1.focus()

As far as your listbox not clearing, it should. Your code looks ok. Try clearing the listbox when it does NOT have focus, as this would make more sense. I would try clearing it, say as I lsft the listbox or from a button click.
 
Hi PRPhx,

Thanks for the reply.

It helps me a lot.

Truly,
Emad
 
If the list box is bound it will not clear. You have to remove the binding first. Otherwise I'm not sure what is happening.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Hi Sorwen,

It was not bound. I was filling it up like this:

Code:
    Private Sub ShowDataFiles()
        lstDBAData.Items.Clear()
        lstMSData.Items.Clear()

        ' Display all files from the DBA and MS Data folders in a listbox.
        '-----------------------------------------------------------------
        Dim folderDBADataInfo As New DirectoryInfo(strSourceDBADataFolder)
        Dim arrFilesInDBADataFolder() As FileInfo
        Dim objFileInDBADataFolder As FileInfo
        Dim folderMSDataInfo As New DirectoryInfo(strSourceMSDataFolder)
        Dim arrFilesInMSDataFolder() As FileInfo = Nothing
        Dim objFileInMSDataFolder As FileInfo

        ' Fill the array with the files in the DBA folder.
        '-------------------------------------------------
        arrFilesInDBADataFolder = folderDBADataInfo.GetFiles("*.*")

        For Each objFileInDBADataFolder In arrFilesInDBADataFolder
            lstDBAData.Items.Add(objFileInDBADataFolder.Name)
        Next

        ' Fill the array with the files in the DBA folder.
        '-------------------------------------------------
        arrFilesInMSDataFolder = folderMSDataInfo.GetFiles("*.*")

        For Each objFileInMSDataFolder In arrFilesInMSDataFolder
            lstMSData.Items.Add(objFileInMSDataFolder.Name)
        Next
    End Sub

And I used this code to remove items in the list box after the user chose to delete an item.

Code:
    Private Sub btnDeleteDBADataFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDeleteDBADataFile.Click
        If MessageBox.Show("Your are about to remove the " & strDbaDataFileToDelete & _
            " file." & vbCrLf & vbCrLf & _
            "Do you REALLY want to REMOVE this?", "Important", _
            MessageBoxButtons.YesNo, _
            MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then

            My.Computer.FileSystem.DeleteFile(strDestinationFolder & "\" & _
                                              strDbaDataFileToDelete)

            lstDBAData.Items.RemoveAt(lstDBAData.SelectedIndex)

            If lstDBAData.Items.Count > 0 Then
                lstDBAData.SelectedIndex = 0
            End If
        End If
    End Sub

All is working fine now.

Truly,
Emad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top