Thursday, July 30, 2009

In a VC++ MFC application, How can u access a member variable of an Edit Box, from outside it's Dialog class?

for instance, I want to change the text in a text box in mydialog, from another cpp file. I need to have access to the member variable defined for the Value of that Edit box.


I included the mydialog.h file in the file but it gives bunch of errors.


Please help me out if you are not a beginner like me!

In a VC++ MFC application, How can u access a member variable of an Edit Box, from outside it's Dialog class?
I think this trick could work, because is used same this in an other case.





Define an CStatic pointer as pTextBox In your dialog cpp file. I mean this line:


CStatic *pTextBox;





If you do it in your dialog cpp file, outside of all method (In begging of file and after includes prefred) you defined a file scope variable. to get access it from your another cpp file which you want to change the text box value from, redefine it by using extern keyword. Like this:





extern CStatic* pTextBox;





Notice that insert this line outside of any method or class. How I told the best place is after include commands.





Now if you assign the variable the pointer of the text box class you could access it in your another cpp file. To do so insert this line into your OnInitDialog event:





pTextBox=%26amp;m_txtTextBox // (m_txtTextBox is your member var)





If you create a CString as member variable for your text box, You can change the everything in this sample from CStatic to CString. But in that case you should be sure that the UpdateData method called with TRUE parameter before running the code stored in other cpp file. I don't recomend the second way.





Be Succeed.


No comments:

Post a Comment