none
ListBox Edits C++ MFC | How can i edit a number inside of the list box. (Self Solved)

    Question

  • So i am making a calculator and I have all the key functions like adding numbers and multiply and subtract.

    But i'm making a history box at the bottom of my calculator and I have it some what working, when you enter in the first sum eg. "21" when press + it places the number into the history box (ListBox).

    No here is the part i need help with big time. I have created another Dialog window with an Edit Control box and a "Okay" Button. When i double click on the a sum ie. "21" it will open the Second dialog window but wont display the text into the Edit Control the way i would like to.

    With the code I have setup it will only display the first sum you have entered regardless no matter how many other sums you add to the List Box.

    I am Really stuck on this and need your help, I have done the research for the past 2 hours and nothing i find has helped me.

    Here is the code I am using when you double click on the sum in the History Box:

    void CCalcDlg::OnLbnDblclkList2() // Double Click
    {
    	CString str, str2;
    	int n;
    	for (int i = 0; i < HistoryBox->GetCount(); i++)
    	{
    		n = HistoryBox->GetTextLen(i);
    		HistoryBox->GetText(i, str.GetBuffer(n));
    		str.ReleaseBuffer();
    
    		str2.Format(_T("item %d: %s\r\n"), i, str.GetBuffer(n));
    		AFXDUMP(str2);
    	}
    	int Num = atoi(str);
    	CEditNumber EditNumberDlg(Num);
    	EditNumberDlg.DoModal();
    
    }

    What this does is gets the number you are clicking on and setting it to a string then i am setting that string to the Integer Num. From here Num called in a different class and then i use a SetDlgItemInt(IDC_EDIT_NUMBER, buff); 

    int buff = Num;

    The outcome i need from this is that when i click on any number in the list box it will set it to the Num and display it inside of the Edit Control in the other dialog.

    If i have not explained anything clearly please let me know, I am bad at wording things sorry.

    Thanks in advanced.

    -Scott





    • Edited by SD2020_ Wednesday, June 8, 2016 11:09 AM
    Wednesday, June 8, 2016 10:35 AM

Answers

All replies

  • Solution:

    void CCalcDlg::OnLbnDblclkList2() // Double Click
    {
    	CString str;
    	int index = HistoryBox->GetCurSel();
    	int Num = HistoryBox->GetCurSel();;
    	
    	Num = HistoryBox->GetTextLen(index);
    	HistoryBox->GetText(index, str.GetBuffer(Num));
    	str.ReleaseBuffer();
    
    	Num = atoi(str);
    	CEditNumber EditNumberDlg(Num);
    	EditNumberDlg.DoModal();
    }
    Not the neatest piece of work but it works.

    Wednesday, June 8, 2016 11:10 AM
  • Hi SD2020_,

    Thank you for your post.

    Glad to hear you have found a solution. And thanks for sharing us your result here.
    If you think your reply is helpful, please mark your reply as the answer, it will benefit to other community members who have the same issue. It will be easier for them to search the solution.

    Have a nice day:)

    Best Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.

    Click HERE to participate the survey.

    Thursday, June 9, 2016 6:08 AM
    Moderator