void CcalculatorDlg::OnBnClickedButton1()
{
CString grabData = _T("");
m_display.GetLine(0,grabData.GetBuffer(10),10);
grabData += _T("1");
m_display.SetWindowTextW(grabData.GetBuffer());
grabData.ReleaseBuffer();
}
I am trying to make a basic calculator application using MFC, and I am having some trouble with the number inputs.
Above is the code for when the “1” button is pressed. I want it to read in what’s already being displayed in the display control, and then add a 1 onto the end of it like real calculators do. However I just can’t get it to work.
Basically the first button press it works and changes the blank display (edit control) to a 1. But then successive presses don’t continue to add 1’s, and I cannot figure out why.
I think the problem in your code is that you tried to modify the string (concatenating
_T("1")) after callingGetBuffer()but before callingReleaseBuffer(). Moreover, you have unbalancedGetBuffer()/ReleaseBuffer()calls.Assuming that
m_displayis aCEditinstance, you can try code like this (worked for me):If you have a multi-line edit control and you want to grab the first (top-most) line using
CEdit::GetLine(), you can use code like this (note that according to MSDN documentation,EM_GETLINEdoesn’tNUL-terminate the copied line, so you have to explicitly specify line length toReleaseBuffer()):