CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Join Date
    Mar 2002
    Location
    San Diego
    Posts
    39

    [RESOLVED] ListBox control

    I have a simple diaglog app (MFC) that is calcualting cost based on two listbox controls. However if one or both items are not selected I get an assertion error, "Expression:nLength>=0". I would like to prompt the user if he forgets to select an item in each list but I can't figure out how to do it. I tried to look at the CList member functions but don't know which one

    the code below is how I assign the item selected in the listbox to a CEdit control to show the user what he selected.
    m_Home.GetText(m_Home.GetCurSel(), m_Hsel);

    Any help would be appreciated!

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

    Re: ListBox control

    Check the value of GetCurSel before you try to use it for anything. Display an error if its value is LB_ERR (-1).

  3. #3
    Join Date
    Mar 2002
    Location
    San Diego
    Posts
    39

    Re: ListBox control

    if (m_Home.GetCurSel()== LB_ERR) {....}

    still getting same error but thanks for the advice anyways.

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

    Re: ListBox control

    Quote Originally Posted by Zouris View Post
    if (m_Home.GetCurSel()== LB_ERR) {....}

    still getting same error but thanks for the advice anyways.
    That doesn't really tell anybody anything. What's in the ... ? Based on the very limited information and code you showed, I gave you the correct solution. Any more help will require more input from you.

  5. #5
    Join Date
    Mar 2002
    Location
    San Diego
    Posts
    39

    Re: ListBox control

    The assertion error occurrs immediately. There is no cursor to get since I'm not selecting anything from the listBox. It seems like I need to initialize something in the OnInitDiag(Void).

    if (m_Home.GetCurSel()== LB_ERR) {AfxMessageBox("error");}

    I appreciate your help!

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

    Re: ListBox control

    I'm pretty sure I could help you more if you'd post relevant code, but for some reason you seem to be keeping the details to yourself, so good luck.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: ListBox control

    Did you ever read what that "Assertion failed..." wrote?
    Did you press the Retry button?
    Victor Nijegorodov

  8. #8
    Join Date
    Mar 2002
    Location
    San Diego
    Posts
    39

    Re: ListBox control

    Really there is not much more code then what I'm showing you?


    Bool CHomePrices2011Dlg::OnInitDialog;

    I add the following code
    ... Intialize the Clist selection (there are more selections I'm showing just one)
    m_Home.AddString(L"Baltimore");
    m_Away.AddString(L"Baltimore");

    // I create a simple structure and initialize values

    BAL.def=10;BAL.off=13;BAL.tot=BAL.def+BAL.off;

    return TRUE; // return TRUE unless you set the focus to a control

    ;


    void CHomePrices2011Dlg::OnBnClickedCompute()
    {


    double homeT awayT;
    homeT =0;
    awayT =0;

    m_Home.GetText(m_Home.GetCurSel(), m_Hsel);
    m_Away.GetText(m_Away.GetCurSel(), m_Asel);

    if (m_Hsel=="Baltimore"){homeT=BAL.def+BAL.off+BAL.tot-6;}

    if (m_Asel=="Baltimore"){awayT=BAL.def+BAL.off+BAL.tot;}



    m_cost = (homeT-awayT)/3000;

    UpdateData(FALSE);

    }

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: ListBox control

    Dear Zouris,
    being in the CG since Mar 2002 and having posted here 36 posts don't still know how to use Code tags?
    Besides, your manner of code writing is very bad for the people who would try to read/understand your code: why do you write more than one command/instruction/assignment in the same line?
    Why don't you provide the minimal necessary info about variable types you use in your code snippets?

    And finally: what does your last code snippets have to do with the error (assertion fault) you have?
    Victor Nijegorodov

  10. #10
    Join Date
    Mar 2002
    Location
    San Diego
    Posts
    39

    Re: ListBox control

    Please be professional and keep your negative comments to yourself. I'm not here to get your approval on my programming styles. Also, I did indicate what the assertion error was in my first post.

    My inclination is that I'm not initializing something correctly and I thought there was a simple command.

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

    Re: ListBox control

    Where are you testing GetCurSel for LB_ERR?

    You can't call GetText if GetCurSel is LB_ERR. From what you wrote in the first post that's your problem, and Victor is right. Please use code tags, and it's not in your best interests to get snippy with senior members here if you want continued help.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    Re: ListBox control

    Quote Originally Posted by Zouris View Post
    Please be professional and keep your negative comments to yourself. I'm not here to get your approval on my programming styles
    Well, if you really need help but your "programming styles" prevent people to read/understand your code/probllems then I will recommend you to change your styles. Seriously!

    Quote Originally Posted by Zouris View Post
    Also, I did indicate what the assertion error was in my first post.
    So again:
    Did you read all the text on the"Assertion failed..." dialog, something like:
    Debug Assertion Failed
    Program <...>.exe
    File <,,,>
    Line <,,,>
    Expression <...>.
    For info on how your program can cause an assertion
    failure, see the visual C++ documentation on asserts
    Press Retry to debug the application
    Did you press the Retry button?
    In what file did the assertion fail?
    Victor Nijegorodov

  13. #13
    Join Date
    May 2009
    Location
    Bengaluru, India
    Posts
    460

    Re: ListBox control

    makes sure you are selecting one of the item in the Listbox before calling :

    Code:
    m_Home.GetText(m_Home.GetCurSel(), m_Hsel);
    check if your m_Home.GetCurSel() is returning "-1" ( means none of the items are selected ) then just proceed further.. ; dont call GetText...

  14. #14
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: ListBox control

    if (m_Home.GetCurSel()== LB_ERR) {AfxMessageBox("error");}
    You must merely interrupt button handler in case of violated/unacceptable conditions.
    Code:
    void SomeDlgClass::OnSomeButton()
    {
        if (m_Home.GetCurSel()== LB_ERR) 
        {
            AfxMessageBox("error"); 
            return;
        }
    
    . . . // normal processing
    }
    Best regards,
    Igor

  15. #15
    Join Date
    Mar 2002
    Location
    San Diego
    Posts
    39

    Thumbs up Re: ListBox control

    Thank You Igor!!! That was exactly the problem.

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