MFC, how to set data to an EditBox by its ID?  
Author Message
spree





PostPosted: Visual C++ Language, MFC, how to set data to an EditBox by its ID? Top

Hey all, I have a Edit box which needs to be linked to a u_short variable.

From Dialog design I added my variable to the Edit box and when i wanted to set and retrieve data betwin the control and my variable I used UpdateData true/false, and it does nothing :/

So I wanted to ask, how do I assign a value to/from the Edit box using its ID

Thanks :)



Visual C++8  
 
 
spree





PostPosted: Visual C++ Language, MFC, how to set data to an EditBox by its ID? Top

I found it:

SetDlgItemTextW ...


 
 
Sarath.





PostPosted: Visual C++ Language, MFC, how to set data to an EditBox by its ID? Top

It's better to use SetDlgItemText only because SetDlgItemTextW is a unicode interface. So if you define ANSI/MBCS character set for your application, your program wil lnot compile.
Normally SDK functions and APIs are defined with unicode awareness.

It's something like this.

BOOL
WINAPI
SetDlgItemTextA(
HWND hDlg,
int nIDDlgItem,
LPCSTR lpString);
WINUSERAPI
BOOL
WINAPI
SetDlgItemTextW(
HWND hDlg,
int nIDDlgItem,
LPCWSTR lpString);
#ifdef UNICODE
#define SetDlgItemText SetDlgItemTextW
#else
#define SetDlgItemText SetDlgItemTextA
#endif // !UNICODE


So compiler will choose as per your environment


 
 
Sarath.





PostPosted: Visual C++ Language, MFC, how to set data to an EditBox by its ID? Top

BTW forgot to tell you.
Suppose if your member varibles which is attached with edit is m_uEditVal.

UpdateData();
m_uEditVal = 10; //Some values
UpdateData(FALSE);

The above code should work fine.

Please make sure that the added variable has attached with the control in the DoDataExchange function



 
 
Simple Samples





PostPosted: Visual C++ Language, MFC, how to set data to an EditBox by its ID? Top

UpdateData implies you are using MFC. MFC has features that are supposed to make things easy. MFC also has various tutorials that include descriptions of many things such as this. Look for the tutorials; as far as I know, they are still provided for free with VC.

 
 
Simple Samples





PostPosted: Visual C++ Language, MFC, how to set data to an EditBox by its ID? Top

I have looked for the MFC tutorials and I can't find them in the VS 2005 documentation. The first tutorial was called Scribble and it does exist as a sample in the VS 2005 documentation. If the tutorials for MFC exist for VS 2005 then I hope someone will say so.

The tutorials for VC 6 are at Visual C++ Tutorials. They should still help but some of the details of how to use the IDE have changed.