How can I update/refresh a Listbox?

MinusZero

Member
Joined
Oct 22, 2012
Messages
7
Programming Experience
1-3
Hi all,

I am creating a program to assist with an upcoming job I have at work. I will be required to rename nearly 200 files with a different filename (but with same extension).

I have successfully created a program that can rename a file which I select from a listbox, but after renaming, the listbox doesnt update. I have searched around for information on this but without success. How do I make the listbox update after I click to rename a file.

My Windows Forms program contains
a listbox (lbxDir),
a button to list the files in the listbox (btnLbxDir),
a textbox for the new filename (tbxLbxDirNewFilename),
a combo box for the file extension (cboLbxDirExtensions)
a button to rename the file (btnLbxDirRenameFile).
a label which displays the selected directory (lblLbxDirectorySelected)

C#:
private void btnLbxDir_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog folderPicker = new FolderBrowserDialog();
            if (folderPicker.ShowDialog() == DialogResult.OK)
            {
             
                lbxDir.Items.Clear();


                string[] files = Directory.GetFiles(folderPicker.SelectedPath);
                lblLbxDirectorySelected.Text = folderPicker.SelectedPath.ToString() + "\\"; //show selected path in label text


                //add the items in the directory to the listbox without the full path showing.
                foreach (string filename in files)
                {
                lbxDir.Items.Add(System.IO.Path.GetFileName(filename));


                }


            }


        }


        private void btnLbxDirRenameFile_Click(object sender, EventArgs e)
        {
            string newfilename = tbxLbxDirNewFilename.Text;
            string oldfileselect = lblLbxDirectorySelected.Text + lbxDir.SelectedItem.ToString(); 
            string ext = cboLbxDirExtensions.Text.ToString();
            string directory = lblLbxDirectorySelected.Text;
            string fullfilenew = directory + newfilename + ext;
            


           
                    File.Move(oldfileselect, fullfilenew);
                    


            }

Basically, when you click the file list button, it will open a folder dialog. After selecting the directory, the filenames will list in the listbox. I select the file to rename, type a new name in the textbox and specify the extension in the combo and press the rename file button to rename the file. This all works fine. The file renames in the directory selected, but the listbox still shows the old name.

How can i make the listbox update after renaming? I have tried lbxDir.Update(); and lbxDir.Refresh(); and neither worked.

Thanks :)
 
The ListBox simply contains a string. It may contain the path or name of a file but it has no specific relationship to that file. It's just text. It has no knowledge of any change to the file name and, even if it did, it wouldn't care. If you want to display a new string in the ListBox then you have to put a new string in the ListBox. You need to set the item at a specific index to replace the item already there, e.g.
myListBox.Items[index] = newValue;
 
Back
Top Bottom