C++Builder Parent & child events

This is a discussion on C++Builder Parent & child events within the Windows Programming forums, part of the Platform Specific Boards category; Dear all, I'm developing a component called TBitmapStringGrid. Inside that component, I declare another component in this way: Code: void ...

  1. #1
    Registered User
    Join Date
    Aug 2003
    Posts
    6

    C++Builder Parent & child events

    Dear all,

    I'm developing a component called TBitmapStringGrid. Inside that component, I declare another component in this way:

    Code:
    void TBitmapStringGrid::Init()
    {
      TListBox* ListBox;
      ListBox= new TListBox(this);
      ListBox->Parent=this; 
    }
    The TListBox component is created inside one of the cells in the grid. The problem is that I need that the TBitmapStringGrid detects when an user clicks into the TlistBox. The OnClick event generated falls into TListBox, and not into TBitmapStringGrid, so TBitmapStringGrid never knows if someone has clicked into a cell.

    I tried to call a TBitmapStringGrid's member function from the TListBox, overriding the MouseDown event as follows:

    Code:
    void __fastcall TListBox::MouseDown(TMouseButton Button, TShiftState Shift, int x, int y)
    {
      AnsiString aux;
      aux=Parent->ClassName();
      if (aux == "TBitmapStringGrid")
      {
        (TBitmapStringGrid *)(Parent->MyOwnFunction());
      }
    
    }
    But it tells me that MyOwnFunction is not a member of TWinControl. I'd like to know if something like this is possible to do.

    What I want to do is to execute one TBitmapStringGrid's class member function when someone clicks in the child component.

    thanks and regards,
    Artelo.

  2. #2
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think you need to declare the class member function you want to call as static. This is because the TListBox function does not have the this pointer to the TBitmapStringGrid instance. The calling conventions of non-static member functions require that the pointer be passed. So, make the member function static, and pass a pointer to the instance manually.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  3. #3
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    It may look something like this:

    Code:
    class TBitmapStringGrid
    {
    public:
    static void MyProcedure(TBitmapStringGrid *pInstance);
    void foo() {return; }
    };
    
    void TBitmapStringGrid::MyProcedure()
    {
    pInstance->foo; //this is the only way to access the member data/functions
    }
    
    class TListBox
    {
    public:
    LRESULT CALLBACK OnClick();
    TBitmapStringGrid *parent;
    };
    
    LRESULT CALLBACK TListBox::OnClick()
    {
    TBitmapStringGrid::MyProcedure(parent);
    }
    See what I mean?
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  4. #4
    Registered User
    Join Date
    Jun 2003
    Posts
    245
    Parent points to a TWinControl, so you'll have to cast it if you wish to use any of your own methods.

  5. #5
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    bennyandthejets,

    Many thanks for your response. I tried to implement something like that, but I found that I can't access to any member function of TBitmapStringGrid. All of the data members have random values. May be that is neccesary to "connect" TBitmapStringGrid::MyProcedure(parent) with the real parent?

    I need to access some global class variables inside TBitmapStringGrid::MyProcedure, but of course, I don't controll it, so I can't make it static.

    And, why would be needed to do something like
    pInstance->foo;? I tried it, and put the foo data dirtectly inside the MyProcedure has the same effect.

    I don't need to access to another TBitmapStringGrid object to get access to one function; I need to acces to the TBitmapStringGrid object wich created the TListBox object, so I can do some checks with it. And with the approach that you say, i'm not able to find how to do that.

    May be I didn't understand what you mean, but making the function static only lets me to access to that static function, not to the object itself to get some values from it.

    Thanks and regards,
    Artelo.

  6. #6
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    _Elixia_,

    As you can see, I tried to cast the function with

    Code:
    TBitmapStringGrid *)(Parent->MyOwnFunction());
    but when i do that, is when it says that MyOwnFunction is not a member of TWinControl.

    I though that I could access to the this pointer of the TBitmapStringGrid, wich i supposed that can be find in ListBox->Parent. But it seems that something is wrong.

    Regards,
    Artelo.

  7. #7
    Registered User
    Join Date
    Aug 2003
    Posts
    6
    It's solved!!

    It was only neccesary to modify some little things in the code to get it working. Now I can access to the function members in a very easy way:

    Code:
    class TBitmapStringGrid
    {
    public:
      static void MyProcedure(void *this_TBitmapStringGrid);
      void foo(){};
    
    };
    
    void TBitmapStringGrid::MyProcedure(void *this_TBitmapStringGrid)
    {
    
      TBitmapStringGrid *objaux=(TBitmapStringGrid *)this_TBitmapStringGrid;
      //now, I can do whatever with objaux, because its the parent object itself:
      objaux->foo();
    }
    
    class TListBox
    {
     public:
      LRESULT CALLBACK OnClick();
    };
    
    LRESULT CALLBACK TListBox::OnClick()
    {
      //Parent is the member pointer wich points to the TBitmapStringGrid parent
      TBitmapStringGrid::MyProcedure(Parent); 
    }
    Lots of thanks for your help!! without your code examples I'd never imagine how to solve it.

    Artelo.

  8. #8
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    No problem.

    Static member functions used to really confuse me, but if you think about what is happening under the hood, it becomes simple.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. inter process communcation, parent - child
    By tallan in forum C Programming
    Replies: 5
    Last Post: 02-28-2009, 03:04 AM
  2. Replies: 3
    Last Post: 10-15-2008, 09:24 AM
  3. process programming
    By St0rM-MaN in forum Linux Programming
    Replies: 2
    Last Post: 09-15-2007, 07:53 AM
  4. pipe
    By smart girl in forum C Programming
    Replies: 4
    Last Post: 04-30-2006, 09:17 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM

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