MessageBox is not displayed (focused) after SaveFileDialog

For some reason, after my SaveFileDialog, my application will never show a MessageBox. Is there something I'm missing? Or is this a problem with threads?

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

I have no exceptions.

To add: When I look at the code, everything seems to be going well. Which is strange, so I think this is a problem of time.

Indicated by LarsTech and others, MessageBoxes do appear, however the focus is gone; in other words, the MessageBox is being squeezed out of other windows or minimized. This is 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"); } } } 
+7
source share
4 answers

I tried this, and he immediately showed:

  MessageBox.Show(new Form() { WindowState = FormWindowState.Maximized, TopMost = true }, "You clicked Cancel button", "Cancel"); 
+4
source

Try this for your message box.

 MessageBox.Show(this,"Probably removed the original file."); 
+2
source

I created a new project and pasted your code, and it works for me. Before starting, make sure that you have completed a complete rebuild. Also with this line:

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

A dialog box will display the file name. Therefore, only pressing the cancel button (provided that you do not clear the save dialog at the beginning) will bring up a message box. Anyway, I got a message box that popped up if you didn't pass the IF test anyway. Your code looks good.

+1
source

Perhaps you should put SaveFileDialog in use to ensure that it is removed before the MessageBox is called:

 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('.')); using (SaveFileDialog dialog = new SaveFileDialog()) { dialog.Title = "SpeedDating App by K.Toet"; 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"); } } } 
+1
source

All Articles