RichEdit String Search Question??

I need a procedure that can search for any letter of the alphabet in a RichEdit String. Here's the scenario:

My richedit text has a listing of Music Artists and their songs. Example:

1. Asia
- Song 1
- Song 2

2. Alan Parsons
- Song 1
- Song 2

3. Beatles
- Song 1
- Song 2

4. Rolling Stones
- Song 1
- Song 2
- Song 3

etc...

I want to put the letters of the alphabet as "Buttons" at the top of my application. If a user clicks a letter of the alphabet I want my RichEdit to automatically move to the FIRST artist that begins with that letter. I also want the RichEdit to move the cursor to that position and make it visible.

Can anyone help. I can give more details if necessary.

Thanks,
mjbcomp
mjbcompAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kretzschmarCommented:
hi mjbcomp,

it would be easier to find the musicartists, if the fontstyle different as by the songs (bold for ex.)

is this possible for you?

remark:
otherwise, its more a job for a database

meikl
0
mjbcompAuthor Commented:
The musicartists are in BOLD and also their color is "clBlue".

I thought about doing it this way but couldn't figure out to get the Font.Style or Font.Color of the current line.

I have written a procedure that actually works but it seems like there could be a better way. This procedure simply looks for the period (.) character on the lines where the music artist is and that's how I can tell where I am. Any ideas? I like your idea about the different font or color.

procedure TfMain.GetLetter(TheString: String);
var
x,y,LengthOf : Integer;
a,b : String;
begin
 LengthOf := 0;
 for x := 0 to RichEdit1.Lines.Count - 1 do
 begin
   a := RichEdit1.Lines[x];
    LengthOf := LengthOf + Length(a) + 2;
   if POS('.',a) <> 0 then
   begin
    y := POS('.',a);
    b := Copy(a, y + 2 ,1);

    if b = TheString then
    begin
     // We are on a line containing an Artist
     RichEdit1.SelStart := LengthOf - Length(RichEdit1.Lines[x]) - 2;
     RichEdit1.SelLength := Length(RichEdit1.Lines[x]);
     RichEdit1.SetFocus;
     RichEdit1.Perform(EM_SCROLLCARET,0,0);
     Exit;
    end;
   end;
 end;
end;


mjbcomp
0
florisbCommented:
If you drop some buttons on your form and give them caption A, B, C, D, ...
And you make all onClickEvents points to the next procedure, it should work:

procedure TForm1.Button1Click(Sender: TObject);
var
  FoundAt: LongInt;
  StartPos, ToEnd: Integer;
begin
with RichEdit1 do
  begin
    //set sellength to 0 if you want to restart at top
    //...
    { begin the search after the current selection if there is one }
    { otherwise, begin at the start of the text }
    if SelLength <> 0 then
      StartPos := SelStart + SelLength
    else
      StartPos := 0;
    { ToEnd is the length from StartPos to the end of the text in the rich edit control }
    ToEnd := Length(Text) - StartPos;

    FoundAt := FindText((Sender as tbutton).caption, StartPos, ToEnd, [stMatchCase]);
    if FindText('.',foundAt-3,foundat-1,[stMatchCase]) <> -1 then //'.' before letter.
      begin
      if FoundAt <> -1 then
        begin
        SetFocus;
        SelStart := FoundAt;
        SelLength := 1; //detect length of found word here and set selected area, remove this
                        //line if you always want the first letter found.
        end;
      end;
  end;

end;

Good Luck!
Floris.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjbcompAuthor Commented:
Thank you so much. This code is excellent. I have modified it slightly for my particular application and it now works fine.

Thanks,
mjbcomp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.