TSaveDialog and 2 formats with same extension
Asked Answered
D

2

5

In TSaveDialog I added 2 formats with the same extension:

Format A|*.form
Format B|*.form
Format C|*.diff

Now I want to identify which format user chose:

var Ext: String;
begin
  if not SaveDialog1.Execute then Exit;

  Ext := LowerCase(ExtractFileExt(SaveDialog1.FileName));

This way I can differentiate "Format B" from "Format C" but I can't "Format A" from "Format B".

Dotted answered 28/10, 2019 at 17:15 Comment(4)
What if user manually types ".form" in the filename?Necrophobia
What is the difference between A and B? Is it on extension/filename basis or are they only different in content?Delfeena
@JerryDodge That's a good question!Dotted
@Delfeena Yes, different content. I want to export an image as assembler code so the extension is .asm but for 3 different assemblersDotted
E
7

Use the FilterIndex property to identify which filter was selected when the dialog was actioned.

Engrain answered 28/10, 2019 at 17:23 Comment(3)
Interestingly, that property appears to be 1 based instead of 0 based. That's kinda odd.Necrophobia
@JerryDodge It directly reflects the underlying winapi nFilterIndex field of OPENFILENAMEEngrain
That's what I figured. However still somewhat odd considering Delphi's tendency to try and make everything standard. I can understand how new Delphi devs can get so puzzled into how to do things - expecting top-level programming, but then realizing they have to dig just a big deeper everywhere they turn.Necrophobia
N
5

While David's answer directly relates to the question as it's asked, there's more to consider when determining user intentions. You can't necessarily rely on just the file extension to know what the user wants to do. After all, the user could manually type .form into the filename themselves, and then what should you do?

Instead, such options shouldn't be implemented on this level. In my experience, such things are implemented on an intermediate level. For example, think of video editing / production. The user may wish to render the video as an MP4 video. Or perhaps an AVI. However, each of those possible formats has a wide variety of other specific options, such as video codecs, quality, and more.

What needs to be done in situations like this is to provide an additional layer of user options before saving a file. Let it be part of a "project" in a sense. User does what they need to do with their content, and part of the process is deciding what type of output format they intend to produce. When the user decides to save, before prompting them for the filename, first prompt them for other specific format options, depending on what formats your application supports.

Long story short, don't rely on the file extension itself to identify all of the user's intentions. There should be an intermediate level of user choice how the file should be formatted, before choosing the output extension.

Necrophobia answered 29/10, 2019 at 2:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.