CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2004
    Posts
    5

    Question How can I test an Object to see if it already exists?

    REF : Visual C++ 6

    Hello

    I have created a dialog and it's class;

    I created a member variable of the created dialog class in the main dialog;

    CGraphDlg m_dlgGraph;

    Later in the Main dialog code I create the second dialog and show it.

    void CSmithGraphDlg::OnBopenGraph()
    {
    // TODO: Add your control notification handler code here

    m_dlgGraph.Create(IDD_SGRAPH, this);
    m_dlgGraph.ShowWindow(SW_SHOW);
    }

    Is there a way to test 'm_dlgGraph' variable, before I try and create the dialog object to see if is already attached to an existing dialog object?

    if there is a method for this, will it work with other objects ?
    thanks

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360
    You can test m_hWnd to see if it's valid or not.

    Also, you can declare your variable dinamically:

    Code:
    CGraphDlg *m_dlgGraph = NULL;
    and test it for NULL:

    Code:
    void CSmithGraphDlg::OnBopenGraph() 
    {
      // TODO: Add your control notification handler code here
      if(m_dlgGraph == NULL)
         m_dlgGraph = new CGraphDlg();
      m_dlgGraph->.Create(IDD_SGRAPH, this);
      m_dlgGraph->ShowWindow(SW_SHOW);
    }
    This way you can delete ar recreate it over an over:

    Code:
    void CSmithGraphDlg::OnBdeleteGraph() 
    {
      // TODO: Add your control notification handler code here
      if(m_dlgGraph != NULL)
      {
         delete m_dlgGraph;
         m_dlgGraph = NULL;
      }
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Sep 1999
    Location
    Colorado, USA
    Posts
    1,002
    You can test whether there is another CGraphDlg active in the app by putting a static member in the CGraphDlg class.
    DWORD m_dwCount;
    This variable is shared by all objects of the CGraphDlg class because it is static. Initialize it at the beginning of the .cpp file:
    DWORD CGraphDlg::m_dwCount = 0;

    In the constructor of CGraphDlg:
    m_dwCount++;

    In the destructor of CGraphDlg:
    m_dwCount--;

    You could then:
    CGraphDlg dlg; //Create object
    if (dlg.m_dwCount > 1)
    //There is already another object of the dlg class

    Steve

  4. #4
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652

    Re: How can I test an Object to see if it already exists?

    Originally posted by hickock6
    Is there a way to test 'm_dlgGraph' variable, before I try and create the dialog object to see if is already attached to an existing dialog object?
    Code:
    if(!m_dlgGraph.GetSafeHwnd())
      m_dlgGraph.Create(IDD_SGRAPH, this);
    
    m_dlgGraph.ShowWindow(SW_SHOW);
    Originally posted by hickock6
    if there is a method for this, will it work with other objects ?
    It will work with all 'CWnd' derived ones...

  5. #5
    Join Date
    Jun 2004
    Posts
    5

    Wink

    Thanks to all the answers :

    Will let you know how it works out..
    Agian thanks for the help

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