0

First, I have looked at this post. One of the answers seems to offer hope for filtering in ShowDialog by name. Now, here is a description of what I am trying to do:

I have this bit of C# code:

private System.Windows.Forms.OpenFileDialog csv_file_open_dlg;
.
.
.
  csv_file_open_dlg.FileName = "";
  csv_file_open_dlg.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
  int rc = 0;


  if (csv_file_open_dlg.ShowDialog() == DialogResult.OK)
  {
     if (0 == m_csv_file_cmp_counter)
     {
        m_firstCsvFileSelected = csv_file_open_dlg.FileName;
     }
.
.
.

Files that appear in ShowDialog

If I select the first file -- May 18 Muni RosterDetailReport.csv -- and preserve its name in a string variable, is there a way to filter that file out the next time ShowDialog is run in the same directory?

In other words, after selecting May 18 Muni RosterDetailReport.csv, is there a way to feed that name back into ShowDialog, as a filter?

I think the answer is no, but I am just double-checking. If not, is there a workaround by subscribing to the OpenFileDialog event as indicated in the post I noted at the beginning of this post?

Edit:

So it sounds like, I could use the OK event to prevent the user from selecting the first file a second time? I am hoping someone will answer this with an answer.

Community
  • 1
  • 1
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131
  • Have you tried putting that string in the `Filter` property? – Kenneth K. Jul 09 '18 at 18:38
  • @KennethK. The `Fliter` property only works for file extensions, not file names. – Lews Therin Jul 09 '18 at 18:39
  • 3
    That is not possible. You can use the FileOk event to prevent the user from selecting a file. A completely different approach is to move the file into a different directory after you processed it. Pretty sensible thing to do, makes cleanup a lot easier as well. – Hans Passant Jul 09 '18 at 18:40
  • 1
    @LewsTherin Been a while since I've done WinForms...couldn't remember :) – Kenneth K. Jul 09 '18 at 18:40
  • @HansPassant And I can accomplish what I want doing this. So, it's a good answer, if you want to answer the question. – octopusgrabbus Jul 09 '18 at 18:48
  • Just share with us what you decided to do. Complete the Q+A and mark your post as the answer. – Hans Passant Jul 09 '18 at 18:53

1 Answers1

1

Given it is not possible to filter by file name in OpenFileDialog, this is what I did to prevent the user from loading the same file twice:

string m_firstFileLoaded; // a member variable of the class.
.
.
.
private void LoadCsvFile_Click(object sender, EventArgs e)
{
    printb.Enabled = false;
    csv_file_open_dlg.FileName = "";
    csv_file_open_dlg.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
    int rc = 0;

    if (csv_file_open_dlg.ShowDialog() == DialogResult.OK)
    {
        if (0 == m_csv_file_cmp_counter)
        {
            m_firstFileLoaded = csv_file_open_dlg.FileName;
            m_ComparisionDirection = -1; // Resets first and second file toggling.
            m_csv_file_cmp_counter = 0;
            m_csv_file_first.set_csv_path(csv_file_open_dlg.FileName);
            rc = m_csv_file_first.map_csv();
            LoadCsvFile.Text = "Load next csv file";
            m_csv_file_cmp_counter++;
        }
        else
        {
            // If the file is already chosen, throw up a warning.
            if (0 == String.Compare(m_firstFileLoaded, csv_file_open_dlg.FileName))
            {
                MessageBox.Show("You have already loaded " + csv_file_open_dlg.FileName + " . Please select another file", "Attempt to reload first file", MessageBoxButtons.OK);
            }
            else
            {

                m_csv_file_next.set_csv_path(csv_file_open_dlg.FileName);
                rc = m_csv_file_next.map_csv();
                LoadCsvFile.Text = "Files loaded.";
                LoadCsvFile.Enabled = false;
                start_compare.Enabled = true;
            }
        }
    }
}
octopusgrabbus
  • 10,555
  • 15
  • 68
  • 131