• Sábado 20 de Abril de 2024, 01:24

Autor Tema:  Problema En Funcion Remplazar En Editor De Texto  (Leído 1934 veces)

alfredo49

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Problema En Funcion Remplazar En Editor De Texto
« en: Lunes 24 de Enero de 2005, 01:50 »
0
Pos eso que en la universidad nos han exo una putada y nos han mandado hacer un editor de texto , ya lo tengo entero funcionando solo hay una cosa que nose hacer que es en la tipika ventanita de Edicion/Remplazar ( para que te buske una palabra en el texto y te la remplaze por la que hayas escrito ) pues nose km implementarla , en teoria no deberian de ser mas de 2 lineas de kodigo , o se basaria en modificar el de la funcion buscar que si que lo tengo, para que una vez modificada borre y remplaze. A ver si alguien me puede ayudar , aki dejo la funcion que te buska una palabra y te la resalta en el editor (funciona perfectamente pk nos la ha dao el tio de la universidad):


void __fastcall TMainForm::FindDialogFind(TObject *Sender)
{
//FUNCION DE BUSQUEDA DE UNA PALABRA
// Hace que la función funcione con cualquier TFindDialog / TRichEdit
TFindDialog *Dialog = (TFindDialog *) Sender;
TRichEdit *Memo = this->Memo;
// FindStr is the text that is being searched. ToFindStr es el string
// that needs to be found.
AnsiString FindStr, ToFindStr;
// This piece of code fills the FindStr and ToFindStr variable. When the
// frDown flag is selected, FindStr will be the text from SelStart to the
// end. When it is deselected, FindStr is the text from the start to
// SelStart. Also both FindStr and ToFindStr are inverted so that the
// first occurrence is the closest to SelStart, instead of the start.
if (Dialog->Options.Contains(frDown)) {
Memo->SelStart = Memo->SelStart + Memo->SelLength;
Memo->SelLength = 0;
FindStr = Memo->Text.SubString(Memo->SelStart,Memo->Text.Length() - Memo->SelStart);
ToFindStr = Dialog->FindText;
}
else {
FindStr = InvertString(Memo->Text.SubString(0, Memo->SelStart));
ToFindStr = InvertString(Dialog->FindText);
}
// If the frMatchCase flag is deselected, both FindStr and ToFindStr are
// made lowercase so that case doesn't matter in the search.
if (!Dialog->Options.Contains(frMatchCase)) {
FindStr = FindStr.LowerCase();
ToFindStr = ToFindStr.LowerCase();
}
// The actual search procedure. When it is found and the frWholeWord flag
// is selected, it checks whether or not it is part of a word.
int FindPos, OldSelStart = Memo->SelStart;
while (1) {
FindPos = FindStr.Pos(ToFindStr);
if (FindPos == 0) {
ShowMessage("La palabra no ha sido encontrada");
return;
}
if (Dialog->Options.Contains(frWholeWord)) {
if ((FindPos == 0 || !isalnum(FindStr.c_str()[FindPos-2])) &&
(FindPos == FindStr.Length() - ToFindStr.Length() - 1 ||
!isalnum(FindStr.c_str()[FindPos-1+ToFindStr.Length()]))) break;
FindStr = FindStr.SubString(FindPos+1, FindStr.Length() - FindPos);
}
else break;
}
if (Dialog->Options.Contains(frDown)) {
if (OldSelStart == 0) OldSelStart++;
Memo->SelStart = OldSelStart + FindPos - 2;
}
else
Memo->SelStart = OldSelStart - FindPos - ToFindStr.Length() + 1;
Memo->SelLength = ToFindStr.Length();
Memo->SetFocus();
}



a ver si alguien tiene la solucion pk m estoy volviendo loko

Gracias y Salu2

Joss

  • Nuevo Miembro
  • *
  • Mensajes: 23
    • Ver Perfil
Re: Problema En Funcion Remplazar En Editor De Texto
« Respuesta #1 en: Lunes 24 de Enero de 2005, 13:48 »
0
La siguiente funcion, reemplaza el texto seleccionado por la cadena que le indiques.

Memo->SetSelTextBuf( TU TEXTO ) ;

Con eso te debe bastar.

alfredo49

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Re: Problema En Funcion Remplazar En Editor De Texto
« Respuesta #2 en: Lunes 24 de Enero de 2005, 15:27 »
0
muchas gracias joss pero eske , lo que tengo que meter en "TU TEXTO" es una variable , k es la variable k almacena el texto k se escribe en el kuadro de texto k sale a la izq de reemplazar ¿Tienes idea donde ver o komo se puede llamar esa variable?


De todos modos decir ke he enkntrado el kodigo de la funcion reemplazar pero tiene un pekeño fallo , si le das a remplazar todas si k te lo hace bien , pero si le das a buskar siguiente te selecciona la palbra, hasta ahi todo bien , pero kuando le das a remplazar te remplaza la siguiente , no la k tiene seleccionada.
Aqui pego el kodigo a ver si alguien tiene la solucion.

void __fastcall TMainForm::ReplaceDialogReplace(TObject *Sender)
{

TSearchTypes SearchTypes;
    int SearchStart = Memo->SelStart+Memo->SelLength;
    bool ReplaceAllFlag;
    bool ReplaceEndFlag = false;
    unsigned int SearchResult;

    if(ReplaceDialog->Options.Contains(frWholeWord)){
        SearchTypes << stWholeWord;
    }
    if(ReplaceDialog->Options.Contains(frMatchCase)){
        SearchTypes << stMatchCase;
    }
    if(ReplaceDialog->Options.Contains(frReplaceAll)){
        ReplaceAllFlag = true;
    }
    if(ReplaceAllFlag == true){
      SearchStart = 0;
      while(ReplaceEndFlag ==false){
        SearchResult = Memo->FindText(ReplaceDialog->FindText,SearchStart,Memo->Text.Length(),SearchTypes);
         if(SearchResult == -1){
           ReplaceEndFlag = true;

        }
        else{
          Memo->SelStart = SearchResult;
          Memo->SelLength = ReplaceDialog->FindText.Length();
          Memo->SelText = ReplaceDialog->ReplaceText;
          SearchStart = Memo->SelStart+Memo->SelLength;
        }
      }
    }
    else{
         SearchResult = Memo->FindText(ReplaceDialog->FindText,SearchStart,Memo->Text.Length(),SearchTypes);
         if(SearchResult == -1){
           ReplaceEndFlag = true;
           ShowMessage("Texto remplazado");
          }
          else{
            Memo->SelStart = SearchResult;
            Memo->SelLength = ReplaceDialog->FindText.Length();
            Memo->SelText = ReplaceDialog->ReplaceText;
            SearchStart = Memo->SelStart+Memo->SelLength;
             



          }
       
    }
}

Salu2 a ver si alguien sabe algo

alfredo49

  • Nuevo Miembro
  • *
  • Mensajes: 3
    • Ver Perfil
Re: Problema En Funcion Remplazar En Editor De Texto
« Respuesta #3 en: Martes 25 de Enero de 2005, 09:38 »
0
nadie sabe nada? eske esta funcion no akaba de funcionar bien. Un Saludo