13,825
社区成员
发帖
与我相关
我的任务
分享
//OpenDialog
/*
This example uses an Open dialog box, a memo, and a button
on a form. When the user clicks the button, the Open dialog
box appears. When the user selects files in the dialog box
and chooses the OK button, the first line from each of the
files is added to the memo. Choose multiple files using the
CNTL key or the SHIFT key.
*/
#include <stdio.h> // for FILE
void __fastcall TForm1::Button1Click(TObject *Sender)
{
FILE *stream;
char FirstLine[512];
OpenDialog1->Options.Clear();
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;
OpenDialog1->Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
OpenDialog1->FilterIndex = 2; // start the dialog showing all files
if (OpenDialog1->Execute())
{
for (int I = 0; I < OpenDialog1->Files->Count; I ++)
{
stream = fopen(OpenDialog1->Files->Strings[I].c_str(), "r");
if (stream)
{
// read the first line from the file
fgets(FirstLine, sizeof(FirstLine), stream);
Memo1->Lines->Append(FirstLine);
fclose(stream);
}
}
}
}
/*
This example uses a tab control to display the contents of
several files. To run the example, place a tab control on a
form and add a memo control that fits into its client area.
Be sure to leave enough room for the tabs when they appear.
Then add an OpenDialog and a button to the form. The
"Add a file" button adds a single file to the tabcontrol.
The "Assign files" button removes previous files and can be
used to assign multiple files. To assign multiple files,
use CNTL Select or SHIFT Select to select files in the
OpenDialog.
*/
void __fastcall TForm1::Add_a_fileClick(TObject *Sender)
{
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly;
if (OpenDialog1->Execute())
{
int index = TabControl1->Tabs->Add(OpenDialog1->FileName);
Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[index]);
}
}
void __fastcall TForm1::Assign_filesClick(TObject *Sender)
{
OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist << ofHideReadOnly;
if (OpenDialog1->Execute())
{
TabControl1->Tabs->Assign(OpenDialog1->Files);
Memo1->Lines->LoadFromFile(TabControl1->Tabs->Strings[TabControl1->TabIndex]);
}
}
/*
Place the following code in the tab control’s OnChange event
handler:
*/
void __fastcall TForm1::TabControl1Change(TObject *Sender)
{
Memo1->Lines->LoadFromFile(
TabControl1->Tabs->Strings[TabControl1->TabIndex]);
}
//SaveDialog
if (SaveDialog1->Execute())
Memo1->Lines->SaveToFile(SaveDialog1->FileName);
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(SaveDialog1->Execute())
{
Memo1->Lines->SaveToFile(SaveDialog1->FileName);
}
}