I'm still learning C++ MFC and my question is: how can I get the CurSel
of 2 listbox as the number, then add them and print the result?
The result displays a weird 659998896 number instead of 2. What have I done wrong here?
BOOL CEngravingCalculatorDlg::OnInitDialog(){
CListBox* emplistbox1 = static_cast<CListBox*>(GetDlgItem(IDC_LIST1));
emplistbox1->AddString(L"1");
emplistbox1->SetCurSel(0);
CListBox* emplistbox3 = static_cast<CListBox*>(GetDlgItem(IDC_LIST3));
emplistbox3->AddString(L"1");
emplistbox3->SetCurSel(0);
}
void CEngravingCalculatorDlg::OnBnClickedButton1()
{
CListBox* emplistbox1 = static_cast<CListBox*>(GetDlgItem(IDC_LIST1));
CListBox* emplistbox3 = static_cast<CListBox*>(GetDlgItem(IDC_LIST3));
int num1 = emplistbox1->GetCurSel();
int num2 = emplistbox3->GetCurSel();
int equal = num1 + num2;
CString str;
str.Format(_T("%d", equal));
GetDlgItem(IDC_Display)->SetWindowTextW(str);
}
_T
is a macro that takes only a single parameter. Because of a typo, you've given it two parameters. The second one is ignored and effectively lost. This means yourformat
call is only getting a single parameter, and you're seeing undefined behavior. Fix it with:P.S. This means your error had absolutely nothing to do with the listbox.