Timage lazarus / 9-Editor ---------------------------------------------------------------------- T-bear · 2010-10-10 12:03 Hi, I want to change some properties of an image and it should be like this: - When image1 is pressed X get the value 1, with image2 it gets 2... - When a button is pressed i want to change the properties of the image[X]. Anyone who nows how to do that without If Then or case? Thanks in advance! ---------------------------------------------------------------------- typo · 2010-10-10 12:07 Why without If Then or case? What properties do you want to change? ---------------------------------------------------------------------- T-bear · 2010-10-10 12:10 I would get a little bit much code, with 16 pictures and 5 buttons. And in the next version it gets even more... ---------------------------------------------------------------------- typo · 2010-10-10 12:16 Well, on the click event for each image you could put: Code: [Select]X := Value;  and on the same event for the buttons you could put: Code: [Select]ImageProperty := Value; ---------------------------------------------------------------------- T-bear · 2010-10-11 11:16 Hi again. I dont really know what you mean ... Can you give an example? ---------------------------------------------------------------------- typo · 2010-10-11 11:27 1) Select the image or button on the form editor. 2) Find on the Events tab of Object Inspector the event OnClick. Double click on it and the event will be created on the code editor. Write the code on the code editor. ---------------------------------------------------------------------- T-bear · 2010-10-11 11:42 OK. I Have made the image onClick procedures. They set the value X. Now i just need to know how i can change the properties of image[ x ] with the same code without If Then or Case. How can i do that? ---------------------------------------------------------------------- typo · 2010-10-11 12:05 Just write: Code: [Select]Image1.DesiredProperty := DesiredValue; ---------------------------------------------------------------------- T-bear · 2010-10-11 12:12 Butt then the property of image1 gets changed. I want to change the property of the image wich has the number X. (If X = 1 Then i want to change the property of image1, when X = 2 i want to change the property of image 2) ---------------------------------------------------------------------- typo · 2010-10-11 12:21 Code: [Select]TImage(FindComponent('image' + IntToStr(X))).DesiredProperty := DesiredValue; ---------------------------------------------------------------------- T-bear · 2010-10-11 12:56 Thank you. Know i understand. ---------------------------------------------------------------------- mas steindorff · 2010-10-11 15:53 If you are changing all of the images in the same way, you can set the OnClick event of all of them to the same code and then use Sender as the pointer to the clicked image.  In my tic tac toe game.. Code: [Select]procedure TFMain.SquareClick(Sender: TObject); // sender is the image begin   if SquareUsed(Sender) then      exit;   if singlePlayer then        PlayCPU(Sender)   else PlayTwoPlayer(Sender); end; function TFMain.SquareUsed( Sender: TObject): boolean; var pic:TImage; begin   pic := TImage(sender);   result := (pic.tag <> 0); // tag of the image set when image is taken end; .... ---------------------------------------------------------------------- T-bear · 2010-10-12 12:39 Hello again, I have a new question:  Is it possible to get the number out of the name of the picture (Like 1 out of image1)? ---------------------------------------------------------------------- typo · 2010-10-12 13:15 StrToInt(Copy(image1.name, Length('image') + 1, 100)); ---------------------------------------------------------------------- T-bear · 2010-10-14 10:13 Hi, One question: Where do i tell which variable the value shall be in? And i get this messages when i run it: Unit1.pas(118,23) Error: Identifier not found "image1" ---------------------------------------------------------------------- typo · 2010-10-14 10:22 If we could have an overview of what are you trying to do, we could answer in a better way. ---------------------------------------------------------------------- T-bear · 2010-10-14 16:59 Well, i am making a game, where you build buildings and get ressources. Each TImage is a field where you can build. You select the images by clicking on them, and then you build by pressing a button. ------ If you need more information please tell. ---------------------------------------------------------------------- mas steindorff · 2010-10-14 17:35 Quote from: T-bear on October 14, 2010, 06:59:58 pmWell, i am making a game, where you build buildings and get ressources. Each TImage is a field where you can build. You select the images by clicking on them, and then you build by pressing a button. ------ so like my tic tac toe game, you will have several TImages for the user to see that will be modified with a new picture as the game goes on.  But the TImage is not a complete object as far as your game is concerned because it lacks the storage area to record "beams, dirt, and 2x4 boards, bills ...".  At this point you can do a few things.  1st, create an object that includes a TImage as well as all of the bookkeeping stuff.  If you don't want to create the timage at run time (IE use the IDE to place in on your form) then your object should be initiated with a pointer to the TImages you wish to link it to.  you can then redirect the OnClick event to code within you new object.  that way you just use self.dirt to access just that building's resources. If your not up to creating and using objects another way to tie each TImages to some info is to create an array of "resources" and set each TImages.Tag = to the index of resources.  then you can know which TImage was clicked with a common OnClick event by looking at TImage(Sender).Tag to get the array index. ---------------------------------------------------------------------- zariq · 2010-10-15 15:50 Drbob has a nice page on his site which is very useful for anybody who wants to learn how to write a component or extend an existing one. His example is also based on a tic tac toe game. http://www.drbob42.com/delphi/componen.htm I used that as a starting point when I needed a tabcontrol for wince. There is also the wiki which has an example. ---------------------------------------------------------------------- T-bear · 2010-12-12 18:59 Hi again.  I dont think i understood that.    Im a pretty big noob at lazarus. But is it posible to get the value out of the name as described above? I would know how to do it then. Thanks ---------------------------------------------------------------------- typo · 2010-12-12 19:16 StrToInt(Copy(image1.name, Length('image') + 1, 5)); ---------------------------------------------------------------------- T-bear · 2010-12-12 19:21 And is it possible to get it into an integer variable? ---------------------------------------------------------------------- typo · 2010-12-12 19:22 Yes. ---------------------------------------------------------------------- T-bear · 2010-12-12 19:24 How? ---------------------------------------------------------------------- typo · 2010-12-12 19:28 Code: [Select]var   n :integer; begin   n := StrToInt(Copy(image1.name, Length('image') + 1, 5)); end; ---------------------------------------------------------------------- T-bear · 2010-12-12 19:38 And how can i use the same code for several images? ---------------------------------------------------------------------- typo · 2010-12-12 19:49 Replacing "image1.name" with each TImage name. ---------------------------------------------------------------------- T-bear · 2010-12-12 19:54 But how can i do that without having that much code? With that i would get 144 of that. And i would need one onclickevent for each of the 144 images. ---------------------------------------------------------------------- typo · 2010-12-12 20:03 You can use only one event handler to all images: Code: [Select]procedure TForm1.Image1Click(Sender: TObject); begin    if Sender is TImage then      if StrToInt(Copy(TImage(Sender).Name, Length('image') + 1, 6)) = 1 then        ShowMessage('Image number 1'); end; You need to link each image to this event handler. ---------------------------------------------------------------------- typo · 2010-12-12 20:07 Better: Code: [Select]procedure TForm1.Image1Click(Sender: TObject); begin   case StrToInt(Copy(TImage(Sender).Name, Length('image') + 1, 5)) of   1: DoSomething;   2: DoSomethingElse;   3..10: DoAnotherThing;   {...}   144: Do;   end; end;