DBGrid1 destroy and create it again? lazarus / 16-Databases ---------------------------------------------------------------------- Elmug · 2012-08-08 17:02 Hi everyone, I found DBGrid1.Destroy; destroys it. But how to create it again, immediately after destroying it? Thanks. ---------------------------------------------------------------------- ludob · 2012-08-08 17:58 QuoteBut how to create it again, immediately after destroying it?DBGrid1:=TDBGrid.Create(AOwner); ---------------------------------------------------------------------- Elmug · 2012-08-08 18:00 That I tried some time ago. I now am about to try:   with TDBGrid.Create(Self) do   begin     // Set properties of the grid     DataSource := xxxxx; // Connect the source to the grid   end; I hope this will work. Thanks. ---------------------------------------------------------------------- Dick, from the internet · 2012-08-08 18:01 Maybe something like: warning:  untested code!!! DBGrid1 := TDBGrid.Create(self); And BTW, as I recall, you are not supposed to call [object].Destroy directly, but use [object].Free instead.  Maybe someone will fact check me here. geno. edit:  damn - ludob beat me to the punch. ---------------------------------------------------------------------- Elmug · 2012-08-08 18:21 Quote from: Elmug on August 08, 2012, 08:00:54 pmThat I tried some time ago. I now am about to try:   with TDBGrid1.Create(Self) do   begin     // Set properties of the grid     DataSource := xxxxx; // Connect the source to the grid   end; I hope this will work. Thanks. ---------------------------------------------------------------------- ludob · 2012-08-08 18:35 QuoteAnd BTW, as I recall, you are not supposed to call [object].Destroy directly, but use [object].Free instead.  Maybe someone will fact check me here. Correct. Free checks if the object is not nil before calling destroy. This is important for TComponents that have an owner. The owner tries to destroy its children when destroying itself. So you could end up destroying an object that is already destroyed just because the order of destruction is not what you had in mind. ---------------------------------------------------------------------- Elmug · 2012-08-08 21:07 So, what's the opposite of DBGrid1.Free? How do I get it back? Thanks. By the way, I have a test button in my form that on click does DBGrid1.Destroy without any problems. But I don't mean to contradict the avoiding destroy. I can see the DBgrid vanish. Then I have to turn off the app, because it needs the DBGrid, of course. ---------------------------------------------------------------------- Dick, from the internet · 2012-08-09 00:20 OK, so refresh my memory.  Why exactly are you destroying the grid and then re-creating it?  What is it doing, or not doing? Can you not just close your query [SQLQuery1.Close], make the appropriate adjustments on the grid through your code, then open your query once again [SQLQuery1.Open]? ---------------------------------------------------------------------- ludob · 2012-08-09 06:23 QuoteSo, what's the opposite of DBGrid1.Free?How many times do you want the same answer? You have even repeated it yourself:QuoteDBGrid1:=TDBGrid.Create(AOwner); If you have problems setting the properties, see this thread: http://www.lazarus.freepascal.org/index.php?topic=8912.0 If you don't know which properties to set to get the dbgrid being exactly the same as the original one, look into the form's lfm and check what properties are set and thier values. ---------------------------------------------------------------------- Elmug · 2012-08-09 09:20 Quote from: ludob on August 09, 2012, 08:23:36 amQuoteSo, what's the opposite of DBGrid1.Free?How many times do you want the same answer? You have even repeated it yourself:QuoteDBGrid1:=TDBGrid.Create(AOwner); If you have problems setting the properties, see this thread: http://www.lazarus.freepascal.org/index.php?topic=8912.0 If you don't know which properties to set to get the dbgrid being exactly the same as the original one, look into the form's lfm and check what properties are set and thier values. Why would you want to Create the TDBGrid if it was not destroyed; the question is after doing a TDBGrid1.Free. , unless Free destroys it, why have to Create it? Does not this contradict your explanation of "destroy can affect children", if you Free, and then Create, what happens to those children, are they still related? Shouldn't you just NEED TO  put the properties back after a Free? Also, I found a simpler faster way to do this things, without having to set properties again. Thanks. ---------------------------------------------------------------------- taazz · 2012-08-09 09:51 Quote from: Elmug on August 09, 2012, 11:20:39 amDoes not this contradict your explanation of "destroy can affect children", if you Free, and then Create, what happens to those children, are they still related? Shouldn't you just NEED TO  put the properties back after a Free? Also, I found a simpler faster way to do this things, without having to set properties again. Free ultimately calls destroy and destroys the component. In the before destruction method the component will remove it self from the owners destruction list. After free you need to call create again and set the properties all over again. there is not magic bullet that would help you out of this. ---------------------------------------------------------------------- Elmug · 2012-08-09 09:59 Quote from: taazz on August 09, 2012, 11:51:00 amQuote from: Elmug on August 09, 2012, 11:20:39 amDoes not this contradict your explanation of "destroy can affect children", if you Free, and then Create, what happens to those children, are they still related? Shouldn't you just NEED TO  put the properties back after a Free? Also, I found a simpler faster way to do this things, without having to set properties again. Free ultimately calls destroy and destroys the component. In the before destruction method the component will remove it self from the owners destruction list. After free you need to call create again and set the properties all over again. there is not magic bullet that would help you out of this. That's what I thought. ---------------------------------------------------------------------- ludob · 2012-08-09 11:06 Quote from: Elmug on August 09, 2012, 11:20:39 amWhy would you want to Create the TDBGrid if it was not destroyed; the question is after doing a TDBGrid1.Free. , unless Free destroys it, why have to Create it? What part of the following do you not understand? Quote from: ludob on August 08, 2012, 08:35:08 pmFree checks if the object is not nil before calling destroy. If it is a matter of understanding English then here is the code: Code: [Select]      procedure TObject.Free;         begin             if self<>nil then              self.destroy;         end; ---------------------------------------------------------------------- Elmug · 2012-08-09 16:28 Quote from: ludob on August 09, 2012, 01:06:58 pmQuote from: Elmug on August 09, 2012, 11:20:39 amWhy would you want to Create the TDBGrid if it was not destroyed; the question is after doing a TDBGrid1.Free. , unless Free destroys it, why have to Create it? What part of the following do you not understand? Quote from: ludob on August 08, 2012, 08:35:08 pmFree checks if the object is not nil before calling destroy. If it is a matter of understanding English then here is the code: Code: [Select]      procedure TObject.Free;         begin             if self<>nil then              self.destroy;         end; I never bothered with the question, but since you bother to ask, I will bother to answer: The part that I don't understand is why Destroy itself does not check if nil. Also, how does Free protect its members, or anything if it destroys if not nil? Do you understand why? If so please explain it to me. ---------------------------------------------------------------------- ludob · 2012-08-09 18:11 QuoteThe part that I don't understand is why Destroy itself does not check if nil.Destroy is a virtual method overridden in a lot of objects while free can not be overridden. So the question "why Destroy itself does not check if nil" has to be asked to a lot of developers. On the other hand there is only one version of Free and that is documented to check if not nil before calling destroy. I don't know if you have already written a class destructor but if you did, please look it up and verify if you have made a check on self being nil before doing the job. I guess that will answer your question. QuoteAlso, how does Free protect its members, or anything if it destroys if not nil?I don't understand that question. What members of Free? What is anything? What protection? Are you afraid for your personal safety when Free destroys? It sounds very philosophical and probably belongs to another forum ---------------------------------------------------------------------- teos · 2012-08-22 11:25 Quote from: Elmug on August 08, 2012, 08:00:54 pmThat I tried some time ago. I now am about to try:   with TDBGrid.Create(Self) do   begin     // Set properties of the grid     DataSource := xxxxx; // Connect the source to the grid   end; I hope this will work. Thanks. That does not work when you need dbGrid1. You can set the name but the object is only re-created if you use dbgrid1 := TDBGRid.Create(); With TDBGrid.Create do only creates a new, kind of unidentified dBGrid. This call will give acces violations if you address dbgrid1 in code. Quote from: ludob on August 09, 2012, 08:11:12 pmQuoteThe part that I don't understand is why Destroy itself does not check if nil.Destroy is a virtual method overridden in a lot of objects while free can not be overridden. So the question "why Destroy itself does not check if nil" has to be asked to a lot of developers. On the other hand there is only one version of Free and that is documented to check if not nil before calling destroy. I don't know if you have already written a class destructor but if you did, please look it up and verify if you have made a check on self being nil before doing the job. I guess that will answer your question. QuoteAlso, how does Free protect its members, or anything if it destroys if not nil?I don't understand that question. What members of Free? What is anything? What protection? Are you afraid for your personal safety when Free destroys? It sounds very philosophical and probably belongs to another forum I don't think Destroy has to check again for not nil -> that is allready taken care of in the Free. The destructor is THE place where to do cleaning up if needed. If in a container, again not by destroying objects but by calling Free.