CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Apr 2004
    Posts
    56

    Question Unable to display List Box

    Help, please....

    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 ? ...

  2. #2
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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.

    TDM

  3. #3
    Join Date
    Apr 2002
    Location
    Egypt
    Posts
    2,210
    can you send more code ?
    are you sure that you added this code after the CDialog::OnInitDialog(); line ?

  4. #4
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867

    Re: Unable to display List Box

    I suspect your problem might be here...

    Originally posted by nugroho2
    - When I put the above code in OnOK, the ListBox is populated for a fraction of seconds and soon disappear.
    By default, OnOK and OnCancel will close the dialog box. Is that what happened to you?

    If so, simple comment out the call to CDialog::OnOk()
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  5. #5
    Join Date
    Apr 2004
    Posts
    56
    Thanks to all of you.

    TDM, that may work, but I don't want to complicate the code with an additional button.

    Hspc, please find the code below.

    ==================================
    void dirlist ( CString NamaSamar);
    char fi [200][256];
    int filecount = 0;

    void CFinddata::OnOK()
    {
    // TODO: Add extra validation here

    AfxMessageBox ("Enter OK 2"); // test List box content

    CFinddata dlg;
    dlg.DoModal();

    CDialog::OnOK();
    }

    ///////////////////////

    #include "io.h"

    void dirlist ( CString NameTmp)
    {
    struct _finddata_t c_file;
    long hFile ;
    int done = 1, i=0;

    filecount = 0;

    if ( (hFile = _findfirst( NameTmp, &c_file )) != -1L )
    done = 0;

    while ( done ==0 )
    {
    strcpy ( fi[i++] , c_file.name );
    done = _findnext (hFile, &c_file) ;
    filecount ++;
    }

    _findclose( hFile );
    }

    ////////////////////

    BOOL CFinddata::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here

    /* GetDlgItemText (IDC_EDIT1, m_Names);

    dirlist (m_Names);

    int i = 0;
    while (i < filecount)
    {
    m_List.AddString ( fi[i++]);
    }

    m_List.SetCurSel (3);
    */
    return TRUE; // return TRUE unless ...
    // EXCEPTION: OCX Property Pages should
    }

    /////////////

    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.

    Guys, thanks for you kind attention.

  6. #6
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    Could you clarify a couple of things? Firstly, ignoring the fact that these lines are commented out for the moment:-

    BOOL CFinddata::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here

    GetDlgItemText (IDC_EDIT1, m_Names);

    dirlist (m_Names);

    . . . // rest of function

    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

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637
    Originally posted by John E
    Could you clarify a couple of things? Firstly, ignoring the fact that these lines are commented out for the moment:-

    BOOL CFinddata::OnInitDialog()
    {
    CDialog::OnInitDialog();

    // TODO: Add extra initialization here

    GetDlgItemText (IDC_EDIT1, m_Names);

    dirlist (m_Names);

    . . . // rest of function

    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.

  8. #8
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    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

  9. #9
    Join Date
    Apr 2004
    Posts
    56
    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.

  10. #10
    Join Date
    Feb 2002
    Posts
    3,788
    can you post the project?

  11. #11
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    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

  12. #12
    Join Date
    Aug 2001
    Location
    Germany
    Posts
    1,384
    I dont understand what do u mean by this
    Code:
    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.

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Unable to display List Box

    Originally posted by nugroho2
    Help, please....

    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.

  14. #14
    Join Date
    Apr 2004
    Posts
    56
    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 ?
    Attached Files Attached Files

  15. #15
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,867
    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

Page 1 of 2 12 LastLast

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