Solved

feedback in an file opendialog ?

Posted on 1999-01-04
4
297 Views
Last Modified: 2013-11-20
Hi,
How can I call an openfiledialog in visual studio 6
is it CFiledialog.dodialog ?
This is not the most important.
What I want is that whan the user selects a file I want
that my dialog box is notifyed of the thing. Because I
want to disable the ok button when the selected file is not appropriate.
Please I need source code, that I only have to cut and paste
or a sample project.
What I want.
Open a file dialog in MFC when I select a certain file
let's say c:\windows\system.ini the ok button should be disabled.
thanks in advance for your help.
0
Comment
Question by:jeurk
  • 2
4 Comments
 
LVL 4

Expert Comment

by:nil_dib
ID: 1327161
The first question is easy. To open/display a CFileDialog as an "open" dialog you have to call DoModal:

CFileDialog myDlg( TRUE, "*.*", "*.*", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT  | OFN_EXPLORER, "All files", NULL);

if(myDlg.DoModal() == IDOK)
{ .....

The second question is a bit tricky. You have to catch the message of a file selction from the user. Therefore you have to
derive your own class from CFileDialog, catch the "file selected"
message (you have to override "OnFileNameChange" method), and disable the "Ok" button ( GetDlgItem(IDOK)->DisableWindow() ).

But the much easyer way is to check the filename AFTER the user
pressed the "OK" button, and to display a message box is case of
a wrong file.

CFileDialog myDlg( TRUE, "*.*", "*.*", OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT  | OFN_EXPLORER, "All files", NULL);

if(myDlg.DoModal() == IDOK)
{
    CString csFile = myDlg.GetPathName();

    if( csFile == "c:\windows\system.ini" )
       AfxMessageBox( "File invalid" );
    else
       // do something csFile
       ......

}

0
 
LVL 4

Author Comment

by:jeurk
ID: 1327162
Hello,
it's nice that you took some time to answer that question.
but I'd like to do it in the tricky way. I can see that you know what there is to
do. Would you please do it for me ? I tryed to use the class wizard to derivate from
a CFileDialog but I can't find a valuable message to catch.
Please, i need this badly and I'm unable to do it. This would give you an A, if the
answer is exactly what I want. If yo want some more points let me know.

0
 
LVL 6

Accepted Solution

by:
snoegler earned 300 total points
ID: 1327163
I think i have what you want :)
I've made a little sample application, displaying a dialog box.
When clicking on 'FileDialog', a CFileDialog derivated class
comes up, disabling the 'Ok' button whenever the user selects
a '.INI' file. It is really short and easy to understand.
The only problem is, it is made with VC++ 4.0, but i don't think
that this will get tricky. As far as i know, you can import old
project files.

You can download the sample project at

http://absd.hypermart.net/filedialog.zip

Take a look into the "CustomFileDialog.h"/".cpp" files.
The function 'OnSelChange' defines the behavior you want.
Hope this helps :)
0
 
LVL 4

Author Comment

by:jeurk
ID: 1327164
This is exactly what I wanted. Thanks.
thanks to you too nil_dib.

I'm pasting part of the solution here for the others to read.
Thanks again.

/////////////////////////////////////////////////////////////////////////////
// CCustomFileDialog

IMPLEMENT_DYNAMIC(CCustomFileDialog, CFileDialog)

CCustomFileDialog::CCustomFileDialog(BOOL bOpenFileDialog, LPCTSTR lpszDefExt, LPCTSTR lpszFileName,
            DWORD dwFlags, LPCTSTR lpszFilter, CWnd* pParentWnd) :
            CFileDialog(bOpenFileDialog, lpszDefExt, lpszFileName, dwFlags, lpszFilter, pParentWnd)
{
}


BEGIN_MESSAGE_MAP(CCustomFileDialog, CFileDialog)
      //{{AFX_MSG_MAP(CCustomFileDialog)
            // NOTE - the ClassWizard will add and remove mapping macros here.
      //}}AFX_MSG_MAP
    ON_NOTIFY_RANGE(CDN_SELCHANGE,0,65535,OnSelChange)
END_MESSAGE_MAP()


void    CCustomFileDialog::OnSelChange(UINT id, NMHDR* pNotifyStruct, LRESULT* pResult)
{
    OFNOTIFY    *pData=(OFNOTIFY*)pNotifyStruct;
    CString     strSelExt=GetFileExt();

    CWnd        *pOkBtn=GetParent()->GetDlgItem(IDOK);
    if(pOkBtn==NULL) return;

    if(strSelExt.CompareNoCase("INI")==0)
        pOkBtn->EnableWindow(FALSE);
    else
        pOkBtn->EnableWindow(TRUE);
}

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 42
Excel Use VBA to get user's Mac Address for their computer 5 241
List out all word 7 316
withoutTen challenge 14 131
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question