4 Replies - 796 Views - Last Post: 11 December 2016 - 08:19 AM Rate Topic: -----

#1 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

How create correctly SaveFileDialog?

Posted 09 December 2016 - 08:11 AM

I want to save (create) file where I need to zip folder content.
I use such code excerpts inside buttonclick method:
//SaveFileDialog saveFileDialog = new SaveFileDialog();
OpenFileDialog saveFileDialog = new OpenFileDialog();
Nullable<bool> result = saveFileDialog.ShowDialog();
//if (saveFileDialog.ShowDialog() == DialogResult.OK)
if (result == true) {
filename = saveFileDialog.FileName;
}
:code:
No SaveFileDialog, No OpenFileDialog do not work as during compilation I got:
Cannot implicitly convert type 'System.Windows.Forms.DialogResult' to 'bool?'
at Nullable<bool> result line. Other ways also lead to compiler errors.
As well as
if(saveFileDialog.ShowDialog() == true)
provide similar compiler error from the example:
http://www.wpf-tutor...savefiledialog/
What to do: there was told about WinForms and WPF versions of SaveFileDialog but no one seems do not work?

This post has been edited by modi123_1: 09 December 2016 - 08:30 AM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: How create correctly SaveFileDialog?

#2 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How create correctly SaveFileDialog?

Posted 09 December 2016 - 10:36 AM

Well, you're mixing and matching WPF and Windows Forms components.

Which kind of application are you building, WPF or Windows Forms?

If you're building a Windows Forms application, you should be using DialogResult as the type of result from ShowDialog().

If you're building a WPF application, somehow you've pulled in references to Windows Forms libraries.

Here's an example of using a Windows Forms SaveFileDialog:

SaveFileDialog sfd = new SaveFileDialog();
DialogResult result = sfd.ShowDialog();
if (result == DialogResult.OK)
{
    MessageBox.Show(sfd.FileName);
}


Was This Post Helpful? 0
  • +
  • -

#3 zapp1   User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 76
  • Joined: 07-December 16

Re: How create correctly SaveFileDialog?

Posted 09 December 2016 - 09:23 PM

I have used different options from the net and yours probably as well. After it I found such new one: if (result.hasValue&&result.Value) {...} - that I cannot verify now. Despite I do not understand what is difference between there two ones. If result has value then Value will be true, not null. Or whether it means exactly OK(true) not Cancel(false) value..?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver   User is online

  • Code herder
  • member icon

Reputation: 7245
  • View blog
  • Posts: 24,556
  • Joined: 05-May 12

Re: How create correctly SaveFileDialog?

Posted 10 December 2016 - 09:09 AM

C# is not like C/C++. C# is stricter with types. In C/C++, a non-zero value gets promoted to true if the expression is used in a Boolean expression. This does not happen automatically in C#.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How create correctly SaveFileDialog?

Posted 11 December 2016 - 08:19 AM

You're not understanding the part where half of the examples you read are not for Windows Forms, they're for WPF. They're different.

Now, please answer my question as to which project type you're using, or we cannot help you further.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1