0
votes

I want to add two numbers in MFC with using CString variables. For example: CString m_nedit1, CString m_nedit2 and I need to store it in CString m_nedit3.

All three edit controls are value type(CString) not a control type

Please provide your answer.

2
It is not exactly clear what you are asking. Do you have two edit boxes in a dialog, and you want to retrieve the text from the dialogs as numeric values, and you want to add those numbers? - Joseph Willcoxson
Yes, I have three edit boxes and button in a dialog, when double click the button edit1 and edit 2 values should be calculated and displayed it on 3rd edit control.All three edit controls are value type(CString) not a control type. - gopa krishna
Are the numbers supposed to be integers or floating point? - Joseph Willcoxson
They should be value type of type int or doulbe but not of type CString. - Jabberwocky
I dont want define any int or float types,i want define only CString type and later on need to convert it to int type.. - gopa krishna

2 Answers

0
votes

This is a question on Dialog Data Exchange

On the button press you want to use UpdateData(TRUE) to set the CString control values m_nedit1 and m_nedit2.

Then you build your m_nedit3 string in whatever way you mean by "calculate". Then you synchronise the change back to the dialog controls with UpdateData(FALSE).

For example lets presume you meant concatenation:

UpdateData(TRUE);
m_nedit3 = m_nedit1 + m_nedit2;
UpdateData(FALSE);

You should handle the case UpdateData(FALSE) returning FALSE. This would mean the sync failed accordingly to any DDV conditions you may have imposed, e.g. maximum string length.

0
votes

While you can synchronize to an int type, using CString is possible. You need to convert to int add, then convert back to CString.

UpdateData();
int nAnswer = _ttoi(m_nedit1) + _ttoi(m_nedit2);
m_nedit3.Format(_T("%d"),nAnswer);
UpdateData(FALSE);