Stack Overflow on Edit Box
-
I have an Edit box on a dialog and check the input and arrange for it to be set to the Max or Min value if data entered is out of range. But I get a Stack Overflow error if say I enter say 999, the routine set the input to 180 which is OK. But I now enter -999 and the stack overflow problem occurs and I dont know why. The Event Routine on the Edit box is show below:- #define Value_MAX 180 #define Value_MIN -180
void CDataInput::OnEnChangeValue() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // Get the New Value string m_cValue.GetWindowText(m_strEditValue); // Convert from string to double (floating) m_fValue = atof(m_strEditValue.GetBuffer()); if(m_fValue > Value_MAX) { m_fValue = Value_MAX; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } else if(m_fValue <= Value_MIN) { m_fValue = Value_MIN; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } }
Also if I just enter -999 I get the Stack Overflow problem. Can someone explain what I am doing wrong. grahamfff -
I have an Edit box on a dialog and check the input and arrange for it to be set to the Max or Min value if data entered is out of range. But I get a Stack Overflow error if say I enter say 999, the routine set the input to 180 which is OK. But I now enter -999 and the stack overflow problem occurs and I dont know why. The Event Routine on the Edit box is show below:- #define Value_MAX 180 #define Value_MIN -180
void CDataInput::OnEnChangeValue() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // Get the New Value string m_cValue.GetWindowText(m_strEditValue); // Convert from string to double (floating) m_fValue = atof(m_strEditValue.GetBuffer()); if(m_fValue > Value_MAX) { m_fValue = Value_MAX; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } else if(m_fValue <= Value_MIN) { m_fValue = Value_MIN; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } }
Also if I just enter -999 I get the Stack Overflow problem. Can someone explain what I am doing wrong. grahamfffYour SetWindowText is generating another EN Change event, you never get out of the loop! You could maybe set yourself a monitor variable, like m_bInEnChange, and if that is TRUE, don't do your own processing again! You are probably also in trouble because you have the else if(m_fValue <= Value_MIN) which will always be true once you set value to Value_MIN, so you get into recursive loop.
-
I have an Edit box on a dialog and check the input and arrange for it to be set to the Max or Min value if data entered is out of range. But I get a Stack Overflow error if say I enter say 999, the routine set the input to 180 which is OK. But I now enter -999 and the stack overflow problem occurs and I dont know why. The Event Routine on the Edit box is show below:- #define Value_MAX 180 #define Value_MIN -180
void CDataInput::OnEnChangeValue() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // Get the New Value string m_cValue.GetWindowText(m_strEditValue); // Convert from string to double (floating) m_fValue = atof(m_strEditValue.GetBuffer()); if(m_fValue > Value_MAX) { m_fValue = Value_MAX; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } else if(m_fValue <= Value_MIN) { m_fValue = Value_MIN; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } }
Also if I just enter -999 I get the Stack Overflow problem. Can someone explain what I am doing wrong. grahamfffRather than validating on the EN_CHANGE notification, try using the EN_KILLFOCUS notification. With EN_CHANGE, your user will get very frustrated trying to enter a valid value. Every keypress, you will try and validate what they are entering. With EN_KILLFOCUS, you validate only when they leave the control (which implies they are ready to commit the value).
Software Zen:
delete this;
-
I have an Edit box on a dialog and check the input and arrange for it to be set to the Max or Min value if data entered is out of range. But I get a Stack Overflow error if say I enter say 999, the routine set the input to 180 which is OK. But I now enter -999 and the stack overflow problem occurs and I dont know why. The Event Routine on the Edit box is show below:- #define Value_MAX 180 #define Value_MIN -180
void CDataInput::OnEnChangeValue() { // TODO: If this is a RICHEDIT control, the control will not // send this notification unless you override the CDialog::OnInitDialog() // function and call CRichEditCtrl().SetEventMask() // with the ENM_CHANGE flag ORed into the mask. // Get the New Value string m_cValue.GetWindowText(m_strEditValue); // Convert from string to double (floating) m_fValue = atof(m_strEditValue.GetBuffer()); if(m_fValue > Value_MAX) { m_fValue = Value_MAX; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } else if(m_fValue <= Value_MIN) { m_fValue = Value_MIN; g_strNewDataStr.Format("%0.2f", m_fValue); m_cValue.SetWindowText(g_strNewDataStr); } }
Also if I just enter -999 I get the Stack Overflow problem. Can someone explain what I am doing wrong. grahamfffSomething else: You don't need to call
m_strEditValue.GetBuffer()
in youratof()
function call. Just passm_strEditValue
, and the built in cast to aLPCTSTR
will take care of the required conversion. Something else #2: In other circumstances, if you have to useGetBuffer()
with aCString
value, make sure you callReleaseBuffer()
. This ensures that theCString
manages the buffer memory correctly.
Software Zen:
delete this;
-
Something else: You don't need to call
m_strEditValue.GetBuffer()
in youratof()
function call. Just passm_strEditValue
, and the built in cast to aLPCTSTR
will take care of the required conversion. Something else #2: In other circumstances, if you have to useGetBuffer()
with aCString
value, make sure you callReleaseBuffer()
. This ensures that theCString
manages the buffer memory correctly.
Software Zen:
delete this;