I am a newbie in C++. In an MFC dialog, I put an Edit control and a List box. I want to populate the List box (m_List) with strings derived from the Edit box string.
- in the dialog's OnInitDialog, I tried to put
GetDlgItemText (..) // get text from Edit box
process the string //
m_List.AddString (..) // etc. several strings
m_List.SetCurSel (..) //
but GetDlgItemText could not work from OnInitDialog.
- When I put the above code in OnOK, the ListBox is populated for a fraction of seconds and soon disappear.
I have read the MSDN library, and tried other events (OnKillFocus, etc.) but could not get the dialog to stop to display the strings in the List Box. What should I do ? ...
One method might be to greate a button on the dialog. Then after you type the string you want in the edit box, press the button to add the string to the list box. Create a handler for the button press. Then inside the button press handler get the edit text and then add the text to the list box.
void CFinddata::OnKillfocusEdit1()
{
// TODO: Add your control notification handler code here
GetDlgItemText (IDC_EDIT1, m_Names);
dirlist (m_Names);
int i = 0;
while (i < filecount)
{
m_List.AddString ( fi[i++]);
}
m_List.SetCurSel (3);
}
====================================
The code in OnInitDialog fails. . . and I moved it to OnKillFocusEdit1, which succeeds to show the list for 1 second ) and disappears.
John, you may have seen that I tried to stop the program at OnOK. When I delete the domodal () in OnOK, the result is the same.
OnInitDialog() gets called after the dialog's controls have been constructed but before they've been initialised. Therefore, unless you've specifically initialised IDC_EDIT1 in the resource compiler, I'd assume that IDC_EDIT1 would always be empty at that point. Is this something you're aware of? Your call to 'dirlist(m_Names);' would probably be meaningless at this point if 'm_Names' is empty. This could give rise to another problem....
m_List.SetCurSel (3);
which appears slightly further down the code in the same function. What is supposed to happen if there are less than 3 items in the list box?
Finally, how is 'm_Names' defined? Is it a CString, a pointer, an array of characters or what?
"A problem well stated is a problem half solved.” - Charles F. Kettering
OnInitDialog() gets called after the dialog's controls have been constructed but before they've been initialised. Therefore, unless you've specifically initialised IDC_EDIT1 in the resource compiler, I'd assume that IDC_EDIT1 would always be empty at that point. Is this something you're aware of? Your call to 'dirlist(m_Names);' would probably be meaningless at this point if 'm_Names' is empty. This could give rise to another problem....
m_List.SetCurSel (3);
which appears slightly further down the code in the same function. What is supposed to happen if there are less than 3 items in the list box?
Finally, how is 'm_Names' defined? Is it a CString, a pointer, an array of characters or what?
THe controls are all usable after the call to the base class OnInitDialog. Of concern to me in this application is the creation and call to another dialog of the same class in OnOK(); That can't be doing whatever the OP is intending.
Originally posted by GCDEF
THe controls are all usable after the call to the base class OnInitDialog.
Yes, I agree. I'm just questioning whether or not they'd have any meaningful data entered at such an early stage in the program....
Originally posted by GCDEF
Of concern to me in this application is the creation and call to another dialog of the same class in OnOK(); That can't be doing whatever the OP is intending.
Can't argue with that. I'm not sure if it's the cause of the problem but it does seem very strange.
"A problem well stated is a problem half solved.” - Charles F. Kettering
John, I have tried to move the OnInitDialog() further down below, and it did not work. On the SetCurSel (), this is just a test, and I know there should be a lot of strings (file names) in the List box.
GCDEF, the application won't stop to display the List even after I delete the domodal() in OnOK. But I know that there is the list there. It is displayed if I activate the AfxMessageBox(). But it is destroyed as soon as I press enter on the AFxMessageBox.
As GCDEF explained, I think you're trying to do the right thing but in the wrong place. Instantiating another CFinddata dialog when OK is pressed surely can't be what you actually want?
What are you actually expecting to happen...?
"A problem well stated is a problem half solved.” - Charles F. Kettering
void CFinddata::OnOK()
{
// TODO: Add extra validation here
AfxMessageBox ("Enter OK 2"); // test List box content
CFinddata dlg;
dlg.DoModal();
CDialog::OnOK();
}
It seems that clicking OK will create another CFinddata. Seems to me like inifinite loop here if u keep clicking OK.
Is that ur intention????:confused:
Regards,
Usman.
I am a newbie in C++. In an MFC dialog, I put an Edit control and a List box. I want to populate the List box (m_List) with strings derived from the Edit box string.
- in the dialog's OnInitDialog, I tried to put
GetDlgItemText (..) // get text from Edit box
process the string //
m_List.AddString (..) // etc. several strings
m_List.SetCurSel (..) //
but GetDlgItemText could not work from OnInitDialog.
- When I put the above code in OnOK, the ListBox is populated for a fraction of seconds and soon disappear.
I have read the MSDN library, and tried other events (OnKillFocus, etc.) but could not get the dialog to stop to display the strings in the List Box. What should I do ? ...
Okay, let's stop to catch our breath here. You want to put strings in the list based on what's in an edit control. Nobody has had a chance to put anything in the edit control at that point (as John said) so there is nothing to be gained by putting it there.
OnOK's default behavior is to close the dialog, so that probably isn't the place either, as the dialog will close if you call the base class implementation. What action taken by the user would indicate that it's time to populate the list box? Somebody else suggested a button, which is probably a good idea.
Guys, thanks. I am just trying to revive the wild cards in the old DOS commands. I like it very much. Please see my code attached.
I am a newbie in Visual C++ and have not had much idea of the complicated nature of the details. It seems that the event of the PRESS ENTER key in an Edit box is captured in OnOK. When I press TAB, all is OK and the apps work alright.
OnCommand and OnUpdateEdit1 are better choices than OnKillFocusEdit1 as the files will be displayed immediately. I have just to figure out the way to handle the PRESS ENTER KEY in the Edit box, to shift the focus to other control when ENTER is pressed. Any idea ?
Ah.... this is slightly different from your first description. What you mean is that you don't want the dialog to close when the user presses ENTER. Is that right?
"A problem well stated is a problem half solved.” - Charles F. Kettering
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.