TMemo Writing to files. Open/Save operations. |
|
axsoft
版主 發表:681 回覆:1056 積分:969 註冊:2002-03-13 發送簡訊給我 |
TMemo Writing to files. Open/Save operations. http://www.hobbypages.net/snip10.html
-------------------------------------------------------------------------------- Most applications require read/write operations as a part of their normal flow. Keeping it simple throughout your coding is what C++ Builder is all about. Lets take a look at a few code snips: Your MainForm's header should have this line, so you can use this string. private: // User declarations
AnsiString PathName; Your MainForm's .cpp should have this const declared.
#pragma resource "*.dfm" TForm1 *Form1; const AnsiString DefaultFileName = "Untitled"; //this line //-------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { PathName = DefaultFileName; //and this line } And now the MenuItem Clicks void __fastcall TForm1::New1Click(TObject *Sender) { PathName = DefaultFileName; Form2->Memo1->Lines->Clear(); Form2->Memo1->SelStart = 0; Caption = DefaultFileName; Form2->Memo1->Modified = false; } //--------------------------------------------------------------------------- void __fastcall TForm1::Open1Click(TObject *Sender) { if (OpenDialog1->Execute()) { Form2->Memo1->Lines->LoadFromFile(OpenDialog1->FileName); Caption = ExtractFileName(PathName); } } //--------------------------------------------------------------------------- void __fastcall TForm1::Save1Click(TObject *Sender) { if (PathName == DefaultFileName) SaveAs1Click(Sender); else { Form2->Memo1->Lines->SaveToFile(PathName); Form2->Memo1->Modified = false; } } //--------------------------------------------------------------------------- void __fastcall TForm1::SaveAs1Click(TObject *Sender) { SaveDialog1->FileName = PathName; if (SaveDialog1->Execute()) { PathName = SaveDialog1->FileName; Caption = ExtractFileName(PathName); Save1Click(Sender); } }Not to say I didn't run into problems with this. First, understand that while creating this example, I just threw together an MDIForm and MDIChild, with the child maximized and no system buttons. When I would save an item as untitled, no extention, open the same item without any kind of close method, and try to save again, I got them exception - access violations things... The little buggers. I know there are ways to correct that, I just didn't get into the whole thing with this code snip. The basics are there. and there are a few other things, like creating back-ups, close querries, open-read only concepts, and the list goes on, whith the things you can do on read/write to files. 網路志工聯盟----Visita網站http://www.vista.org.tw ---[ 發問前請先找找舊文章 ]--- 發表人 - axsoft 於 2003/03/31 13:30:51 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |