Miki-lion
中階會員
發表:90 回覆:180 積分:54 註冊:2005-04-24
發送簡訊給我
|
好不容易看書上的學到這裡了(Dialog最後一個)~卻出了問題~~
其他的部分都ok,只有ReplaceDialog這部份,問題在這行(下面有程式碼) 程式碼
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
{
RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
}
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(SaveDialog1->Execute())
{
RichEdit1->Lines->SaveToFile(SaveDialog1->FileName);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(FontDialog1->Execute())
{
RichEdit1->Font=FontDialog1->Font;
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
ReplaceDialog1->Position = Point(RichEdit1->Left + RichEdit1->Width, RichEdit1->Top);
ReplaceDialog1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReplaceDialog1Replace(TObject *Sender)
{
TReplaceDialog *dlg = (TReplaceDialog *)Sender;
/* perform a global case-sensitive search for FindText in Memo1 */
int SelPos = RichEdit1->Lines->Text.Pos(dlg->FindText);
if (SelPos > 0)
{
RichEdit1->SelStart = SelPos - 1;
RichEdit1->SelLength = dlg->FindText.Length();
// Replace selected text with ReplaceText
RichEdit1->SelText = dlg->ReplaceText;
}
else
MessageBeep(0); }
//---------------------------------------------------------------------------
void __fastcall TForm1::ReplaceDialog1Find(TObject *Sender)
{
int FoundAt, StartPos, ToEnd;
// 如果不是第一次搜尋,將接著下一個搜尋
if (RichEdit1->SelLength)
StartPos = RichEdit1->SelStart + RichEdit1->SelLength;
else
// 如果是第一次搜尋,初始化StartPos
StartPos = 0;
// ToEnd是搜尋字串起始位址到RichEdit最末端位置的剩餘字串長度
ToEnd = RichEdit1->Text.Length() - StartPos;
FoundAt = RichEdit1->FindText(ReplaceDialog1->FindText, StartPos, ToEnd, TSearchTypes()<< stMatchCase);
if (FoundAt != -1)
{ RichEdit1->SetFocus();
RichEdit1->SelStart = FoundAt;
RichEdit1->SelLength = ReplaceDialog1->FindText.Length();
}
}
//---------------------------------------------------------------------------
感謝這裡的大大陪我成長。
|
cashyy
高階會員
發表:117 回覆:322 積分:212 註冊:2004-04-30
發送簡訊給我
|
您好!我試過相同的程式碼!可以正常執行!程式碼如下:
//--------------------------------------------------------------------------- #include
#pragma hdrstop #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(OpenDialog1->Execute())
RichEdit1->Lines->LoadFromFile(OpenDialog1->FileName);
}
//--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(SaveDialog1->Execute())
RichEdit1->Lines->SaveToFile(SaveDialog1->FileName);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
if(FontDialog1->Execute())
RichEdit1->Font=FontDialog1->Font;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button4Click(TObject *Sender)
{
ReplaceDialog1->Position = Point(RichEdit1->Left + RichEdit1->Width, RichEdit1->Top);
ReplaceDialog1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReplaceDialog1Replace(TObject *Sender)
{
TReplaceDialog *dlg = (TReplaceDialog *)Sender;
/* perform a global case-sensitive search for FindText in Memo1 */
int SelPos = RichEdit1->Lines->Text.Pos(dlg->FindText);
if (SelPos > 0)
{
RichEdit1->SelStart = SelPos - 1;
RichEdit1->SelLength = dlg->FindText.Length();
// Replace selected text with ReplaceText
RichEdit1->SelText = dlg->ReplaceText;
}
else
MessageBeep(0);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ReplaceDialog1Find(TObject *Sender)
{
int FoundAt, StartPos, ToEnd;
// 如果不是第一次搜尋,將接著下一個搜尋
if (RichEdit1->SelLength)
StartPos = RichEdit1->SelStart + RichEdit1->SelLength;
else
// 如果是第一次搜尋,初始化StartPos
StartPos = 0;
// ToEnd是搜尋字串起始位址到RichEdit最末端位置的剩餘字串長度
ToEnd = RichEdit1->Text.Length() - StartPos;
FoundAt = RichEdit1->FindText(ReplaceDialog1->FindText, StartPos, ToEnd, TSearchTypes()<< stMatchCase);
if (FoundAt != -1)
{
RichEdit1->SetFocus();
RichEdit1->SelStart = FoundAt;
RichEdit1->SelLength = ReplaceDialog1->FindText.Length();
}
}
//---------------------------------------------------------------------------
[code] 請看一下.H裡面有沒有這些function或物件
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
TOpenDialog *OpenDialog1;
TReplaceDialog *ReplaceDialog1;
TRichEdit *RichEdit1;
TSaveDialog *SaveDialog1;
TFontDialog *FontDialog1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
void __fastcall Button3Click(TObject *Sender);
void __fastcall Button4Click(TObject *Sender);
void __fastcall ReplaceDialog1Replace(TObject *Sender);
void __fastcall ReplaceDialog1Find(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
|
Miki-lion
中階會員
發表:90 回覆:180 積分:54 註冊:2005-04-24
發送簡訊給我
|
真的是少
void __fastcall ReplaceDialog1Replace(TObject *Sender);
void __fastcall ReplaceDialog1Find(TObject *Sender);
這2行 不過我都有照著書上做 這地方怎會出錯呢? 另外跑出來的取代功能視窗,都在螢幕外面
跳出來的視窗位置我知道有個屬性可調~請問是哪裡呢? 最重要的是好像也沒有功能...XD
跟wordpad一樣的取代功能~~
|
blk5743
高階會員
發表:34 回覆:371 積分:236 註冊:2003-11-17
發送簡訊給我
|
引言:
真的是少
void __fastcall ReplaceDialog1Replace(TObject *Sender);
void __fastcall ReplaceDialog1Find(TObject *Sender);
這2行 不過我都有照著書上做 這地方怎會出錯呢?
這應該是你照著書KEY進去的吧
應該要選擇BCB的元件,他才會自動加到.h檔
|