• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 222
  • Last Modified:

Unclassified edit related stuff (in for a word search thingie?)

Hi Guys!

I made a square consisting of 4x4 edits. Each edit displays but one character.
 If I click on an edit, I want its background to change to clRed instead of the default clWhite, unless it already is clRed, then it is supposed to change into clWhite.
Now the hard part:)
I have a label on that form (lblword) and its caption should reflect all the characters of the edits of which the background is colored clRed. If the clicking turns an edits background into clWhite again, that edits character should be removed.
Ex. > 
edit1.text := 'A'
edit2.text := 'B'
edit3.text := 'C'
edit4.text := 'D'

Clicking on edit1 edit2 edit3 and edit4 (in the order clicked) should turn lblword.caption into: ABCD, that is if all these edits backgrounds are clRed.
Now I can't get this thing to work. The color swapping thingie aint the problem but the character adding\removing surely is. If all the backgrounds of all edits are clWhite the lblword.caption should be empty. The first of the edits to be clicked, turning its background into clRed, would then turn the lblword.caption into that edits text. If another edit is clicked and turned clRed that edits character should be added to the lblword.caption. If the last clicked edit of which the background is clRed is clicked again, its background should turn into clWhite and its character should be removed from the lblword.caption.
When the lblword.caption is equal to some predefined string, the fun is over and the lblword.caption should be blanked and all the edits backgrounds should be clWhite again.

  Btw > if a label suits better instead of the edits I wish to use, then that is ok.

My first issue, adding and removing the characters of clicked edits is the most important and I think its possible to do so. The second is of minor importance. Providing code which solves the first issue grants you the points.

Here's part of my code which handles the colorchanging thingie as well as the lblword.caption-changing-thingie (incorrectly btw):

//all of the edits use this procedure

procedure TForm1.EditClick(Sender:TObject);
var Edit: TEdit;
begin
// Grab hold of the particular edit > there will be lots of them named from edit1 up to edit64 or more
Edit := FindComponent((Sender as Tedit).Name) as tEdit;

// here comes  my attempt to prevent addition of this edits.text in case its red, since then it is already selected > it should
// now change into clWhite and do nothing, I think (?) ,furthermore if its clWhite it should turn into clRed and continue here
if Edit.Color = clRed then  exit else
edit.color := clRed;

//add this edits text to the lblword.caption
lblword.caption := lblword.Caption + edit.text;

//compare the 'created' word with some predefined string and if they match the fun is over
if lblword.Caption = SomePredefinedStringieThingie then
begin
//
end;
end;
end;
//

Doubleclicking should reset all edits backgrounds to clWhite and blank the lblword.caption, that part works to my satisfaction (hoera hoera! ;)
Every editclick adds its character to my lblword.caption and my attempt to prevent that by using its current color
resulted in nothing.

Now if you need more info let me know, I'm pretty determined to get this thing working one way or the other (the removing\adding thingie I mean)

Regards Peter

Please modify the provided code or add or remove what should not be there and comment it well because it is obvious to me I am missing an important part here but even after searching my room for a couple of hours still no trace of it :)

Ps Bonus >Another thing I'm curious about is whether it is possible to drag over the different edits and let that drag do the color change as well as altering the lblword.caption into the 'dragselected edit characters' > I saw that behaviour at work in a word search puzzle, some java class file. Now this dragging thingie is not the important thing of this project, just something I'm curious about as to whether its possible, cause I doubt that.
0
PeterdeB
Asked:
PeterdeB
  • 9
  • 7
1 Solution
 
shaneholmesCommented:
something like this:


procedure TForm1.UpdateWord;
var
 I: Integer;
begin
 lblWord.Caption:= '';
 for I:= 0 to ComponentCount - 1 do
  if COmponents[I] is TEDit then
   if TEdit(Components[I]).Color = clRed then
    lblWord.Caption:= lblWord.Caption +  TEdit(Components[I]).Text;
end;

procedure TForm1.Edit1Click(Sender: TObject);
begin
 if TEdit(Sender).Color = clWindow then
  TEdit(Sender).Color:= clRed
 else
  TEdit(Sender).Color:= clWIndow;
  UpdateWord;
end;

sholmes
0
 
shaneholmesCommented:
procedure TForm1.Edit3DblClick(Sender: TObject);
var
 I: Integer;
begin
 lblWord.Caption:= '';
 for I:= 0 to ComponentCount - 1 do
  if COmponents[I] is TEDit then
  begin
   TEdit(Components[I]).Color:= clWindow;
   TEdit(Components[I]).Text:= '';
  end;
end;
0
 
PeterdeBAuthor Commented:
Geez didn't except a reply this quick! Problem solved!

Regards Peter
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
PeterdeBAuthor Commented:
Hi Shane!

How can I make sure that the lblword.caption displays the characters in the order the edits were clicked? I'd say I'd have to modify the UpdateWord procedure but numerous attempts to do so failed.
Is there a way to let the order the edits were clicked determine  in which order the characters of the lblword.caption are displayed?

Regards Peter
0
 
shaneholmesCommented:
Im working on something for you

sholmes
0
 
PeterdeBAuthor Commented:
This seems to work, but I'm still testing it though:

procedure TForm1.EditClick(Sender: TObject);
var edit:Tedit;
begin  if (Sender is TEdit) then
    begin
    Edit := FindComponent((Sender as Tedit).Name) as tEdit;
    if Edit.Color = clWindow then
    Edit.Color := clRed
 //if TEdit(Sender).Color = clWindow then
  //TEdit(Sender).Color:= clRed
 else Edit.Color := clWindow;
  //TEdit(Sender).Color:= clWIndow;
  UpdateWord;
  if lblWord.Caption = lbl1.Caption then
  begin
  timer1.Enabled := false;
  pagecontrol1.ActivePageIndex := pagecontrol1.ActivePageIndex + 1;
end;
end;
end;
 

Regards Peter
0
 
PeterdeBAuthor Commented:
Oh okay > I figured you wouldn't be online right now so I fired Delphi up to see whether I could 'find' or 'see' the missing link. I astonished myself when I suddenly got it working and can't really say I comprehend why it works now, but alas > so far the order the edits were clicked seems to be reflected by the lblword.caption.

Mind me I'm curious as to what your solution is. I started with modifying the UpdateWord procedure but that seemed the wrong approach so I then started modifying the edit procedure and suddenly got it working.

Regards Peter
0
 
shaneholmesCommented:
How many total TEDits will there be any any given time?  4 ?

sholmes
0
 
shaneholmesCommented:
Give me alittle bit - im trying to work on two things at once - <SMILE>

I'll get it for you though.....

sholmes
0
 
shaneholmesCommented:
OK, im back working on yours - give me a few minutes

sholmes
0
 
shaneholmesCommented:
Try this out


unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    lblWord: TLabel;
    procedure Edit1Click(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    procedure UpdateWord;
  end;

var
  Form1: TForm1;
  AList: TStringList;

implementation

{$R *.dfm}


procedure TForm1.UpdateWord;
var
 I: Integer;
begin
 lblWord.Caption:= '';
 for I:= 0 to AList.Count - 1 do
  lblWord.Caption:= lblWord.Caption + AList[I];
end;


procedure TForm1.Edit1Click(Sender: TObject);
var
 Indx: Integer;
begin
 if TEdit(Sender).Color = clWindow then
 begin
  TEdit(Sender).Color:= clRed;
  AList.Add(TEdit(Sender).Text);
 end
 else
 begin
  TEdit(Sender).Color:= clWindow;
  AList.Find(TEdit(Sender).Text, Indx);
  AList.Delete(Indx);
 end;
 UpdateWord;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
 AList.Free;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 AList:= TSTringList.Create;
end;

end.
0
 
PeterdeBAuthor Commented:
Okay Shane tnx! It works now the way I wanted.

 Is it possible to fill each and every edit with  a space + a randomly chozen character ranging from > A to Z > in delphi this would be:

// having 26 edits should result in 26 random characters all with a space
edit.text := ' A'; // just to make sure there should be a space inserted as well

This ofcourse is another question, but mind me you will be rewarded or anyone who can help me out on this one.

Regards Peter
0
 
PeterdeBAuthor Commented:
I'll post this as a new question, hang on.

Regards Peter
0
 
shaneholmesCommented:
Tell ya what - firt i'll try and help you figure the code out - if you can't get it, then i'll write it for you

1.) look at Randomize & random for doing random numbers

2.) use the Char function to show the string representation of a ascii decimal

3.) use the Ord function to get the ordinal representation of a ascii char


example:


char(65)   = A

char(90)   = Z

Ord('A') = 65
Ord('Z') = 90


Hope this helps!

sholmes
0
 
shaneholmesCommented:
procedure TForm1.Button1Click(Sender: TObject);
var
 I, C: Integer;
begin
 randomize;
 for I:= 0 to ComponentCount -1 do
  if Components[I] is TEdit then
  begin
   repeat
    C:= Random(90) + 1;
   until (C > 65) AND (C <= 90);
   TEdit(Components[I]).Text:= Char(C);
  end;
end;
0
 
PeterdeBAuthor Commented:
:)
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now