Search code examples
c#web-servicesopenfiledialog

Add prompt after user selects file


I have added an open file dialog box to my client application so that the used can select a specific file they want to send to the web service.

However the file gets sent the moment the file has been selected, whereas I would like to have a secondary prompt e.g. "Send - 'file name' Button Yes. Button No." to pop up after they have selected the file.

This would be incase the user selected the wrong file they would have a chance to see which one they selected.

So far I have the following code -

private void button1_Click(object sender, EventArgs e)
    {

        //Read txt File
        openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        openFileDialog1.FilterIndex = 1;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            StreamReader myReader = new StreamReader(openFileDialog1.FileName);

            myReader.Close();

            string csv = File.ReadAllText(openFileDialog1.FileName);

I need the prompt to come up after they have selected the file but not sure how to do this so any input would be greatly appreciated.


Solution

  • you need to add the second check manually after the first dialog:

    private void button1_Click(object sender, EventArgs e)
    {
    
        //Read txt File
        openFileDialog1.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
        openFileDialog1.FilterIndex = 1;
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if (MessageBox.Show("Message", "Title",MessageBoxButtons.YesNo)==DialogResult.Yes)
            {
                StreamReader myReader = new StreamReader(openFileDialog1.FileName);
                myReader.Close();
                string csv = File.ReadAllText(openFileDialog1.FileName);
    

    etc etc

    Information on MessageBox.Show. You can get information on the possible results/options from here.

    You could ensure that the user sees the file to be uploaded by making the message something like:

    "Are you sure you want to upload " + openFileDialog1.FileName;