Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
In OnInitDialog I have a for cycle that fills edits that are in dialog..but I would like that , for each element of the for loop, dialog is update..how Can I do?

What I have tried:

I tried to introduce a "sleep" in for cycle but dialog is shown in the last of for cycle
Posted
Updated 25-Jul-23 5:24am
v2
Comments
Richard MacCutchan 25-Jul-23 9:09am    
Your question is not clear. What do you mean by ",every time, dialog is shown with new element in edit."?
You need to expalin what these "edits" are, and what you are filling them with. Please use the Improve question link above, and add complete details.
Member 14594285 25-Jul-23 9:46am    
I improved question, is it clear now?
Richard MacCutchan 25-Jul-23 9:50am    
No, it is exactly the same. Show us your code and explain what you are doing in detail.
[no name] 25-Jul-23 9:48am    
When your application receives the WM_CREATE message the window has not been created yet. The child edit boxes have also not been created so you cannot set the text.

You could handle the WM_CTLCOLOREDIT message to set the text on your edit boxes. The lParam will be the window handle. You can use GetDlgItem function to match the numeric control identifier.

https://learn.microsoft.com/en-us/windows/win32/controls/wm-ctlcoloredit

Update: Corrected window message
Richard MacCutchan 25-Jul-23 9:56am    
I have an application that modifies a dialog's text controls in the OnInitDialog method.

1 solution

As was mentioned, your question is not clear. I work with MFC a lot and, if I understand you, this is an easy thing to do. First, I almost always make control objects where I declare the objects as members of the dialog class and then make a DDX_CONTROL entry for each one in the dialog's DoDataExchange method. If you have a lot of them then you can make an array of the CEdit controls. Here's an example :
C++
class CMyDialog : public CDialog
{
public:
    static const int FirstEdit = IDC_FIRST_EDIT_ID;
    static const int EditCount = 6;
    CEdit m_EditControls[ EditCount ];

    // more declarations here

public:
    void UpdateEditControls();
};

void CMyDialog::DoDataExchange( CDataExchange* pDX )
{
	__super::DoDataExchange( pDX );

    for( int n = 0; n < EditCount; ++n )
        DDX_Control( pDX, FirstEdit + n, m_EditControls[ n ] );
}

void CMyDialog::UpdateEditControls()
{
    for( int n = 0; n < EditCount; ++n )
        m_EditControls[ n ].SetWindowText( m_EditValueString[ n ] );
}
The last method there, UpdateEditControls, is used to update the text in each of the edit controls. If those values are held in integers then that code needs to be revised accordingly.

Note that this requires the identifiers of each control to be consecutive values or it will not work correctly. Those are usually in the file Resource.h which is a just a text file so you can set them to be what ever you want them to be.

Here's a little helper function that can be useful.
C++
void SetDialogInt( CWnd & control, int value )
{
    char buffer[ 64 ];
    sprintf( buffer, "%d", value );
    control.SetWindowText( buffer );
};
If your data values are integers you can call this function to set the text of the corresponding controls. If they are a different data type then you can make a different helper function to handle that type.
 
Share this answer
 
Comments
Member 14594285 26-Jul-23 8:59am    
but where can be called function "UpdateEditControls();"?
Rick York 26-Jul-23 11:05am    
Where ever you want to update the edit controls. Usually it's done in OnInitDialog but if those values are changing constantly then you can also do it in OnTimer or when you get a notification that they have changed. That's the point though - do it when ever you need to and where ever it has to be done.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900