Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MessageBox not showing (focused) after SaveFileDialog

For some reason after my SaveFileDialog, my app will never show the MessageBox. Is there something I'm missing? Or is this a threading issue?

I run the application as a Windows Form application using VS 2010 Express.

I do not get any exceptions.

To add: When I step through the code, all seems to go well. Which is weird, so I believe it is a timing issue.

Pointed out by LarsTech and others, the MessageBoxes do show up, however the focus is gone; in other words the MessageBox is pushed behind other windows or minimized. This is a problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using System.IO;
namespace SpeedDating
{
    class Program
    {
         [STAThread]
        static void Main(string[] args)
        {

        string filename = "test.test"; // args[0];
        string ext = filename.Substring(filename.LastIndexOf('.'));
        SaveFileDialog dialog = new SaveFileDialog();
        dialog.Title = "SpeedDating App";
        dialog.RestoreDirectory = true;
        dialog.CheckFileExists = false;
        dialog.CheckPathExists = false;
        dialog.FileName = DateTime.Now.ToString("yyyyMMdd") + ext;

        DialogResult result = dialog.ShowDialog();
        if (result == DialogResult.OK && dialog.FileName != "")
        {
            try
            {
                FileStream outfs = File.Create(dialog.FileName);
                FileStream infs = File.Open(filename, FileMode.Open);
                infs.CopyTo(outfs);
                infs.Close();
                outfs.Close();
            }
            catch (NotSupportedException ex)
            {
                MessageBox.Show("Probably removed the original file.");
            }
        }
        else
        {
            MessageBox.Show("No path found to write to.");
        }

        MessageBox.Show("I came here and all I got was this louzy printline");

        }
    }
}
like image 939
RobotRock Avatar asked May 31 '13 13:05

RobotRock


3 Answers

I tried this one and it showed right away:

 MessageBox.Show(new Form() { WindowState = FormWindowState.Maximized, TopMost = true }, "You clicked Cancel button", "Cancel");
like image 89
Edper Avatar answered Oct 20 '22 08:10

Edper


Try this for your Message Box.

MessageBox.Show(this,"Probably removed the original file.");
like image 33
Bearcat9425 Avatar answered Oct 20 '22 08:10

Bearcat9425


I created a new project and pasted your code and it works for me. Make sure you have done a full rebuild before running. Also, with this line:

dialog.FileName = DateTime.Now.ToString(format) + "." + ext;

The dialog box will have a file name to begin with. Therefore, only hitting the cancel button (assuming you don't clear the save dialog first) will trigger the message box. Either way, I got the message box to pop up by failing your IF test either way. Your code looks alright.

like image 36
Mike Avatar answered Oct 20 '22 10:10

Mike