Estoy desarrollando un mini editor de textos (trabajo de la Universidad) y me piden que haga búsquedas de texto.
Utilizo un TMemo para la visualización y edición de texto, tengo que poner una opción que busque una palabra en el texto del TMemo y la seleccione. Lo que he encontrado por Internet es bastante sencillo, consiste en buscar la palabra y decir si existe o no, pero no selecciona la palabra buscada. Al estilo de Word.
Publicado:
Vie Ago 07, 2009 6:17 am
alonsojpd Administrador/Moderador
Registrado: Sep 16, 2003 Mensajes: 2687
Asunto: Re: Buscar texto en un TMemo con Delphi
Anuncios
varios escribió:
Estoy desarrollando un mini editor de textos (trabajo de la Universidad) y me piden que haga búsquedas de texto.
Utilizo un TMemo para la visualización y edición de texto, tengo que poner una opción que busque una palabra en el texto del TMemo y la seleccione. Lo que he encontrado por Internet es bastante sencillo, consiste en buscar la palabra y decir si existe o no, pero no selecciona la palabra buscada. Al estilo de Word.
Si te dejan en el proyecto de la Universidad utilizar el componente FindDialog, tal vez te sirva este código:
procedure TformMenuPrincipal.dlBuscarFind(
Sender: TObject);
var
textoTemp : string;
poscionTextoBuscado : integer;
begin
with TFindDialog(Sender) do
begin
if posicionSelecFinal = 0 then
Options := Options - [frFindNext];
if frfindNext in Options then
begin
poscionTextoBuscado := posicionSelecFinal +
Length(Findtext);
textoTemp := Copy(mTexto.Lines.Text,
poscionTextoBuscado, MaxInt);
end
else
begin
textoTemp := mTexto.Lines.Text;
poscionTextoBuscado := 1;
end;
posicionSelecFinal := Pos(FindText, textoTemp);
if posicionSelecFinal > 0 then
begin
posicionSelecFinal := posicionSelecFinal +
poscionTextoBuscado - 1;
mTexto.SelStart := posicionSelecFinal - 1;
mTexto.SelLength := Length(FindText);
mTexto.SetFocus;
end
else
begin
if frfindNext in Options then
textoTemp := Concat('No hay más coincidencias de "',
FindText, '" en el texto.')
else
textoTemp := Concat('No se ha encontrado "',
FindText, '" en el texto.');
MessageDlg(textoTemp, mtInformation, [mbOK], 0);
end;
end;
end;
procedure TformMenuPrincipal.bBuscarClick(Sender: TObject);
begin
posicionSelecFinal := 0;
dlBuscar.Execute;
end;
procedure TformMenuPrincipal.bAbrirClick(Sender: TObject);
begin
if dlAbrir.Execute then
mTexto.Lines.LoadFromFile(dlAbrir.FileName);
end;
end.
Para que funcione necesitarás en el TForm (formulario):
* Un TMemo con el nombre (name) "mTexto".
* Un TButton con el nombre (name) "bAbrir".
* Un TFindDialog con el nombre (name) "dlBuscar".
* Un TOpenDialog con el nombre (name) "dlAbrir" (este no es necesario).
El procedimiento principal se encuentra en el evento "OnFind" del TFindDialog.
La ventaja de utilizar el TFindDialog es que puedes programar todas las opciones que aparecen:
* "Sólo palabras completas".
* "Coincidir mayúsculas y minúsculas.
* Dirección de búsqueda en el texto (arriba, abajo).
* Buscar siguiente.
Publicado:
Vie Ago 07, 2009 6:36 am
alonsojpd Administrador/Moderador
Registrado: Sep 16, 2003 Mensajes: 2687
Asunto: Re: Buscar texto en un TMemo con Delphi
Anuncios
Si en la práctica de la Universidad no te dejan usar el TFindDialog, siempre podrás pogramarlo manualmente. Para ello te recomendamos que le eches un vistazo a esta aplicación AjpdSoft NoteJAVA que lleva la opción de búsqueda y reemplazo implementada para un TMemo sin utilizar el TFindDialog ni el TReplaceDialog:
frDisableMatchCase: Disables (grays) the Match Case check box in a find dialog.
frDisableUpDown: Disables (grays) the Up and Down buttons, which determine the direction of the search.
frDisableWholeWord: Disables (grays) the Match Whole Word check box of find dialog.
frDown: Selects the Down button by default when the dialog opens. If the frDown flags is off, Up is selected when the dialog opens. (By default, frDown is on).
frFindNext: This flag is turned on when the user clicks the Find Next button and turned off when the dialog closes.
frHideMatchCase: Removes the Match Case check box from the dialog.
frHideWholeWord: Removes the Match Whole Word check box from the dialog.
frHideUpDown: Removes the Up and Down buttons from the dialog.
frMatchCase: This flag is turned on (off) when the user selects (deselects) the Match Case check box. To select the check box by default when the dialog opens, set frMatchCase at design time.
frReplace: Applies to TReplaceDialog only. This flag is set by the system to indicate that the application should replace the current occurrence (and only the current occurrence) of the FindText string with the ReplaceText string. Not used in search routines.
frReplaceAll: Applies to TReplaceDialog only. This flag is set by the system to indicate that the application should replace all occurrences of the FindText string with the ReplaceText string.
frShowHelp: Displays a Help button in the dialog.
frWholeWord: This flag is turned on (off) when the user selects (deselects) the Match Whole Word check box. To select the check box by default when the dialog opens, set frWholeWord at design time.
Puede publicar nuevos temas en este foro No puede responder a temas en este foro No puede editar sus mensajes en este foro No puede borrar sus mensajes en este foro No puede votar en encuestas en este foro
Visita nuestro nuevo sitio web con programas y contenidos actualizados: Proyecto A