CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Oct 2005
    Posts
    27

    Finding value of control from another dialog

    Hi,

    I've created a listbox in a dialog, now when I select a button another dialog pops up which has textbox that I'd like to load the column text value into.

    The issue I'm having is accessing that listbox, so far I've created a static CString function on the first dialog class that I can access from second dialog class. However I'm not sure how to proceed in accessing the control.

    Any recommendations would be greatly appreciated,
    Regards Hayden

    Dialog 1 Header file
    Code:
     
    #if !defined(AFX_CHECKLCDEMODLG_H__8F9BBED5_8C3E_447A_ADF4_C4E47DCEC10D__INCLUDED_)
    #define AFX_CHECKLCDEMODLG_H__8F9BBED5_8C3E_447A_ADF4_C4E47DCEC10D__INCLUDED_
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    #include "LCZoomDlg.h"
    #include <string>
    /////////////////////////////////////////////////////////////////////////////
    // CCheckLCDemoDlg dialog
    class CCheckLCDemoDlg : public CDialog
    {
    // Construction
    public:
     CCheckLCDemoDlg(CWnd* pParent = NULL); // standard constructor
     static CString GetZoomLevels();
    // Dialog Data
     //{{AFX_DATA(CCheckLCDemoDlg)
     enum { IDD = IDD_CHECKLCDEMO_DIALOG };
     CCheckListCtrl m_listCtrl;
     //}}AFX_DATA
     
     // ClassWizard generated virtual function overrides
     //{{AFX_VIRTUAL(CCheckLCDemoDlg)
     protected:
     virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
     //}}AFX_VIRTUAL
    // Operations
    private:
     CImageList m_checkLCImgList;
    // Implementation
    protected:
     // Generated message map functions
     //{{AFX_MSG(CCheckLCDemoDlg)
     virtual BOOL OnInitDialog();
     afx_msg void EditZoomLevels();
     //}}AFX_MSG
     DECLARE_MESSAGE_MAP()
    };
    //{{AFX_INSERT_LOCATION}}
    // Microsoft Visual C++ will insert additional declarations immediately before the previous line.
    #endif
    Dialog 1 CPP file
    Code:
     
    CCheckLCDemoDlg::CCheckLCDemoDlg(CWnd* pParent /*=NULL*/)
     : CDialog(CCheckLCDemoDlg::IDD, pParent)
    {
     //{{AFX_DATA_INIT(CCheckLCDemoDlg)
      // NOTE: the ClassWizard will add member initialization here
     //}}AFX_DATA_INIT
    }
    void CCheckLCDemoDlg::DoDataExchange(CDataExchange* pDX)
    {
     CDialog::DoDataExchange(pDX);
     //{{AFX_DATA_MAP(CCheckLCDemoDlg)
      DDX_Control(pDX, IDC_LIST1, m_listCtrl);
     //}}AFX_DATA_MAP
    }
    BEGIN_MESSAGE_MAP(CCheckLCDemoDlg, CDialog)
     //{{AFX_MSG_MAP(CCheckLCDemoDlg)
     ON_BN_CLICKED(IDC_ZOOM, EditZoomLevels)
     ON_BN_CLICKED(IDC_LABEL, EditLabels)
     //}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    ///////////////////////////////////////////////////////
    // CCheckLCDemoDlg message handlers
    BOOL CCheckLCDemoDlg::OnInitDialog()
    {
     CDialog::OnInitDialog();
     return TRUE;
    }
    
    void CCheckLCDemoDlg::EditZoomLevels()
    {
    	CLCZoomDlg dlgZoom;
    	dlgZoom.DoModal();
    }
    
    CString CCheckLCDemoDlg::GetZoomLevels()
    {
    //***********IN HERE I WOULD LIKE TO ACCESS THE LIST CTRL SOMEHOW  ********
    return "some value";
    }
    And a small piece of Dialog 2 CPP file.
    Code:
     
    BOOL CLCZoomDlg::OnInitDialog()
    {
     CDialog::OnInitDialog();
     CString newtext = CCheckLCDemoDlg::GetZoomLevels();
     //Update textbox
     m_edit_zoom_low = newtext;
     UpdateData(FALSE);
     
     return TRUE;
    }
    Last edited by ill_comms; March 24th, 2009 at 02:13 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: Finding value of control from another dialog

    However I'm not sure how to proceed in accessing the control.
    Code:
    BOOL CLCZoomDlg::OnInitDialog()
    {
     CDialog::OnInitDialog();
     CString newtext = CCheckLCDemoDlg::GetZoomLevels();
     //Update textbox
     m_edit_zoom_low = newtext;
     UpdateData(FALSE);
     
     return TRUE;
    }
    Now what are you trying to do??

  3. #3
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    The main dialog has a list in it, when I select one the items and press the button it opens up another dialog "CLCZoomDlg".

    In this CLCZoomDlg dialog it has a textbox which gets populated with the text from selected list item, which you can then edit.

    In CLCZoomDlg once the Ok button is selected it will close and update the list item in the main dialog.

    That's why on initializing the CLCZoomDlg dialog I'm calling a method of the main dialog GetZoomLevels() to ***somehow*** get the list text. You may have a better idea!

  4. #4
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: Finding value of control from another dialog

    To achive that, you have several methods.
    1.) By using definition an object and get the pointer to access the listcontrol in dialog1, as you did.
    2.) Create a temporary file to store the datas(or contents) in listcontrol or textbox control, you can update and autoload the file contents there when you need.
    .....

    I think two methods can fulfill that.

  5. #5
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    I agree, it's the pointer that I'm having an issue with. I thought I'd do something like this:

    CCheckListCtrl* theList = CCheckLCDemoDlg::m_listCtrl;

    however this fails twice upon compile:
    1.) illegal reference to data member 'CCheckLCDemoDlg::m_listCtrl' in a static member function
    2.)'initializing' : cannot convert from 'class CCheckListCtrl CCheckLCDemoDlg::*' to 'class CCheckListCtrl *'

    CCheckListCtrl is valid, a derived of CListCtrl.

  6. #6
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: Finding value of control from another dialog

    Static member function only can reference to static member variable. You cannot access the static member in another class directly, the static member should be protected and accessed by member in the same class only. You may post a message to the class1 from class2, and invoke the static member indirectly.
    Last edited by sunny_sz; March 24th, 2009 at 03:34 AM.

  7. #7
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: Finding value of control from another dialog

    Source If you want to access member variables in your class from static functions, either the member variables must also be static, or you need an object to access them. This is because static functions have no 'this' argument.

    From MSDN:
    Member functions are either static or nonstatic. The behavior of static member functions differs from other member functions because static member functions have no implicit this argument.
    Nonstatic member functions have an implied argument, this, that points to the object through which the function is invoked. The type of this is type * const. These functions are considered to have class scope and can use class data and other member functions in the same class scope directly.

    Perhaps an easier way to think about this would be as follows:

    Code:
    void SomeFunction(CDlg *pDlg)
    {
    // This will not work because this function
    // isn't a member function of CDlg - it's just
    // a regular function. It's pretty obvious
    // why this doesn't work - and this is
    // basically what you were trying to do
    // originally.
    strcpy(m_szSomeString, "blah");
    
    // This will work because I'm accessing
    // the member variable through via a CDlg
    // object pointer
    strcpy(pDlg-&gt;m_szSomeString, "blah");
    }
    Hope it helps.

  8. #8
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    I'm a little unclear about the invoking indirectly and posting a message.

    So I call the CCheckLCDemoDlg::GetZoomLevels() from the CLCZoomDlg::OnInitDialog(), once I'm in GetZoomLevels I can begin to invoke the list indirectly


    Code:
     
    CString CCheckLCDemoDlg::GetZoomLevels()
    {
    CString theText = "";
     
    //Invoke somehow, find the item text here
    //........
    //load the text into variable 'theText and return it as a message??
    
     return theText;
    }

    I realise that this doesn't work, how do I invoke the member indirectly?

  9. #9
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    Whoops you double posted me, I'll check out the second for a while. Ignore the last post.

  10. #10
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    This is really confusing me, please bear with me.
    So back in the CLCZoomDlg::OnInitDialog() I create a dialog object.

    Code:
    CCheckLCDemoDlg *LCDemoDlg = new CCheckLCDemoDlg;
    from there I call getZoomLevels() with the object.

    Code:
    CCheckLCDemoDlg::GetZoomLevels(LCDemoDlg);
    in the getZoomLevels().

    Code:
     
    void CCheckLCDemoDlg::GetZoomLevels(CCheckLCDemoDlg *LCDemoDlg)
    {
    strcpy(LCDemoDlg.m_varInsidedialog1, "blah");
    }
    which sets a variable back in dialog 1.

  11. #11
    Join Date
    Jul 2005
    Location
    E: 120°.6, N: 31°.3′
    Posts
    795

    Re: Finding value of control from another dialog

    void CCheckLCDemoDlg::GetZoomLevels(CCheckLCDemoDlg *LCDemoDlg)
    {
    strcpy(LCDemoDlg->m_varInsidedialog1, "blah");
    }
    Please note the bold above.

  12. #12
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    Ok I just gave this a shot and it compiled but crashed. Am I still trying to access it directly, how do I know?

    void CCheckLCDemoDlg::GetZoomLevels(CCheckLCDemoDlg *LCDemoDlg)
    {
    CString theText = "";
    int countSel = LCDemoDlg->m_listCtrl.GetSelectedCount();
    theText.Format("Count = %d",countSel);
    AfxMessageBox(theText);
    }

  13. #13
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    Maybe on the other hand should I be setting up variables when I select a list item.

    E.g. when an item is clicked should I have a static variable "m_theListTextValue" that gets populated with the item text and then try to access that from the GetZoomLevels() with:
    CString theText = LCDemoDlg->m_theListTextValue;

    What do you think, is it a better approach?

  14. #14
    Join Date
    Oct 2005
    Posts
    27

    Re: Finding value of control from another dialog

    Ok still not working please help, I've tried setting up some public variables in the header of my list control. When I select an item it loads the text into the variables. On clicking the button, I create and initialize my new dialog and call GetZoomLevels(CCheckLCDemoDlg *LCDemoDlg) the following code:

    Code:
     
    void CCheckLCDemoDlg::GetZoomLevels(CCheckLCDemoDlg *LCDemoDlg)
    { 
     CString aLabel = LCDemoDlg->m_listCtrl.theText;
     AfxMessageBox(aLabel);
    }
    But it's coming back blank, no doubt I'm doing something wrong or going down the wrong path, maybe I should forget the variables altogether and do it a different way.

    Does someone have a decent tutorial that could help?

    As in the attached image, basically trying to get the text from the previous dialogs list sub item into the new dialog. At the moment I'm stuck at how to get the text.
    Attached Images Attached Images
    Last edited by ill_comms; March 25th, 2009 at 12:13 AM.

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