Search code examples
delphidelphi-7

How can I filter only plain text files in the TOpenDialog in delphi


I need to be able to put text from a file into a memo but that I have that part covered.

What I would like to do is filter only plain text files(.txt; .html; .c; .cs; ect) in the file dialogue

Is there some quick way to do that or do I just manually filter all plain text files?


Solution

  • The answer given in the comment by Andreas Rejbrand is correct.

    Here is a code fragment that implement it:

    procedure TForm1.Button1Click(Sender: TObject);
    begin
        OpenDialog1.Filter := 'Text files|*.TXT;*.HTML;*.C;*.CS';
        if OpenDialog1.Execute(Handle) then
            Memo1.Lines.LoadFromFile(OpenDialog1.FileName)
        else
            Memo1.Lines.Add('**** cancel ****');
    end;
    

    This code write load the selected file content into the memo.