CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2009
    Posts
    84

    [MFC] Event handler in a class without tool controls

    Hello everyone!!!!

    I have a class with an external structure like:

    Code:
    struct MY_STRUCT {
           int m_variable1;
           int m_variable2;
           bool m_variable3;
           MY_STRUCT() : m_variable1(0), m_variable2(0), m_variable3(FALSE) {}
    };
    
    class my_own_class
    {
    public:
           my_own_class() { IsSettled = FALSE };
           ~my_own_class();
    public:
           MY_STRUCT c_struct;
    
           bool IsSettled;
    }
    I'm looking for an event handler in the class my_own_class if possibile which would fire when a variable in the struct is changed so the event would check if for example both int variables are validated as != 0 so I can change IsSettled variable to TRUE... it is possible? If yes, how I could do that in MFC?...

    Thanks to all in advance,
    Ciao
    Luigi

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    Re: [MFC] Event handler in a class without tool controls

    Make c_struct member private, and allow access to its members using Get/Set my_own_class methods. Inside of Set methods you can implement your logic for IsSettled value.
    Another way is to make all MY_STRUCT members private, and write Get/Set methods for these members in MY_STRUCT structure. When some variable is changed in a Set method, structure can notify all observers interesting in this event - see Observer Design Pattern.
    In any case, notification is possible only for private members, which are changed using Set method. Without this, you cannot know when structure member is changed.

  3. #3
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: [MFC] Event handler in a class without tool controls

    Hi,

    To fire an event would be very complicated (although it is possible in some way by watching the contents of your variables in an idle or timer function or so).

    The much better c++ way is to protect your variables so that no one can directly change them. Instead implement a Set function for each variable (public). And in this Set function you can handle all things. It is exactly "fired" when the variable is changed.

    Code:
    class CExample
    {
      public:
      CExample(){m_iVar1=0;};
    
      BOOL SetI(int i);
    protected:
      int m_iVar1;
    };
    
    BOOL CExample::SetI(int i)
    {
      if ( ... ) // check here if the new i is correct ...
      {
        m_iBar1 = i;
        return TRUE;
      }
      return FALSE;
    }
    
    int main()
    {
      CExample MyInstance;
    
      MyInstance.m_iVar1 = 12; // compile time error; var could not be changed from outside
      MyInstance.SetI(898); // correct and handled
    
      return 0;
    }
    Edit: Alex has been faster...

    With regards
    Programartist

  4. #4
    Join Date
    Aug 2009
    Posts
    84

    Re: [MFC] Event handler in a class without tool controls

    Uhm.... thanks to both you but I think this solution is good for single independent variables...

    In my case indeed lets suppose to have:

    Code:
    struct MY_STRUCT {
           int m_variable1;
           int m_variable2;
           int m_variable3;
           int m_variable4;
           .
           .
           .
           int m_variable12;
           CStringA m_variable13;
           .
           .
           .
           CStringA m_variable22;
           bool m_variable23;
           bool m_variable24;
           .
           .
           .
           bool m_variable32;
           MY_STRUCT() : m_variable1(0), m_variable2(0),... 
           m_variable23(FALSE),...m_variable32(FALSE) {}
    };
    
    class CExample
    {
    private:
          MY_STRUCT cStructExample;
    public:
          CExample() { IsSettled = FALSE };
          ~CExample();
    
          bool IsSettled;
    
          void SetSettingValue(MY_STRUCT pStructExample, int value_to_set);
          void SetSettingValue(MY_STRUCT pStructExample, bool state_to_set);
          void SetSettingValue(MY_STRUCT pStructExample, CStringA string_to_set);
    
          EVENT_LIKE_EACH_TIME_M_VARIABLE_IS_SET void CheckValues() 
          {
              if (cStructExample.m_variable3 > 0 && cStructExample.m_variable6 > 0 && 
                  !cStructExample.m_variable14.IsEmpty() && !cStructExample.m_variable16.IsEmpty() &&
                  cStructExample.m_variable24 == TRUE && cStructExample.m_variable27 == TRUE &&
                  cStructExample.m_variable28 == TRUE && cStructExample.m_variable31 == TRUE)
                 IsSettled = TRUE;
              else 
                 IsSettled = FALSE;
          }
    };
    
    int main()
    {
        CExample MyInstanceExample;
    
        ....
    }
    
    void CSomeClassDialog::OnBnClickedChkSomeCheckControl(CExample pExampleInstance)
    {
           pExampleInstance.m_variable24 = TRUE;
    }
    
    void CSomeClassDialog::OnBnClickedBnSomeButtonControl(CExample pExampleInstance)
    {
         if (pExampleInstance.IsSettled)
            StartSomeElaborationFunction();
        else
            AfxGetMainWnd()->MessageBox(_T("blah blah blah"), _T("blah"), MB_ICONSTOP|MB_OK);
    }
    In this way it would be more readable and simpler because that function event would check always each time a m_variablexx is changed... so isn't there a solution similar?....

  5. #5
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: [MFC] Event handler in a class without tool controls

    Quote Originally Posted by npuleio View Post
    Uhm.... thanks to both you but I think this solution is good for single independent variables...

    In my case indeed lets suppose to have:

    Code:
    struct MY_STRUCT {
           int m_variable1;
           int m_variable2;
           int m_variable3;
           int m_variable4;
           .
           .
           .
           int m_variable12;
           CStringA m_variable13;
           .
           .
           .
           CStringA m_variable22;
           bool m_variable23;
           bool m_variable24;
           .
           .
           .
           bool m_variable32;
           MY_STRUCT() : m_variable1(0), m_variable2(0),... 
           m_variable23(FALSE),...m_variable32(FALSE) {}
    };
    ...

    In this way it would be more readable and simpler because that function event would check always each time a m_variablexx is changed... so isn't there a solution similar?....
    Of course there is:

    If you have a big count of variables (numbered as in your example) then change them into an array, make the array protected and implement a setting function (and a getting one too) taking the index and the value.

    With regards
    Programartist
    Last edited by ProgramArtist; January 19th, 2010 at 06:58 AM. Reason: Edit: Typos

  6. #6
    Join Date
    May 2006
    Location
    Dresden, Germany
    Posts
    458

    Re: [MFC] Event handler in a class without tool controls

    Hi,

    Regarding your question I want to add:

    There is no way to fire an event automatically exactly at the time a certain variable has been changed. The right solution for this is the c++ way shown by Alex and me.

    The MFC toolbars and statusbars do not work this way: They check the status of certain variables from time to time (exactly when no messages are in the message queue). This could be a possible approach too for your task (since we do not know what exactly you're trying to achieve).
    To use this approach you should overwrite your app's OnIdle function (reference and (at least one) article you'll find in the MSDN).
    But remember the difference: The OnIdle function will try everytime it is called if one of your variables has been changed. Our approach check the contents of the variable exactly when it is going to be changed.

    And if someone wants to add it: Of course a hard-implementation of memory watching (like the debugger is able to) is possible. But IMHO it is far too complicated for this problem.

    With regards
    Programartist

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured