-7

I have bound the openFileDialog control with button. On button event, I am calling the openFileDialog control.

Now, when I am running my application, the openFileDialog box gets open but does not select the file. The open button is not working.

Screen capture of OpenFileDialog

Example code:

    private void button1_Click(object sender, EventArgs e)
    {
       openFileDialog1.ShowDialog();

    }

    private void openFileDialog1_FileOk_1(object sender, CancelEventArgs e)
    {
        // logic written here
    }

It was working fine earlier but now its not working.

Theraot
  • 31,890
  • 5
  • 57
  • 86
Ashish Sharma
  • 131
  • 1
  • 5
  • 19
  • Put your code where you are opening this dialouge – Abdur Rahim Feb 08 '16 at 07:48
  • What exactly happens when you click Open, and what did you expect to happen? – Theraot Feb 08 '16 at 07:48
  • Can you provide an example in code of how you are calling this dialog? What are you doing with the result after you display the dialog? – Lemonseed Feb 08 '16 at 07:49
  • 1
    What does "the open button is not working" entail? errors? invalid results? is "datasheet.xlsx on the desktop? – Sayse Feb 08 '16 at 07:58
  • I think people are here to gain the reputations only on editing the questions. The question is easier to understand. Thanks for making so. I will get the solution on my own. – Ashish Sharma Feb 08 '16 at 09:11

3 Answers3

4

You need to use the DialogResult to get the event of open confirmation by the user. Then you can use a stream to read the file. Here is some sample code (provided by MS in the MSDN - source:https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog(v=vs.110).aspx):

private void button1_Click(object sender, System.EventArgs e)
{
    Stream myStream = null;
    OpenFileDialog openFileDialog1 = new OpenFileDialog();

    openFileDialog1.InitialDirectory = "c:\\" ;
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
    openFileDialog1.FilterIndex = 2 ;
    openFileDialog1.RestoreDirectory = true ;

    if(openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        try
        {
            if ((myStream = openFileDialog1.OpenFile()) != null)
            {
                using (myStream)
                {
                    // Some logic here
                }
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: Failed to open file. Original error: " + ex.Message);
        }
    }
}
Gnqz
  • 3,292
  • 3
  • 25
  • 35
  • 2
    This is the example provided in [this MSDN article](https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.aspx). @AshishSharma you can read for more specifics on how to properly implement. – Lemonseed Feb 08 '16 at 07:58
  • 1
    If you're going to post code from a source you should always give credit to it... regardless of how simple it is – Sayse Feb 08 '16 at 08:03
2

Answer post edits

The included code shows the method openFileDialog1_FileOk_1 the "_1" at the end suggest to me that you had some issues binding the event. Perhaps there was at some point a method openFileDialog1_FileOk caused conflict.

You should check if the method is correctly bound to the event.

For that I will remit you to my answer to How to change the name of an existing event handler?

For abstract you want to see what method is bound to the event. You can do it from the properties panel, or by checking the form designer file (that is named something .Designer.cs for example: Form1.Designer.cs).

Addendum: Consider adding a break point in the event handler. It will allow you to debug step by step what is happening. Also, it will allow you notice if the event handlers is not being executed at all, which would suggest that the method is NOT correctly bound to the event.


Original Answer

OpenFileDialog Does not open files, it barely select thems and makes the selection available for your application to do whatever your applications is intended to do with those files.

The following is the usage pattern from the MSDN article linked above:

if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
    try
    {
        if ((myStream = openFileDialog1.OpenFile()) != null)
        {
            using (myStream)
            {
                // Insert code to read the stream here.
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show
        (
            "Error: Could not read file from disk. Original error: " + ex.Message
        );
    }
}

Now, observe that after cheking the result of ShowDialog - which returns once the dialogs is closed - the codes uses the method OpenFile to actually open the file. The result is a stream that you can process anyway you prefer.

Alternatively you can retrieve the selected files via the property FileNames, which returns an array of strings. If you have configured the dialog to only allow selecting asingle file you are ok to use FileName instead.


Addendum, if by "Open" you mean to invoke the default application associated with the selected file to open the file. You can accomplish that by passing the file path to System.Diagnostics.Process.Start.

Community
  • 1
  • 1
Theraot
  • 31,890
  • 5
  • 57
  • 86
0
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string name = openFileDialog1.FileName;
object objOpt = Type.Missing;
                var excelApp = new Excel.Application();
                Excel.Workbook wbclass = excelApp.Workbooks.Open(name, objOpt, 
                                                                true,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt,
                                                                objOpt);

            }
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83