0

I would like to open the openFileDialog at the click of a button. But all my efforts are unsuccessful.

I tried this:

private void button1_Click(object sender, EventArgs e)
        {
            var dialog = new OpenFileDialog();
            if (dialog.ShowDialog() == DialogResult.OK)

            {
                Class1 excel = new Class1(dialog.InitialDirectory, 1);
                string path = dialog.InitialDirectory + ".txt"; //this is to create a text document with the same name
                TextWriter tw = new StreamWriter(path, true);

                tw.Write(excel.ReadCell(0, 0));
                tw.Close();
            }
        }

There always seems to be something going on but the window disappears right away.

Does anyone know your advice?

Thanks

userdavid
  • 27
  • 1
  • 8
  • Probably the property DialogResult for that button is not set to None. – Steve Nov 26 '20 at 10:10
  • What are you targetting: Winforms, WPF, ASP..? YOU should __always__ TAG your questions correctly so one can see it on the questions page! - _the window disappears right away_ Really??? You should set up the dialog befor you show it but it should show just fine. Of course the stuff you later do will not show.. - __And__ you need to use the `dialog`, not some `openFileDialog1` !! – TaW Nov 26 '20 at 10:10
  • @Steve *DialogResult* is set to None – userdavid Nov 26 '20 at 10:22
  • 1
    @TaW Is it better? – userdavid Nov 26 '20 at 10:24
  • This line _string path = dialog.InitialDirectory + ".txt";_ is giving you a filename to write with the name of the directory with appended the .txt extension. It doesn't seems correct. Side note. Could you explain what is disappearing? – Steve Nov 26 '20 at 10:27
  • Looks even better. But still the dialog ought to show up in any case. Do us all a favor and use your best friend, the [debugger](https://msdn.microsoft.com/en-us/library/y740d9d3.aspx)! – TaW Nov 26 '20 at 10:28
  • @Steve Side note is added, the window that pops up is very fast so I don't even know if it's an *openfiledialog* – userdavid Nov 26 '20 at 10:50
  • @TaW with debugger I´m not good freind :( in my language isnť good instructions unfortunately – userdavid Nov 26 '20 at 10:52
  • 1
    Then now is the time to learn it! Really !! Just add a breakpoint at the line with the showdialog and step once.. To set a breakpoint click at the grey part to the left of the line number. It will creat a dark red circle.. - To step press F11 ! – TaW Nov 26 '20 at 11:07
  • @TaW okey I try it, thank you! – userdavid Nov 26 '20 at 11:09
  • @TaW when i click on f11 it throws me into program.cs, is it ok? – userdavid Nov 26 '20 at 11:18
  • 1
    You did set the beakpoint? and then started the program? If not press F5 ! Now your button should come up and when you click it the debugger should stop at the breakpoint.. – TaW Nov 26 '20 at 11:19
  • @TaW Yes my button showed up when I clicked on it nothing happened, I guess it's supposed to show where the debbuger read the code – userdavid Nov 26 '20 at 11:44
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/225162/discussion-between-taw-and-userdavid). – TaW Nov 26 '20 at 11:57
  • Is the click event [Hooked up?](https://stackoverflow.com/questions/33275763/copy-datagridview-values-to-textbox/33276161?s=1|0.3901#33276161) – TaW Nov 26 '20 at 12:00

1 Answers1

-1

Can you try this.

            using (OpenFileDialog upload = new OpenFileDialog())
                {
                    upload.Filter = "Txt Files|*.txt";
                    upload.Title = "Select File";
                    if (upload.ShowDialog() != DialogResult.OK)
                        return;
                    string filePath= upload.FileName;
                    this.fileName = Path.GetFileName(fileName);
                }
Sadullah DOĞAN
  • 335
  • 2
  • 10