MFC edit boxes and integers

I'm trying to make an edit box that takes int variables only, and I've used the MFC class wizard in VC++ 6.0 Enterprise to make the member variable an int called "m_EditOne" with control "m_EditOneControl"

I tried extracting the integer using the OnChange event with the command:
m_EditOneControl.GetDlgItemInt(m_EditOne);
Which didn't work. I've read about it, but most people suggest using CStrings and I don't want to go back and redo all of my member variables (I have 20 edit boxes). Is there any way to make this work as is?
Unfortunately, saying it didn't work is not much help unless the error is too obvious. You should collect any error numbers being thrown by the system. I don't know if there is an MFC way of calling GetLastError(), but at the very least you can use ::GetDlgItemInt() (the actual WinAPI function used behind the scenes by MFC, see http://msdn.microsoft.com/en-us/library/windows/desktop/ms645485(v=vs.85).aspx ) and if bTranslated is FALSE then call ::GetLastError(). You can also get the error text via FormatMessage().
If you don't understand how MFC works, then don't use it, stick with winapi directly as it will always work as expected.

@webJose

When I say it didn't work, i mean that the value remained at 0 which it was initialized to when it was created. I put breakpoints into the code and then checked the value at each onchange event for all of my 20 edit boxes, each time the values were all zero.
Ok, so then show the code that is supposed to set the integer variables, or maybe explain to us how the textboxes are filled. As of now, I have assumed that the textboxes are filled by the end user the good old fasion way: Clicking over the textbox, then typing the number. After you have collected the data, you would go about using GetDlgItemInt() to collect the actual input as a number. But if this is not a correct assumption, please let us know how this is supposed to work.

And regardless of the above, you need to always check the bTranslated flag to ensure you got a valid number ESPECIALLY if the number is input from a human being. This is there to help, and it indeed helps a lot.
I don't actually know what the bTranslated flag is...

The edit boxes are filled by a user with integer values (whole numbers only, no negative). I trapped OnChange to record each value, so

1
2
3
4
5
6
7
8
9
10
11
12
13
void CMy2011_four_twoDlg::OnChangeEdit16() 
{

	m_EditSixteenControl.GetDlgItemInt(m_EditSixteen); //should return the integer value
	
//This is explained next. Width is obtained previously through an edit box the same way. 
//Using breakpoints, I've determined that I don't get a value for m_Width either.
        if(m_Width==4)
	{
		ColThreeSum=m_EditThree+m_EditSeven+m_EditEleven+m_EditFifteen;
	}
	
}


The boxes are set up and numbered in this order:

1 2 3 4
X X X 8
X X X 12
X X X 16

This is important for the second half of my OnChange event, which gets the sum of rows/collumns depending on where it is (for example OnChange box 8, I would get the row sum of row two).
Last edited on
if you do not know any of what you are doing just stick on the one that you know
Step #1 Make sure your edit controls have the ES_NUMBER so they won't accept non-numeric entry
(there's a corresponding check box in the resource editor).

Step #2 If you're using MFC, then the class wizard should have added the correct DDX macro to its map; you shouldn't have to do anything in the EN_CHANGE handler. But I'd just use int, and get rid of the control.

(DDX_Text has various overloads for int, UINT, etc)

If you need to force the dialog to sync up the controls and the data member, use the dialog's UpdateData() member.

For some examples, see (eg) "Dialog Data Exchange in MFC"
http://www.codeproject.com/Articles/14510/Dialog-Data-Exchange-in-MFC
Last edited on
@andywestken

"I'd just use int, and get rid of the control."

Do you mean using the member variables only? like m_EditThree or whatever? and removing the m_EditThreeControl variable? I just want to make sure I understand you right. You're saying that the DDX macro will set the variable without me trapping the OnChange, and I can just call the variable, correct?
Topic archived. No new replies allowed.