|
Is there a way to retreive the data entered in a Edit Control Box while using a numerical Type variable? I've seen lots of examples that retreive the data when the variable Type is a CString, but none when it is numerical. In my dialog box I've got a Edit Control Box and I've added a variable of integer type so the number entered could be retreived without doing a conversion. But the value is always zero no mater what is entered. The following is some of the code:
Edit_Numbers_Dlg::Edit_Numbers_Dlg(CWnd* pParent /*=NULL*/)
: CDialog(Edit_Numbers_Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(Edit_Numbers_Dlg)
m_entered_number = 0;
//}}AFX_DATA_INIT
}
void Edit_Numbers_Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(Edit_Numbers_Dlg)
DDX_Text(pDX, IDC_ENTERED_NUMBER_EDIT, m_entered_number);
DDV_MinMaxInt(pDX, m_entered_number, 0, 999999);
//}}AFX_DATA_MAP
}
void Edit_Numbers_Dlg::SearchNumbers()
{
for( int z=0; z<counter-1; z++="" )<br="" mode="hold"> {
if( oldnumbers[z] == m_entered_number )
found = 1;
}
}
|
|
|
|
|
hilop wrote: Is there a way to retreive the data entered in a Edit Control Box while using a numerical Type variable?
Yes. Use GetWindowText() , followed by atoi() .
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Isn't GetWindowText() and atoi() for string types?
|
|
|
|
|
Edit controls contain only text, thus to get an integer you must get the text and convert it.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Joe Woodbury wrote: only text, thus to get an integer you must get the text and convert it
Not true. If you work with a CEdit* and Get/SetWindowText then is ok what you say. But you can also associate a numeric member variable to the edit in the resource editor and it works as well.
HEADER
enum { IDD = IDD_MYFORMVIEW};
double m_dMyDouble;
CPP
void CMyFormView::DoDataExchange(CDataExchange* pDX)
{ CFormView::DoDataExchange(pDX);
DDX_Text(pDX, IDC_MY_EDIT, m_dMyDouble);
}
With this you will be able to work directly with numbers. And still more, if you make it like that, the edit will give you an error itself when you are introducing letters.
PS. This is under MFC (VC++ 6.0)
Greetings.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
modified on Monday, June 2, 2008 10:37 AM
|
|
|
|
|
It's only text. MFC converts it to a numeric value, but the control stores only text. Next time, please actually look at what the source code is doing.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
From OP: Is there a way to retreive the data entered in a Edit Control Box while using a numerical Type variable?
Ok, it is the MFC who converts to numeric, but the OP is using MFC and having problems to associate the edit to a numeric variable... so the fact is, that he actually can do it.
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
Your code should work. Did you call UpdateData(TRUE) to retrieve the data from the control?
Edit: Actually, your for loop is incomplete.
|
|
|
|
|
Have you tried CWnd::GetDlgItemInt()[^]?
Nobody can give you wiser advice than yourself. - Cicero
.·´¯`·->Rajesh<-·´¯`·.
Codeproject.com: Visual C++ MVP
|
|
|
|
|
Although my answer[^] has been downvoted, you may want to check it. The other answers about get the data in string format and then convert it are totally correct, but you can make what you were asking as well.
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
GetWindowText() would not work with a type of int or cstring. Debug always showed the value was 0 though I'd entered 123456. So I've changed the category to Control and the type is CEdit. This is what I'd been doing earlier in the code and used the following code to get and convert the entry.
m_entered_number.GetWindowText( temp_CString );
entered_number = atoi( temp_CString );
Was looking for a way to eliminate the string and conversion but VS does not want to cooperate.
Everyone, thank you for you help.
|
|
|
|
|
hilop wrote:
m_entered_number.GetWindowText( temp_CString );
entered_number = atoi( temp_CString );
You are using it wrongly. If you declare the member variable as numeric then you can not use it. (see my downvoted answer)
If you want to use that functions you have to declare the member variable as CEdit.
Or do it programmatically:
CEdit* pEdit = GetDialogItem (IDC_MYEDIT);
pEdit->GetWindowText (strTemp);
number = atoi (strTemp);
PS: I don't have VC++ in this laptop and I don't remember if the function is GetDialogItem or GetDlgItem. Check you local help, it anyways is a member function of CDialog
Regards.
--------
M.D.V.
If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Rating helpfull answers is nice, but saying thanks can be even nicer.
|
|
|
|
|
you forget to give call to UpdateDate(false) function which retrieve values from dialog box back to variable
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Hello dear friends,
I've created a .docx file programatically but opening this file says that
the file has been currupted.
Here is the code.
unsigned long nSize = 0;
char szBuffer[100];
CFile o;
CFileException e;
strcpy(szBuffer,"hi");
nSize=strlen(szBuffer);
if(!o.Open("C:\\Documents and Settings\\Rigel\\Desktop\\Exl.docx", CFile::modeCreate | CFile::modeWrite,&e))
{
MessageBox("Can't open the file for writing.",_T("File"),MB_OK);
}
else
{
o.Write(szBuffer,nSize);
o.Close();
}
ShellExecute(NULL,"open","C:\\Documents and Settings\\Rigel\\Desktop\\Exl.docx",NULL,NULL,SW_NORMAL);
Help me please.
ritz1234
|
|
|
|
|
Well, of course, if you just put a string in the file without respecting the file format. A doc (and docx) file is a binary file, so you have to respect the format of that file. I know that you can use automation to create doc file but I don't know if the format is the same as a docx file.
|
|
|
|
|
Cedric Moonen wrote: I know that you can use automation to create doc file but I don't know if the format is the same as a docx file.
Of course not, docx file is an XML file.
led mike
|
|
|
|
|
|
All you've done is create a file with a .docx extension, not a true Word 2007 document.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
I am creating an application in VC++4 on Win CE5. The application is for a smart device with ARMV4I CPU. The code is compiled under the device SDK.
In the below code, I try to write all objects passed in an CArray object. The error that I get is also mentioned below.
void DbOperations::WriteDepartments(CArray<DeptDetails, DeptDetails&> dptArr)
{
HANDLE hFile = CreateFile(DEPT_FILE,
GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("Error OPeniong File"));
else {
CFile myfile((int)hFile);
CArchive ar(&myfile, CArchive::store);
if (dptArr.GetSize() == 0)
return;
for (int pos=0; pos < dptArr.GetSize(); pos++) {
DeptDetails& ddt = dptArr.ElementAt(pos);
ddt.Serialize(ar);
ddt.~DeptDetails();
}
myfile.Close();
}
return;
}
ERROR: c:\program files\windows ce tools\wce500\mc1000c50\mfc\include\afxtempl.h(855) : error C2582: 'DeptDetails' : 'operator =' function is unavailable
c:\program files\windows ce tools\wce500\mc1000c50\mfc\include\afxtempl.h(1578) : while compiling class-template member function 'struct __POSITION *__cdecl CList<class DeptDetails,class DeptDetails &>::AddTail(class DeptDetails &)'
Error executing clarm.exe.
I also tried the above with CMapStringToPtr
void DbOperations::WriteDepartments(CMapStringToPtr dptMap)
{
HANDLE hFile = CreateFile(DEPT_FILE,
GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("Error OPeniong File"));
else {
CFile myfile((int)hFile);
CString key;
DeptDetails* dd;
CArchive ar(&myfile, CArchive::store);
for (POSITION pos = dptMap.GetStartPosition(); pos != NULL;) {
dptMap.GetNextAssoc(pos, key, (DeptDetails*&)dd );
dd->Serialize(ar);
}
key.Empty();
dd->~DeptDetails();
myfile.Close();
}
return;
}
ERROR : : error C2664: 'GetNextAssoc' : cannot convert parameter 3 from 'class DeptDetails *' to 'void *& '
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing clarm.exe.
Also, if I don't comment the code of CMap, I don't get any errot of CArray; only error of CMap else of CArray.
Does anyone has any idea where I am going wrong. Please help me out.
Thanks
Terry
|
|
|
|
|
What does the declaration of DeptDetails look like?
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
Trupti Mehta wrote: ERROR: c:\program files\windows ce tools\wce500\mc1000c50\mfc\include\afxtempl.h(855) : error C2582: 'DeptDetails' : 'operator =' function is unavailable
c:\program files\windows ce tools\wce500\mc1000c50\mfc\include\afxtempl.h(1578) : while compiling class-template member function 'struct __POSITION *__cdecl CList<class deptdetails,class="" deptdetails="" &="">::AddTail(class DeptDetails & [Wink] '
Error executing clarm.exe.
I think DeptDetails is derived from CObject , because you are serializing it. If your class is derived from CObject, the compiler won't generate default copy constructor and assignment operator for your derived class. So you should provide it. For more info - http://msdn.microsoft.com/en-us/library/ccb3dh5c.aspx[^]
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Yes you are absolutely correct, DeptDetails is derived from CObject & IMPLEMENT_SERIAL(DeptDetails, CObject, 1);
is added as expected.
Thanks for your help.
Based on your link, I added both constructor/method :
public:
DeptDetails(const DeptDetails &d);
DeptDetails& operator=(const DeptDetails &d);
And implemented as :
DeptDetails::DeptDetails(const DeptDetails &d) {
deptNo = d.deptNo;
deptName = d.deptName;
vat = d.vat;
}
DeptDetails& operator=(const DeptDetails &d) {
deptNo = d.GetDeptNo();
deptName = d.GetDeptName();
vat = d.GetVat();
return *this;
}
Copy constructor is perfect, but operator assignment gives 8 error :
eptDetails.cpp
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(28) : error C2801: 'operator =' must be a <Unknown> member
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(29) : error C2065: 'deptNo' : undeclared identifier
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(29) : error C2662: 'GetDeptNo' : cannot convert 'this' pointer from 'const class DeptDetails' to 'class DeptDetails &'
Conversion loses qualifiers
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(30) : error C2065: 'deptName' : undeclared identifier
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(30) : error C2662: 'GetDeptName' : cannot convert 'this' pointer from 'const class DeptDetails' to 'class DeptDetails &'
Conversion loses qualifiers
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(31) : error C2065: 'vat' : undeclared identifier
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(31) : error C2662: 'GetVat' : cannot convert 'this' pointer from 'const class DeptDetails' to 'class DeptDetails &'
Conversion loses qualifiers
D:\TRUPTI\HANDHELD\MC_App\DeptDetails.cpp(33) : error C2673: '=' : global functions do not have 'this' pointers
Error executing clarm.exe.
DeptDetails.obj - 8 error(s), 0 warning(s)
I tried a lot, and understand that their is a simple minute mistake which is causing so many errors, but couldn't trap the mistake.
Can please help me know the mistake and resolve the errors.
I appreciate your help.
Thanks
Terry
|
|
|
|
|
Trupti Mehta wrote: // Assignment operator
DeptDetails& operator=(const DeptDetails &d) {
deptNo = d.GetDeptNo();
deptName = d.GetDeptName();
vat = d.GetVat();
return *this;
}
Did you forget to add DeptDetails:: while copy pasting the function signature from header to cpp?
DeptDetails& DeptDetails::operator=(const DeptDetails &d)
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|
Thanks Jijo,
But this didn't solve the Map error of the followng function:
void DbOperations::WriteDepartments(CMapStringToPtr dptMap)
{
HANDLE hFile = CreateFile(DEPT_FILE,
GENERIC_WRITE, FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
AfxMessageBox(_T("Error OPeniong File"));
else {
CFile myfile((int)hFile);
CString key;
DeptDetails* dd;
CArchive ar(&myfile, CArchive::store);
for (POSITION pos = dptMap.GetStartPosition(); pos != NULL;) {
dptMap.GetNextAssoc(pos, key, (DeptDetails&)dd );
dd->Serialize(ar);
}
key.Empty();
dd->~DeptDetails();
myfile.Close();
}
return;
}
On GetNextAssoc method, I still get :
error C2664: 'GetNextAssoc' : cannot convert parameter 3 from 'class DeptDetails *' to 'void *& '
A reference that is not to 'const' cannot be bound to a non-lvalue
Error executing clarm.exe.
How to resolve this error yet? Do i have to write another operator= for DeptDetails*. Can you let me know the code so I don't make mistake again.
I highly appreciate for your quick and helpful responses.
One more query I have:
I wanted to store deptNo (int) as key & DeptDetails as value in a Map. I looked at CMap but couldn't find any method to iterate the map contents, hence selected CMapStringToPtr seemed to be best suited to store deptNo in the form of CString. Is this decision correct or should I approach some other class of CMap.
Thanks
Terry
|
|
|
|
|
Buddy, sorry for the late reply. I went to city with my friend to buy a brand new helmet. So back to your problem,
Trupti Mehta wrote: dptMap.GetNextAssoc(pos, key, (DeptDetails& [Wink] dd ); //.GetNextAssoc(pos, key, (DeptDetails*& [Wink] dd);
The reason for your last error is - 3ed parameter of GetNextAssoc() expects CObject*& . Rewrite the code snippet as follows,
DeptDetails* dd;
...
dptMap.GetNextAssoc(pos, key, ((CObject*&)dd);
Trupti Mehta wrote: How to resolve this error yet? Do i have to write another operator= for DeptDetails*. Can you let me know the code so I don't make mistake again.
I hope everything will be solved by this fix. Yep, I've posted the code snippet!
Trupti Mehta wrote: One more query I have:
I wanted to store deptNo (int) as key & DeptDetails as value in a Map. I looked at CMap but couldn't find any method to iterate the map contents, hence selected CMapStringToPtr seemed to be best suited to store deptNo in the form of CString. Is this decision correct or should I approach some other class of CMap.
1) You can iterate CMap by using GetStartPosition() and GetNextAssoc() .
2) Since your intention is to keep int as key, instead of CMapStringToPtr , CMapWordToPtr will be more suitable, because it have mapkey as word by default and you can avoid that DepartNo to CString conversion.
Feel free to ask more doubts, if you have.
Regards,
Jijo.
_____________________________________________________
http://weseetips.com[ ^] Visual C++ tips and tricks. Updated daily.
|
|
|
|
|