I need some help to complite my game word with richtext


  Hello Guys.
  I really need a help from you.
  I am trying to make a scrambled word game and there are some things that I would like to do
  and I don't have any ideia how to do. Some things I got but it isn't work so fine so far. If you could
  check and give your opinion , it will be well acceptable.

  Well, Let me explain the game:

  I have two RxRichEdit one of them is gonna have the scrambled phrase and the other one is gonna have the
  correct phrase. So, you have a scrambled word like that in the first rxrichedit:

  Mother working father today but home at me with is my my.

  The student is gonna listen the correct phrase and must put all the scrambled word on the correct way, see an
  example:

  My father is working today but my mother is at home with me

  Well, How it works:

  In the first rxrichedit you must click on a word, when the student makes a doble clicked,
  the function will get the word and will put it in the second rxrichedit, creating the correct phrase
  listened. Sometimes he/she can make any mistake and the word must come back to the first rxrichedit, taking off
  the word from the second rxrichedit. After all finish the student will correct  the phrase.

  Well, this step was done until here, sometimes it didn't work fine and I haven't figured out why so far, so,
  I would like you to take a look and help me.

  Second: There is another thing that must be done and I don't have any ideia how to make it, it is.

  Imagine this phrase:

  "work here is a professional good she"

  "She can because"

  As you can see, it is missing a word between because and can that is WORK, well, this work must be inserted this
  way: You have to click on the word from first richedit, this way, you will get it, with your mouse you have to
  insert the word between the two words that is can and because.

  It is not be a good solution working only with double click because if you have a long phrase and something missed
  almost at the beginning, you would have to take off all the words until the correct word to start imputing all the
  words again, so with the you have a good save time.

  That's all I need to do
  I thanks in advance
  Alexandre


  Follow below the code was make until now .



****************************************************************************************
Function VisibleLines(RE: TRxRichEdit): integer;
Var
  Oldfont:HFont;
  DC     :THandle;
  TM     :TTextMetric;
  TheRect:TRect;
begin
  DC := GetDC(RE.Handle);
  try
    OldFont := SelectObject(DC, RE.Font.Handle);
    try
      GetTextMetrics(DC, TM);
      RE.Perform(EM_GETRECT, 0, longint(@TheRect));
      Result:=(TheRect.Bottom - TheRect.Top) div (TM.tmHeight + TM.tmExternalLeading);
    finally
      SelectObject(DC, Oldfont);
    end;
  finally
    ReleaseDC(RE.Handle, DC)
  end
end;

procedure TFSCRAMBLEDWORD.Button1Click(Sender: TObject);
var
  NoLines, X:Integer;
begin
  RodaBaseDados;
  Edit1.Text :=StringReplace(TQ.FieldByName('CP_FRASE_US').Value, '@','',[rfReplaceAll]);
  RxRichEdit1.Lines.Clear;
  RxRichEdit2.Lines.Clear;  
  RxRichEdit1.Lines.Add(EmbaralhaFrase(StringReplace(TQ.FieldByName('CP_FRASE_US').Value, '@','',[rfReplaceAll])));
  NoLines:=VisibleLines(RxRichEdit1) div 2;
  for X:=1 to NoLines do  RxRichEdit1.Lines.Insert(0, ' ');
end;

procedure TFSCRAMBLEDWORD.RodaBaseDados;
begin
  With TQ do
  Begin
    Close;
    Sql.Clear;
    Sql.Add('SELECT CP_CODIGO, CP_FRASE_US FROM CADPAL');
    Sql.Add('ORDER BY RND(CP_CODIGO)');
    Open;
  End;
end;

procedure TFSCRAMBLEDWORD.RxRichEdit1DblClick(Sender: TObject);
Var
  Pos : Integer;
  NoLines, X:Integer;
  Txt, Str : String;

begin
  Txt := Trim(RxRichEdit2.Text);
  Txt := StringReplace(txt,chr(10),'',[rfReplaceAll]);
  Txt := StringReplace(txt,chr(13),'',[rfReplaceAll]);
  Txt := TrimLeft(Txt);
  Txt := txt +  ' '  + Trim(RxRichEdit1.SelText);
  RxRichEdit2.Lines.Clear;
  RxRichEdit2.Lines.Append(Txt);
  NoLines:=VisibleLines(RxRichEdit2) div 2;
  For X:=1 to NoLines do RxRichEdit2.Lines.Insert(0, ' ');

  str := RxRichEdit1.Text;
  Str := StringReplace(Str,chr(10),'',[rfReplaceAll]);
  Str := StringReplace(Str,chr(13),'',[rfReplaceAll]);
  Str := TrimLeft(Str);
//  Str := TrimRight(Str);
  Txt := RxRichEdit1.SelText;
  Txt := StringReplace(Str, txt,'',[]);
  RxRichEdit1.Lines.Clear;
  RxRichEdit1.Lines.Append(Txt);
  NoLines:=VisibleLines(RxRichEdit1) div 2;
  for X:=1 to NoLines do  RxRichEdit1.Lines.Insert(0, ' ');
End;

Function TFSCRAMBLEDWORD.EmbaralhaFrase(Str: String): String;
var
  lr   : integer;
  lS   : TStringlist;
  lout : string;
begin
  Randomize;
  ls:=TStringlist.create;
  ls.commatext:= Str;
  While ls.count>0 do
  Begin
    lr:=Random(ls.count);
    lout:=lout+ ' '+ls[lr];
    ls.Delete(lr);
  End;
  Result := lout;
  freeandNil(ls);
End;

procedure TFSCRAMBLEDWORD.Button2Click(Sender: TObject);
Var
  Txt : String;
begin
  Txt := RxRichEdit2.Text;
  Txt := StringReplace(txt,chr(10),'',[rfReplaceAll]);
  Txt := StringReplace(txt,chr(13),'',[rfReplaceAll]);
  If TrimLeft(Txt) = Edit1.Text Then ShowMessage('OK')
end;

procedure TFSCRAMBLEDWORD.RxRichEdit2DblClick(Sender: TObject);
Var
  Pos : Integer;
  NoLines, X:Integer;
  Txt, Str : String;

begin
  Txt := Trim(RxRichEdit1.Text);
  Txt := StringReplace(txt,chr(10),'',[rfReplaceAll]);
  Txt := StringReplace(txt,chr(13),'',[rfReplaceAll]);
  Txt := TrimLeft(Txt);
  Txt := txt +  ' '  + Trim(RxRichEdit2.SelText);
  RxRichEdit1.Lines.Clear;
  RxRichEdit1.Lines.Append(Txt);
  NoLines:=VisibleLines(RxRichEdit1) div 2;
  For X:=1 to NoLines do RxRichEdit1.Lines.Insert(0, ' ');

  str := RxRichEdit2.Text;
  Str := StringReplace(Str,chr(10),'',[rfReplaceAll]);
  Str := StringReplace(Str,chr(13),'',[rfReplaceAll]);
  Str := TrimLeft(Str);
//  Str := TrimRight(Str);
  Txt := RxRichEdit2.SelText;
  Txt := StringReplace(Str, txt,'',[]);
  RxRichEdit2.Lines.Clear;
  RxRichEdit2.Lines.Append(Txt);
  NoLines:=VisibleLines(RxRichEdit2) div 2;
  for X:=1 to NoLines do  RxRichEdit2.Lines.Insert(0, ' ');

LVL 1
hidrauAsked:
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.

pcsentinelCommented:
Ok hidrau, I dont use rxRichedit so I've written a simple prog using textboxes that will show you the logic

*******************UNIT CODE***********************
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    lblSentence: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    lblDone: TLabel;
    procedure Edit1DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit2DblClick(Sender: TObject);
  private
    FStore: TStringlist;
    procedure UpdateEdit1;
    function Scramble(Str: String): String;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
function TForm1.Scramble(Str: String): String;
var
  lr   : integer;
  lS   : TStringlist;
  lout : string;
begin
  Randomize;
  ls:=TStringlist.create;
  ls.commatext:= Str;
  while ls.count>0 do
  begin
    lr:=Random(ls.count);
    lout:=lout+ ' '+ls[lr];
    ls.Delete(lr);
  end;
  Result := lout;
  freeandNil(ls);
end;

procedure TForm1.UpdateEdit1;
var
      li: integer;
begin
  Edit1.Text:='';
  for li:=0 to pred(FStore.Count) do
  begin
    if Integer(FStore.Objects[li])=0 then
    begin
        if li>0 then
          Edit1.Text:=Edit1.Text+' ';
        Edit1.Text:=Edit1.Text+FStore[li];
        end;
  end;
  Edit1.Text:=Trim(Edit1.Text);
end;


procedure TForm1.Edit1DblClick(Sender: TObject);
begin
      if edit1.sellength>0 then
  begin
    if Edit2.Text<>'' then
          Edit2.Text:=Edit2.Text+' ';
        edit2.text:=edit2.text+Trim(edit1.seltext);
    FStore.Objects[FStore.IndexOf(Trim(edit1.seltext))]:=pointer(1);
    UpdateEdit1;
  end;
  if Trim(Edit2.Text)=Trim(lblSentence.Caption) then
        lblDone.Caption:='Correct'
  else
        lblDone.Caption:='InCorrect';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
      FStore:=TStringlist.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
      li: integer;
begin
  Edit1.Text:='';
      FStore.CommaText:=Scramble(lblSentence.Caption);
  for li:=0 to pred(FStore.Count) do
        FStore.Objects[li]:=pointer(0);
  UpdateEdit1;
end;

procedure TForm1.Edit2DblClick(Sender: TObject);
begin
      if edit2.sellength>0 then
  begin
    FStore.Objects[FStore.IndexOf(Trim(edit2.seltext))]:=pointer(0);
    Edit2.SelText:='';
    UpdateEdit1;
  end;
  Edit2.Text:=Trim(Edit2.Text);
  if Trim(Edit2.Text)=Trim(lblSentence.Caption) then
        lblDone.Caption:='Correct'
  else
        lblDone.Caption:='InCorrect';
end;

end.


************************FORM CODE***********************
object Form1: TForm1
  Left = 1273
  Top = 106
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object lblSentence: TLabel
    Left = 144
    Top = 72
    Width = 83
    Height = 13
    Caption = 'A day on the farm'
  end
  object lblDone: TLabel
    Left = 56
    Top = 256
    Width = 42
    Height = 13
    Caption = 'Incorrect'
  end
  object Edit1: TEdit
    Left = 52
    Top = 128
    Width = 225
    Height = 21
    TabOrder = 0
    OnDblClick = Edit1DblClick
  end
  object Edit2: TEdit
    Left = 48
    Top = 208
    Width = 229
    Height = 21
    TabOrder = 1
    OnDblClick = Edit2DblClick
  end
  object Button1: TButton
    Left = 412
    Top = 68
    Width = 75
    Height = 25
    Caption = 'Load'
    TabOrder = 2
    OnClick = Button1Click
  end
end

hope this helps

regards
hidrauAuthor Commented:
Hello pcsentinel
Thanks for taking a look at my problem, but I don't know if you read the lines in my first post. I need to work with a mouse.


(  As you can see, it is missing a word between because and can that is WORK, well, this work must be inserted this
  way: You have to click on the word from first richedit, this way, you will get it, with your mouse you have to
  insert the word between the two words that is can and because.

  It is not be a good solution working only with double click because if you have a long phrase and something missed
  almost at the beginning, you would have to take off all the words until the correct word to start imputing all the
  words again, so with the you have a good save time. )


  I thanks in advance
  Alexandre
pcsentinelCommented:
Ok try this


the user can reposition then cursor in the second edit box and that is where the word from the first box is inserted. If the user clicks within a word in the second box then this is invalid and the cursor moves to the end.

*********************UNIT CODE*************************
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdFTP, Word97, OleServer, ExtCtrls, IdAntiFreezeBase, IdAntiFreeze;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    lblSentence: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    lblDone: TLabel;
    procedure Edit1DblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Edit2DblClick(Sender: TObject);
    procedure Edit2Click(Sender: TObject);
    procedure Edit2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    FStore: TStringlist;
    procedure UpdateEdit1;
    function Scramble(Str: String): String;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}
function TForm1.Scramble(Str: String): String;
var
  lr   : integer;
  lS   : TStringlist;
  lout : string;
begin
  Randomize;
  ls:=TStringlist.create;
  ls.commatext:= Str;
  while ls.count>0 do
  begin
    lr:=Random(ls.count);
    lout:=lout+ ' '+ls[lr];
    ls.Delete(lr);
  end;
  Result := lout;
  freeandNil(ls);
end;

procedure TForm1.UpdateEdit1;
var
      li: integer;
begin
  Edit1.Text:='';
  for li:=0 to pred(FStore.Count) do
  begin
    if Integer(FStore.Objects[li])=0 then
    begin
        if li>0 then
          Edit1.Text:=Edit1.Text+' ';
        Edit1.Text:=Edit1.Text+FStore[li];
        end;
  end;
  Edit1.Text:=Trim(Edit1.Text);
end;


procedure TForm1.Edit1DblClick(Sender: TObject);
begin
  edit2.Seltext:=Trim(edit1.seltext)+' ';
  edit2.Text:=Trim(edit2.Text)+' ';
  edit2.SelStart:=Length(Edit2.Text);
  FStore.Objects[FStore.IndexOf(Trim(edit1.seltext))]:=pointer(1);
  UpdateEdit1;
  if Trim(Edit2.Text)=Trim(lblSentence.Caption) then
        lblDone.Caption:='Correct'
  else
        lblDone.Caption:='InCorrect';
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
      FStore:=TStringlist.Create;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
      li: integer;
begin
  Edit1.Text:='';
      FStore.CommaText:=Scramble(lblSentence.Caption);
  for li:=0 to pred(FStore.Count) do
        FStore.Objects[li]:=pointer(0);
  UpdateEdit1;
end;

procedure TForm1.Edit2DblClick(Sender: TObject);
begin
      if edit2.sellength>0 then
  begin
    FStore.Objects[FStore.IndexOf(Trim(edit2.seltext))]:=pointer(0);
    Edit2.SelText:='';
    UpdateEdit1;
  end;
  Edit2.Text:=Trim(Edit2.Text)+' ';
  edit2.SelStart:=Length(Edit2.Text);
  if Trim(Edit2.Text)=Trim(lblSentence.Caption) then
        lblDone.Caption:='Correct'
  else
        lblDone.Caption:='InCorrect';
end;


procedure TForm1.Edit2Click(Sender: TObject);
begin
 //first check
 if edit2.SelStart>0 then
 begin
       if (copy(edit2.text,edit2.selstart,1)<>' ') and  (copy(edit2.text,edit2.selstart+1,1)<>' ')then
        edit2.selstart:=length(edit2.text);
 end;
 //adjust insert pos
 if (edit2.SelStart>0) and (copy(edit2.text,edit2.selstart,1)<>' ') then
        edit2.selstart:=edit2.selstart+1;
 //recheck
 if edit2.SelStart>0 then
 begin
       if (copy(edit2.text,edit2.selstart,1)<>' ') and  (copy(edit2.text,edit2.selstart+1,1)<>' ')then
        edit2.selstart:=length(edit2.text);
 end;
end;

procedure TForm1.Edit2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
      Key:=0;
end;

end.

**********************************FORM CODE*************************
object Form1: TForm1
  Left = 111
  Top = 169
  Width = 696
  Height = 480
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object lblSentence: TLabel
    Left = 144
    Top = 72
    Width = 83
    Height = 13
    Caption = 'A day on the farm'
  end
  object lblDone: TLabel
    Left = 56
    Top = 256
    Width = 42
    Height = 13
    Caption = 'Incorrect'
  end
  object Edit1: TEdit
    Left = 52
    Top = 76
    Width = 225
    Height = 21
    TabOrder = 0
    OnDblClick = Edit1DblClick
  end
  object Edit2: TEdit
    Left = 48
    Top = 208
    Width = 229
    Height = 21
    TabOrder = 1
    OnClick = Edit2Click
    OnDblClick = Edit2DblClick
    OnKeyDown = Edit2KeyDown
  end
  object Button1: TButton
    Left = 412
    Top = 68
    Width = 75
    Height = 25
    Caption = 'Load'
    TabOrder = 2
    OnClick = Button1Click
  end
end


regards
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

hidrauAuthor Commented:
Hi pcsentinel
I did understand what you have made, but I think we aren't talking about the same thing, see if you understood what I need.


FIRS EDIT1
 
is working here working and there my sister


SECOND EDIT2

She is here with my father.
She is _______ here with my father


The user forgot putting the word "working" in that blank space,(of course, there will not be any blank space, it is an example) , if the user click on the word "working" this word is gonna be placed at the last word, because of that I need to get the word with mouse and drag it at the right position that is between is and here.

Thanks
Alexandre
Slick812Commented:
hello hidrau, , I think that I understand what you want, a way to "Mouse Drag" a seleted word from one RichEdit to another richEdit. . .
I looked at your code and I have some questions about it

1. .  Why are you using the RxRichEdit component, does it have some special property that you need ? ? (I do not see any in your code) .
         and I am NOT familar with the RxRichEdit. .

2. Why do you need the  -  VisibleLines(RE: TRxRichEdit):    function  ? ? and why do you have these lines of code -
     NoLines:=VisibleLines(RxRichEdit1) div 2;
     for X:=1 to NoLines do  RxRichEdit1.Lines.Insert(0, ' ');

3.  Did you know that a TRichEdit has the ability to drag the selected Text to another Rich Edit
         and it will drop the text in to that Rich Edit where ever you release the mouse?
hidrauAuthor Commented:
Hello slick812

Ok, let me explain.

1) I use the rxrichedit or richedit because I need to make other things that I didn't relate here. But the example could be with edit, after that I will make the changes to richedit.

2) Visiblelines is a function only to align the text in richedit vertically. It counts how many lines there is in the richedit and put the sentence in the middle of it.

3) I know that I can select the text, but how could I transfer the selected text to other richedit at the right place, I don't know.


This is the problem, pcsentinel has helped me a lot, he gives me an example using edits.

Thanks
Alexandre
Slick812Commented:
3)  If to have selected text in a TRIchEdit. .  then just place your mouse cursor over the seleded Text and press and hold the Left Mouse button down. . then move the mouse cursor to the RichEdit2 and place your mouse cursor at the Point you want to insert the Text and release the left mouse button, , and the RichEdit2 will automatically insert ALL of the selected Text in RichEdit1, into the position of the mouse cursor over RichEdit2. .  you should try this. . .


since a TRichEdit already has the ability to "Drag" selected text to another TRichEdit, I will not do any code for dragging text on a TRichEdit. .
What I do is a "Simple" way to get the drag and drop thing for your game. I do NOT have any  Double Click for the RichEdits. .  I use a single  "Right Click" for the game word section in the RichEdit1. .  and the user then "Drags and Drops" the selected text onto RichEdit2. .

First I will scramble the words in a Text string, I only use the "Space Character" as a "Word" separator. .  To do the scramble, I have the  ScrambleWords function -

function ScrambleWords(const Text: String): String;
var
hold: String;
i, j: Integer;
pStart, pNext: PChar;
aryWords: Array of String;
begin
// this fuction will also chop OUT all extra spaces in the Text
// The result String will always have a Space character at the begining and end
// if the length of Text is less than 6 then there is not enough to use
if Length(Text) < 6 then
  begin
  Result := '';
  Exit;
  end;
hold := Text+' ';

while hold[1] = ' ' do
  Delete(hold, 1,1);
if Length(hold) < 5 then
  begin
  Result := '';
  Exit;
  end;

pStart := PChar(hold);
pNext := StrScan(pStart,' ');

while pNext <> nil do
  begin
  if pNext <> pStart then
    begin
    SetLength(aryWords, Length(aryWords)+1);
    SetString(aryWords[High(aryWords)],pStart, pNext-pStart);
    end;
  pStart := pNext+1;
  pNext := StrScan(pStart,' ');
  end;

Randomize;
for i := 0 to High(aryWords) do
  begin
  hold := aryWords[i];
  j := Random(High(aryWords));
  aryWords[i] := aryWords[j];
  aryWords[j] := hold;
  end;

Result := ' ';
for i := 0 to High(aryWords) do
  Result := Result+aryWords[i]+' ';
end;


= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Here is a Button Click to set Up the 2 TRichEdits -

procedure TForm2.sbut_RandomWordsClick(Sender: TObject);
var
Text: String;
begin
Text := ' This     is the    Text  that'#10#13'WILL   be scrambled ';
Text := StringReplace(Text,#10,'',[rfReplaceAll]);
Text := StringReplace(Text,#13,'',[rfReplaceAll]);
RichEdit1.Text := ScrambleWords(Text); // RichEdit1 gets the scrambled words
RichEdit2.Clear; // nothing is in RichEdit2
end;
// this adds the Text to the RichEdit, I do NOT chop out all the
// RichEdit Lines after the first one, you code does not seem to be nessary
// seems like you would limit the length of the Text to the width of the RichEdit


 = = = = = = = = = = = = = = = =

In order to "Select" the correct text for the game's Drag and Drop, with a space before and after the selected word, I have made the "Right Mouse Click" the Game selection click, with this code -
this is the RichEdit1  OnMouseUp  Event -

procedure TForm2.RichEdit1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
SL: Integer;
begin
inherited;
if Button = mbRight then
  begin
  // I send the Double click message so a single "Word" will be selected
  RichEdit1.Perform(WM_LBUTTONDBLCLK,0,MakeLParam(X,Y));
  Application.ProcessMessages;
  SL := RichEdit1.SelLength+1;
  if RichEdit1.SelStart > 0 then
    begin // I add the Space before and after word to the seleted text
    RichEdit1.SelStart := RichEdit1.SelStart-1;
    Inc(SL);
    end;

  if SL > 0 then
     RichEdit1.SelLength := SL;
  end;
end;

 = = = = = = = = = = = = = = = = = = = = = = = = = =

for the RichEdit2, I had to add the OnChange Event, so I could erase the extra spaces that the drag and drop may add. .


procedure TForm2.RichEdit2Change(Sender: TObject);
var
Text: String;
len: Integer;
begin
len := Length(RichEdit2.Text);
Text := StringReplace(RichEdit2.Text,'  ',' ',[rfReplaceAll]); // make all double Space to single
if length(Text) <> len then
RichEdit2.Text := Text;
end;

= = = = = = = = = = = = = = = = = = = = = = = = = = =

Now

right Click a Word in the RichEdit1  (with the scrambled text) and that word should be Selected. . . now drag and drop that selected word to the place in RichEdit2, that you want it to be placed , and then release your mouse button, the word shoul be inserted in to the RichEdit2

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
Slick812Commented:
I had much better success if I used two  TStringGrids  for this "Game" ,  instead of two TRichEdits



  private
    { Private declarations }
    aryWords: Array of String;
    PlayGame: Boolean;

    procedure SetGrids(const Text: String);
    function WordsCorrect: Boolean;







procedure TForm1.SetGrids(const Text: String);
var
hold: String;
i, j, tw, maxWidth: Integer;
pStart, pNext: PChar;
aryWd: Array of String;
begin
if Length(Text) < 3 then Exit;

hold := Text+' ';
tw := Length(hold)+1;
i := 1;
while i < tw do
 if hold[i] = ' ' then
   Inc(i) else Break;

if i = tw then Exit;
SetLength(aryWords, 0);
Dec(i);
pStart := PChar(hold)+i;
pNext := StrScan(pStart,' ');

while pNext <> nil do
  begin
  if pNext <> pStart then
    begin
    SetLength(aryWords, Length(aryWords)+1);
    SetString(aryWords[High(aryWords)],pStart, pNext-pStart);
    end;
  pStart := pNext+1;
  pNext := StrScan(pStart,' ');
  end;

if Length(aryWords) < 2 then
  begin
  SetLength(aryWords, 0);
  Exit;
  end;

setLength(aryWd, Length(aryWords));
for i := 0 to High(aryWords) do
  aryWd[i] := aryWords[i];

Randomize;
for i := 0 to High(aryWords) do
  begin
  hold := aryWd[i];
  j := Random(High(aryWd));
  aryWd[i] := aryWd[j];
  aryWd[j] := hold;
  end;

StringGrid1.ColCount := Length(aryWd);
StringGrid1.RowCount := 1;
StringGrid2.ColCount := StringGrid1.ColCount;
StringGrid2.RowCount := 1;

maxWidth :=6;
for i := 0 to High(aryWd) do
  begin
  tw := StringGrid1.Canvas.TextWidth(aryWd[i]);
  if tw > maxWidth then
    maxWidth := tw;
  StringGrid1.Cells[i, 0] := aryWd[i];
  StringGrid2.Cells[i, 0] := '';
  end;

StringGrid1.DefaultColWidth := maxWidth+6;
StringGrid2.DefaultColWidth := maxWidth+6;
StringGrid1.Col := 0;
StringGrid1.Row := 0;
StringGrid2.Col := 0;
StringGrid2.Row := 0;
PlayGame := True;
end;


procedure TForm1.sbut_NewWordsClick(Sender: TObject);
begin
SetGrids('Please try and place these words into the correct order');
end;


procedure TForm1.StringGrid1MouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
inherited;
if PlayGame then
  StringGrid1.BeginDrag(False,4);
end;

procedure TForm1.StringGrid1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
DragObject := nil;
end;

procedure TForm1.StringGrid2DragOver(Sender, Source: TObject; X,
  Y: Integer; State: TDragState; var Accept: Boolean);
begin
if Source = StringGrid1 then Accept := True;
end;

function TForm1.WordsCorrect: Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to High(aryWords) do
  if aryWords[i] <> StringGrid2.Cells[i,0] then Exit;
PlayGame := False;
Result := True;

ShowMessage('GOOD WORK ! ! All the words are correct');
end;

procedure TForm1.StringGrid2DragDrop(Sender, Source: TObject; X,
  Y: Integer);
var
aRow, aCol: Integer;
begin
if Source = StringGrid1 then
  begin
  Y := 6;
  if X > StringGrid2.CellRect(StringGrid2.ColCount-1, 0).Right then
    X := StringGrid2.CellRect(StringGrid2.ColCount-1, 0).Right;
  StringGrid2.MouseToCell(X,Y,aCol,aRow);
  if aCol < 0 then Exit;
  StringGrid2.Cells[aCol,aRow] := StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row];
  WordsCorrect;
  end;
end;


procedure TForm1.StringGrid1DblClick(Sender: TObject);
var
i: Integer;
begin
if not PlayGame then Exit;
for i := 0 to StringGrid2.ColCount-1 do
  if StringGrid2.Cells[i, 0] = '' then
  begin
  StringGrid2.Cells[i, 0] := StringGrid1.Cells[StringGrid1.Col, StringGrid1.Row];
  WordsCorrect;
  break;
  end;
end;
hidrauAuthor Commented:
Thanks Friend, I am gonna check this option.
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.