I'll make this my last post in this thread, but I'd love any further input. I hope it is some help to others later.
I'm learning--but that's the whole point, isn't it? I had been using "atoi" previously, but I thought there should be a better way to get an integer value out of the edit box. By defining it's value as an integer, it will only allow integer inputs, but I still had to pick up the contents as a string and then convert it.
The value associated with an edit box is only updated when the dialog is initialized and when the dialog is closed (look up "DoDataExchange"). That works fine for pop-up dialog boxes that set values within other programs, but for a dialog based app it isn't real useful to have the edit box contents never update the associated variable until you close the dialog.
Here's the story behind my self-imposed homework. My mother has just bought an oval mirror for her bathroom. The mounting brackets didn't give what weight they were recommended for, but gave the dimensions of a rectangle mirror they would support. My mother wanted to know if her oval mirror was too big. We want to compare the area of the mirrors--which should be a good approximation to comparing weights.
I have defined my own member variables for the values I need, and am setting them with " GetDlgItemInt" when the edit box contents change, instead of looking for the variable associated with the edit box. I then calculate the new area of the rectangle and call a function that writes out the results to my ouput edit box. The "SetWindowText" function in the output is causing an assertion error when I run the debug application. I'm sure there is a good reason, but I havn't figured it out yet.
I'm happy for now that the release version works. Here is the heart of the code, and I think enough for someone to figure out why I'm getting an assertion error. The error occurs in the "SetWindowText" call under "OutputArea".
Code:
class CMirrorDlg : public CDialog
{
// Construction
public:
CMirrorDlg(CWnd* pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CMirrorDlg)
enum { IDD = IDD_MIRROR_DIALOG };
CEdit m_RectAreaControl;
CEdit m_OvalAreaControl;
CSpinButtonCtrl m_spinRectWidth;
CSpinButtonCtrl m_spinRectHeight;
CSpinButtonCtrl m_spinOvalWidth;
CSpinButtonCtrl m_spinOvalHeight;
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMirrorDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
void OutputArea(void);
//Variables
int m_nOvalHeight;
int m_nOvalWidth;
int m_nOvalArea;
int m_nRectHeight;
int m_nRectWidth;
int m_nRectArea;
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CMirrorDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnChangeOvalHeight();
afx_msg void OnChangeOvalWidth();
afx_msg void OnChangeRectHeight();
afx_msg void OnChangeRectWidth();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
void CMirrorDlg::OutputArea(void)
{
CString chBuffer;
chBuffer.Format( "%d square inches", m_nOvalArea);
m_OvalAreaControl.SetWindowText(chBuffer);
chBuffer.Format( "%d square inches", m_nRectArea);
m_RectAreaControl.SetWindowText(chBuffer);
}
void CMirrorDlg::OnChangeOvalHeight()
{
// 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.
// TODO: Add your control notification handler code here
m_nOvalHeight = GetDlgItemInt(IDC_OvalHeight);
m_nOvalArea = int(3.14159 * (m_nOvalHeight/2.0) * (m_nOvalWidth/2.0));
OutputArea();
}
void CMirrorDlg::OnChangeOvalWidth()
{
// 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.
// TODO: Add your control notification handler code here
m_nOvalWidth = GetDlgItemInt(IDC_OvalWidth);
m_nOvalArea = int(3.14159 * (m_nOvalHeight/2.0) * (m_nOvalWidth/2.0));
OutputArea();
}
void CMirrorDlg::OnChangeRectHeight()
{
// 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.
// TODO: Add your control notification handler code here
m_nRectHeight = GetDlgItemInt(IDC_RectHeight);
m_nRectArea = int(m_nRectHeight * m_nRectWidth);
OutputArea();
}
void CMirrorDlg::OnChangeRectWidth()
{
// 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.
// TODO: Add your control notification handler code here
m_nRectWidth = GetDlgItemInt(IDC_RectWidth);
m_nRectArea = int(m_nRectHeight * m_nRectWidth);
OutputArea();
}