0

I have been searching for DAYS for material explaining how to do this...

I have a code in WPF (C#) that loops through a collection of FrameworkElements. The objective is to save each of the FrameworkElements as separate images (gif files). The code is working currently and does in fact save all of the images I need.

However, the user has to manually click the "Save" button in the SaveFileDialog box for EACH element. There are over 100 elements. I want the code to Open the SaveFileDialog box and automatically "click" the "Save" button for the user throughout the loop (ie - for each element).

Here is the code currently...

foreach(FrameworkElement x in y)
{
    SaveFileDialog dlg = new SaveFileDialog();
    dlg.Filter = " ...blah...";
    dlg.DefaultExt = "...blah..";

    Nullable<bool> result = dlg.ShowDialog(); // (A)
    string path = dlg.FileName;
    int selectedFilterIndex = dlg.FilterIndex;

    if(result == true)                        // (B)
    {   ... then do all the rendering, cropping, etc.. .. }
}

I know that I can write some line in between the line containing "(A)" and "(B)" that will activate the "Save" button. I cannot find any examples online that eliminate the "if" statement.

I don't want my code to "wonder if this result will be true". I want the code to actually set the result to true regardless of anything else.

Any help is greatly appreciated.

Sentry
  • 4,102
  • 2
  • 30
  • 38
Ash
  • 11
  • 1
  • 4
  • 1
    Stupid question, but could you do without the `SaveFileDialog`? I mean, why show it when you want to have your code click OK anyway? – Sentry Feb 16 '17 at 17:28
  • 1
    Sorry but...if user doesn't _need_ to click Save then there is no confirmation and he can't even type a new name. Why don't you just remove the dialog and write to unique file names? You might ask once outside the loop for the first name (and file format) – Adriano Repetti Feb 16 '17 at 17:29
  • As it's written, it will open a new dialog for each element. Is that what you want? Or do you want to show 1 dialog to get a path, for example, and then operate on every file in that folder? You could perhaps open the dialog before the loop, store the path, and then operate on the files inside the loop. – Aaron Feb 16 '17 at 17:29
  • 1
    https://msdn.microsoft.com/en-us/library/aa969773(v=vs.110).aspx – Matthew Whited Feb 16 '17 at 17:45
  • Show SaveFileDialog ONCE before the loop. Save the path, filename, whatever you get from it. If you don't require any further user input at each step of the loop, get all the user input outside the loop. The only reason SaveFileDialog exists is to get input from the user. That's all. *It's a dialog*. If you don't want user input, you don't want SaveFileDialog. – 15ee8f99-57ff-4f92-890c-b56153 Feb 16 '17 at 18:22

0 Answers0