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

    resource.h at runtime

    Point me in the right direction please.

    I would like to iterate through all the edit controls on a dialog. Something like this.

    CEdit *pEdit

    for( long lIDC_Value = 0; lIDC_Value <= Last Edit Control; LIDC_Value++)
    {
    pEdit = (CEdit*)GegDlgItem(lIDC_Value);
    }
    I know all the IDC values are defined in the
    resource.h, but I do not know how to get them at run time.

    Thank you.

    Phil McGahan
    (303) 252-8889
    12100 Elizabeth St.
    Thornton, Colorado 80241

  2. #2
    Join Date
    Apr 1999
    Posts
    48

    Re: resource.h at runtime

    There are several ways of doing this:

    You could ensure that all the edit controls have contiguous IDs in your resource.h and just code:

    for( long lIDC= IDC_FIRSTEDIT; lIDC<= IDC_LASTEDIT; lIDC++)
    {
    pEdit = (CEdit*)GetDlgItem(lIDC);
    }

    but that's asking for trouble if someone who doesn't realize the order changes them.
    You could set up a static array in your program cpp file.

    e.g.

    #include "stdafx.h"
    #include "resource.h"

    static UINT MyEditControls[]=
    {
    IDC_FIRSTEDIT,
    IDC_SECONDEDIT,
    IDC_THIRDEDIT,
    IDC_LASTEDIT
    };


    ...

    const int iLimit=sizeof(MyEditControls)/sizeof(MyEditControls[0]);

    for( long lIDC= 0; lIDC<iLimit; lIDC++)
    {
    pEdit = (CEdit*)GetDlgItem(MyEditControls[lIDC]);
    }
    which at least means they don't have to be kept contiguous. But you still have to update the table when you add or remove edit controls.

    Finally you could use GetTopWindow()/GetNextWindow() to iterate over the controls in the dialog at run time and pick out the ones that are Edit controls. That would adapt to changes to the dialog and be pretty much self-maintaining.
    You could always compare the classnames found against that obtained from something you know is a CEdit control.

    void CDummytDlg::OnButton1()
    {
    CWnd *pWnd=GetTopWindow();
    while(pWnd)
    {
    TCHAR sBUFFER[1024];
    VERIFY(GetClassName(pWnd->m_hWnd,sBUFFER,sizeof(sBUFFER)/sizeof(TCHAR)));
    {
    TRACE("found %s\n",sBUFFER);
    }
    pWnd=pWnd->GetNextWindow();
    }
    }


    Hope one of those solves it.
    Cheers,
    Roger




  3. #3
    Join Date
    Apr 1999
    Posts
    64

    Re: resource.h at runtime

    Thank you Roger. The GetTopWindow/GetNextWindow was a simple and elegant solution.

    Phil McGahan
    (303) 252-8889
    12100 Eli

  4. #4
    Join Date
    Apr 1999
    Posts
    48

    Re: resource.h at runtime

    No probs. Glad it helped.
    Method 3 is a bit slower than 2 but unless the dialog has many controls it's probably worth it, IMHO.




  5. #5
    Join Date
    Apr 1999
    Posts
    48

    Re: resource.h at runtime

    Actually, thinking a bit harder, GetDlgItem probably iterates over the controls anyway, so method 3 is probably faster too.




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