I have an application that allows the user to select a file and display the folder\filename in a listbox. The problem I am having is when it adds it to the listbox it adds a blank row above it. This is causing a second row to be created in the database with this field being blank. This only happens though with the first file selected. If I go back to add another one it does not add the blank row. It also does not happen if I select multiple files at once to add. In addition to that if I minimize then maximize the application any items listed in the listbox are duplicated.
- private void btnModSel_Click(object sender, EventArgs e)
- {
- System.IO.Stream myStream;
- OpenFileDialog openFiledialog1 = new OpenFileDialog();
- openFiledialog1.FilterIndex = 2;
- openFiledialog1.InitialDirectory = "P:\\Folder\\subfolder";
- openFiledialog1.RestoreDirectory = true;
- openFiledialog1.Multiselect = true;
- openFiledialog1.Title = "Please Select a File";
- if (openFiledialog1.ShowDialog() == DialogResult.OK)
- {
- foreach (String file in openFiledialog1.FileNames)
- {
- try
- {
- if ((myStream = openFiledialog1.OpenFile()) != null)
- {
- using (myStream)
- {
- lstModFiles.Items.Add(file.Substring(52));
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
- }
- }
- }
- }