|
-
September 12th, 2011, 06:47 PM
#1
[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!
-
September 12th, 2011, 07:33 PM
#2
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).
-
September 13th, 2011, 12:55 PM
#3
Re: ListBox control
if (m_Home.GetCurSel()== LB_ERR) {....}
still getting same error but thanks for the advice anyways.
-
September 13th, 2011, 12:57 PM
#4
Re: ListBox control
 Originally Posted by Zouris
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.
-
September 13th, 2011, 02:34 PM
#5
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!
-
September 13th, 2011, 02:47 PM
#6
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.
-
September 13th, 2011, 02:59 PM
#7
Re: ListBox control
Did you ever read what that "Assertion failed..." wrote?
Did you press the Retry button?
Victor Nijegorodov
-
September 13th, 2011, 04:05 PM
#8
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);
}
-
September 13th, 2011, 04:24 PM
#9
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
-
September 13th, 2011, 06:48 PM
#10
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.
-
September 13th, 2011, 08:08 PM
#11
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.
-
September 14th, 2011, 01:08 AM
#12
Re: ListBox control
 Originally Posted by Zouris
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! 
 Originally Posted by Zouris
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
-
September 14th, 2011, 02:05 AM
#13
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...
-
September 14th, 2011, 02:20 AM
#14
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
-
September 14th, 2011, 11:11 AM
#15
Re: ListBox control
Thank You Igor!!! That was exactly the problem.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|