Recent

Author Topic: [SOLVED] How to change a control's parent  (Read 4799 times)

carlangas

  • Jr. Member
  • **
  • Posts: 57
[SOLVED] How to change a control's parent
« on: October 01, 2018, 07:54:00 pm »
I have some tabs and inside each of them, a TFrame containing, among others, a TMemo. The idea is that, as sometimes the TMemo can be too small, when I double-click on it, another form opens and the TMemo is shown inside of it, returning to its place once I close the form.

I have some procedures attached to OnKeyPress and OnKeyDown events of the TMemo, that remain in the original unit.

Everything works well, but once I close the Form, if I run it from the command line, I get the message

Code: Pascal  [Select][+][-]
  1. GLib-GObject-CRITICAL **: g_object_get_data: assertion 'G_IS_OBJECT (object)' failed

and after doing it some other times, an empty window turns up and I have to kill the process.

This is the way I change the memo from one place to another (apparently the warning goes after finishing this procedure):

Code: Pascal  [Select][+][-]
  1. procedure TMarco.Memo1DblClick(Sender: TObject);
  2. begin
  3.  
  4. With Memo1 do
  5. begin
  6.   Parent:=Editor1;
  7.   Left:=3;
  8.   Top:=3;
  9.   Width:=Editor1.Width-6;
  10.   Height:=Editor1.Height-6;
  11. end;
  12. Editor1.ShowModal;
  13.  
  14.  
  15.  
  16. Memo1.Parent:=Button1.Parent;
  17.   With Memo1 do
  18.   begin
  19.     Left:=472;
  20.     Top:=69;
  21.     Width:=Parent.Width-478;
  22.     Height:=Parent.Height-75;
  23.   end;
  24.  
  25. end;                              

Editor1 is the Form, and since I don't know how to refer to the Frame, I do it as the parent of Button1, another Control inside it.

What is going wrong?
« Last Edit: October 02, 2018, 07:35:48 pm by carlangas »

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: How to change a control's parent
« Reply #1 on: October 01, 2018, 08:15:32 pm »
The idea is that, as sometimes the TMemo can be too small, when I double-click on it, another form opens and the TMemo is shown inside of it, returning to its place once I close the form.
May be easier to copy text to TMemo for a pop-up form?

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: How to change a control's parent
« Reply #2 on: October 01, 2018, 08:28:43 pm »
Yes, that is what I am thinking of doing to solve it. Nevertheless, it seems to work alright, until it sends out that message. The procedure does what it is meant to, so I think it is a pity not to try to solve it, but I don't know what the problem is.

Your solution only would require a bit more of code, because of the procedures I use to process the keys, although it is just a matter of copy-paste.

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: How to change a control's parent
« Reply #3 on: October 01, 2018, 10:06:10 pm »
...because of the procedures I use to process the keys, although it is just a matter of copy-paste.
You can use common frame with TMemo and events.

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: How to change a control's parent
« Reply #4 on: October 01, 2018, 10:29:34 pm »
I gave up and did what you said: To copy the text to another TMemo and back to the original one when closing the popup. It works, just as before, but now what happens is that on moving the mouse I find that the text between the starting position of the cursor and where the mouse cursor is gets blue, just as if I were selecting it for copying. How can I prevent this from happening?

ASerge

  • Hero Member
  • *****
  • Posts: 2242
Re: How to change a control's parent
« Reply #5 on: October 02, 2018, 11:18:02 am »
It works, just as before, but now what happens is that on moving the mouse I find that the text between the starting position of the cursor and where the mouse cursor is gets blue, just as if I were selecting it for copying. How can I prevent this from happening?
I understood correctly that when a double-click is done, the current word in TMemo is highlighted and a window pops up. And you want to remove the selection (before or after popup)?
In this case use Memo1.SelLength := 0;

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: How to change a control's parent
« Reply #6 on: October 02, 2018, 05:12:16 pm »
No. When I double-click, the popup form opens normally and everything works fine, but once I close the popup it seems to believe the left button of the mouse is pushed and when I move it, it behaves as if I were selecting (pushing the left button and dragging).

Maybe it would be possible to force it to realise the mouse button is released, but I cannot find how to do it.

Meanwhile, I can do what I wanted triggering the popup by pressing a button, but still I would want to do things as I had planned as first.

balazsszekely

  • Guest
Re: How to change a control's parent
« Reply #7 on: October 02, 2018, 05:16:44 pm »
Just disable the memo while the popup form is visible.

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: How to change a control's parent
« Reply #8 on: October 02, 2018, 05:19:16 pm »
Nothing changes.

balazsszekely

  • Guest
Re: How to change a control's parent
« Reply #9 on: October 02, 2018, 05:22:35 pm »
Can you please attach a small demo application? Otherwise we just shot in the dark.

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: How to change a control's parent
« Reply #10 on: October 02, 2018, 05:37:30 pm »
This is the procedure I am currently using:
Code: Pascal  [Select][+][-]
  1. procedure TMarco.Memo1DblClick(Sender: TObject);
  2. begin
  3.  
  4.   Editor1.Memo1.Text:=Memo1.Text;
  5.  
  6.   Memo1.Enabled:=False;
  7.  
  8.   Editor1.ShowModal;
  9.  
  10.   Memo1.Text:=Editor1.Memo1.Text;
  11.  
  12.   Memo1.SelLength:=0;
  13.  
  14.   Memo1.Enabled:=True;
  15. end;  

Editor1 is simply a TForm containing a TMemo and nothing else.

The behaviour I describe happens when I copy the text from popup form to the original form. It did not happen when I changed Memo1's parent, although it led to other problems, as I said before.

balazsszekely

  • Guest
Re: How to change a control's parent
« Reply #11 on: October 02, 2018, 05:48:18 pm »
Quote
Editor1 is simply a TForm containing a TMemo and nothing else.
The behaviour I describe happens when I copy the text from popup form to the original form. It did not happen when I changed Memo1's parent, although it led to other problems, as I said before.
It works fine here. I'm unable to reproduce the bug.

carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: How to change a control's parent
« Reply #12 on: October 02, 2018, 06:05:36 pm »
It must be something related to gtk2. I have compiled it for win32 and seems to work under wine.

balazsszekely

  • Guest
Re: How to change a control's parent
« Reply #13 on: October 02, 2018, 06:17:34 pm »
@carlangas
As a workaround, drop a TTimer to your form, set interval to 50, enabled to false, then:

Code: Pascal  [Select][+][-]
  1. procedure TMarco.Memo1DblClick(Sender: TObject);
  2. begin    
  3.    Timer1.Enabled := True;
  4. end;  
  5.  
  6. procedure TForm1.Timer1Timer(Sender: TObject);
  7. begin
  8.   Timer1.Enabled := False;
  9.   Editor1.Memo1.Text:=Memo1.Text;
  10.   Memo1.Enabled:=False;
  11.   Editor1.ShowModal;
  12.   Memo1.Text:=Editor1.Memo1.Text;
  13.   Memo1.SelLength:=0;
  14.   Memo1.Enabled:=True;
  15. end;


carlangas

  • Jr. Member
  • **
  • Posts: 57
Re: How to change a control's parent
« Reply #14 on: October 02, 2018, 06:36:29 pm »
Great, that works, but what am I really doing with this? Why does it work? I had previously tried with Sleep(50), but it did nothing.

 

TinyPortal © 2005-2018