Insert TMemo on my Form ????

This is a discussion on Insert TMemo on my Form ???? within the C++ Programming forums, part of the General Programming Boards category; Using C++ Builder 4.0 I need to push a button and insert a Memo on my Form. Here is my ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    64

    Insert TMemo on my Form ? Help Please ? C++Builder 4.0

    Using C++ Builder 4.0

    I need to push a button and insert a Memo on my Form.
    Here is my code, what am I doing wrong...
    This doesn't work proper...

    void __fastcall TBagground::testbuttonClick(TObject *Sender)
    {
    TMemo *memo1 = new TMemo(memo1); ????
    memo1->Left = 100;
    memo1->Top = 500;

    }
    The same should I have done for some textstrings, but that should hopeful be done the same way...

    Hope someone can help me....

    tx a lot

    Gugge
    Last edited by Gugge; 08-05-2002 at 07:19 AM.
    !G!

  2. #2
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    You need to attach your TMemo object to the form.

    I.e.

    void __fastcall TBagground::testbuttonClick(TObject *Sender)
    {
    TMemo *memo1 = new TMemo(TBagground);


    memo1->Parent = TForm1;

    memo1->Left = 100;
    memo1->Top = 500;

    }


    Also, note your argument in the constructor was incorrect. For VCL objects, the constructor argument is the object which owns the object - The Owner. The Owner is the object responsible for destroying the owned object. This is different to the Parent. The Parent is the visual object on which the newly created object appears.

    When creating a new VCL object, you can pass it NULL if you prefer. Whatever you do, you need to ensure that you destroy the object explicitly. This is different to dragging & dropping object at design time, in which case the compiler ensures this is done by default.

    Also check object the Controls property of TControl objects.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    Hi tx for answering.

    It worked allmost.

    There are the only changes I made to your answer...

    void __fastcall TBagground::testbuttonClick(TObject *Sender)
    {
    TMemo *memo1 = new TMemo(Bagground); // not TBagground ??


    memo1->Parent = TForm1; // Bagground, not TForm1...

    memo1->Left = 100;
    memo1->Top = 500;

    }


    But that should be the same for all objects I need to insert in Runtime right ??

    Gugge

  4. #4
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Just ensure that you delete your new memo object, you will have a memory leak otherwise.

    From your code, memo1 goes out of scope. How do you plan to delete it?

    Every VCL object has a list of objects its owns, you could use this (I can't remember what the property is called from the top of my head). Also, every object has an Owner property - the value it was passed in the constructor. This is sometimes useful.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    64
    First:

    Couldnt' I also use this.

    void __fastcall TBagground::testbuttonClick(TObject *Sender)
    {
    TMemo *memo1 = new TMemo(this);
    memo1->Parent = this;

    memo1->Left = 100;
    memo1->Top = 500;
    }

    Second:
    Sorry, I missed something I think...:-)
    Why should I delete it again when I just created it.?

    Or in that case, should I just use;

    void __fastcall TBagground::deleteMemoClick(TObject *Sender)
    {
    TMemo *memo1 = delete TMemo(Bagground);
    }

    I like to have a delete button in case that I suddenly make to much.

    tx for info...

    Gugge
    Last edited by Gugge; 08-06-2002 at 10:44 AM.
    !G!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2009, 04:40 AM
  2. Calling datas from another form?
    By Aga^^ in forum C# Programming
    Replies: 2
    Last Post: 02-06-2009, 01:17 AM
  3. Accessing main form from functions in other classes
    By pj_martins in forum C++ Programming
    Replies: 1
    Last Post: 11-05-2004, 08:27 AM
  4. My UserControls dissapear off my form
    By zMan in forum C# Programming
    Replies: 2
    Last Post: 09-15-2004, 08:55 AM
  5. Making an MFC form to suit
    By TJJ in forum Windows Programming
    Replies: 1
    Last Post: 04-17-2004, 11:20 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21