开发者

C# - SaveFileDialog not showing as the active window

I am doin开发者_开发技巧g a small program to open an excel spreadsheet, update it then save it to a new file then close everything up.

It is all mostly working except when I use the SaveFileDialog the popup window is hidden behind the excel window, on the first attempt only (following attempts, triggered by a button, all work with the SaveFileDialog coming to the fore).

Does anyone know how I get this to happen on the 1st occurance too?

I also want to know if it is possible to not highlight the file name so that the user can append to the filename instead of directly overwriting it, by mistake, usually?

Here is the code I am using for the savefiledialog...

SaveFileDialog saver = new SaveFileDialog();
            saver.FileName = "test";
            saver.DefaultExt = "xls";
            saver.Filter = "Microsoft Office Excel Workbook |(*.xls*)";
            saver.CheckFileExists = false;
            saver.InitialDirectory = "c:\\George";

        if (saver.ShowDialog() == DialogResult.OK)
            //MessageBox.Show("Save Dialog launched");
            excelWorkbook.SaveAs(saver.FileName, Type.Missing, Type.Missing,Type.Missing,
            Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing,
            Type.Missing, Type.Missing, Type.Missing);

Many thanks, George


IIRC you have to set OwnerWndProc to be the main window of the excel application.


A hacky but working solution:

        saver.FileName = "test";
        saver.DefaultExt = "xls";
        saver.Filter = "Microsoft Office Excel Workbook |(*.xls*)";
        saver.CheckFileExists = false;
        saver.InitialDirectory = "c:\\George";
        Form dummyForm = new System.Windows.Forms.Form();
    if (saver.ShowDialog(dummyForm) == DialogResult.OK)
        //MessageBox.Show("Save Dialog launched");
        excelWorkbook.SaveAs(saver.FileName, Type.Missing, Type.Missing,Type.Missing,
        Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing,
        Type.Missing, Type.Missing, Type.Missing);

All I have done is create a new dummy form and thrown your SaveFileDialogue on to that. You could try making the owner your actual form but thought this is the most likely solution to your problem in the short term.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜