c++快速开发例子--查找替换

本文介绍了如何在 TFormMain 的 RichEdit1 中使用 TFindDialog 和 TReplaceDialog 进行文本查找与替换,包括匹配模式设置和全词搜索。

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "mainform.h"
//---------------------------------------------------------------------------
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Find1Click(TObject *Sender)
{
    FindDialog1->FindText = RichEdit1->SelText;
    FindDialog1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Replace1Click(TObject *Sender)
{
    ReplaceDialog1->FindText = RichEdit1->SelText;
    ReplaceDialog1->Execute();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Find(TObject *Sender)
{
    TSearchTypes st;
    TFindDialog *cd;
    int newpos;

    if ((cd = dynamic_cast<TFindDialog *> (Sender)) == 0)
        MessageBox(NULL, "Dynamic Cast Failed!", "Find/Rep", MB_OK);

    if (cd->Options.Contains(frMatchCase))
        st << stMatchCase;
    if (cd->Options.Contains(frWholeWord))
        st << stWholeWord;

    if (RichEdit1->SelLength)
        RichEdit1->SelStart += 1;

    newpos = RichEdit1->FindText(cd->FindText,
                        RichEdit1->SelStart,
                        RichEdit1->Text.Length(),
                        st);

    if (newpos != -1)
    {
        RichEdit1->SelStart = newpos;
        RichEdit1->SelLength = cd->FindText.Length();
    }
    else
    {
        MessageBox(NULL, "End of document reached.", "Find/Rep", MB_OK);
        RichEdit1->SelStart = 0;
    }
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::Replace(TObject *Sender)
{
    if (RichEdit1->SelLength == 0)
        Find(Sender);
    else
    {
        RichEdit1->SelText =  ReplaceDialog1->ReplaceText;
        Find(Sender);
    }

    if (ReplaceDialog1->Options.Contains(frReplaceAll))
        while (RichEdit1->SelLength !=0)
            Replace(Sender);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
    RichEdit1->SelStart = 0;
}
//-----------------------------------------------------

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

moneytree

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值