| partytime4321 (28) | |||
|
Hi everyone, I am working on one of my first windows.h projects, and this really stumped me; I cannot seem to find a way to get the input the user has typed into a text box, and then store that in a variable. A little background on what I want to do: > User executes the program > text file with required info is in the same directory, just need the name > prompt for the name, and have an edit field for the file name > user presses ok button when ready to proceed (I can get my program to respond to buttons, but Im stuck on the text input portion) > when the user has pressed ok, the program reads the data from the edit box, stores it, closes the window, and opens a different window after preforming several actions. Here is ALL of the code so far. (Sorry that I have to post all of it, im not sure exactly what is and isn't relevant with win32 GUI projects yet)
I would really appreciate it if someone could help me with this, I am quite lost and the community here has been very helpful in the past. Incidentally, if anyone happens to know how to destroy this window and then create a new one automatically, if you could please explain briefly that would be great as well. Thanks very much, Ryan | |||
|
|
|||
| partytime4321 (28) | |
| Line 88 is where the text field I would like to read from is created | |
|
|
|
| andywestken (1964) | |
|
You can use GetWindowText() to read all the text from an Edit control. GetWindowText function (Windows) http://msdn.microsoft.com/en-gb/library/windows/desktop/ms633520%28v=vs.85%29.aspx But it works with a char buffer, so you'll need to read it from the control and then assign it to a std::string afterwards. Also see GetWindowTextLength function (Windows) http://msdn.microsoft.com/en-gb/library/windows/desktop/ms633521%28v=vs.85%29.aspx Andy PS There is no way to automatically recreate a window using Win32. Given the way you're using your window above, I would use a dialog to obtain the filename instead. And then create the main window after closing the dialog. | |
|
Last edited on
|
|
| partytime4321 (28) | |
|
Thanks very much for your quick reply, It doesnt matter to me whether the file name is char[] or string. I can change to an array of characters if that would make it easier. how would I write the assignment part if I just change: std::string textfilename; to: char textfilename[50];I looked at the GetWindowText() on msdn, but I dodnt understand what they were talking about :/ it says something about returning the length of the input, but I would have thought it would return the actual input or a pointer to it. If you could give me a quick example of just how I would assign it to an array, that would be great. Thanks very much, Ryan | |
|
|
|
| partytime4321 (28) | |
| Oh do I just pass the name of the array as the 2nd parameter? | |
|
|
|
| andywestken (1964) | |||
Yes, the second param is the char array name or char* buffer pointer (TCHAR would be better here, to go with TEXT())
Andy PS _MAX_PATH is defined somewhere in windows.h or one of the headers it includes; it's maximum normal file path (260 chars) _countof() gives then number of elements of an array (it's a smarter version of sizeof(a)/sizeof(a[0])) | |||
|
Last edited on
|
|||
| partytime4321 (28) | |
|
Sorry to bother you again with what is probably a silly question but I cannot get it to compile with hwndEdit as parameter 1... Im really not sure what I am supposed to fill in there. The error message says "hwndEdit was not declared in this scope". Thanks, Ryan | |
|
|
|
| andywestken (1964) | |
|
Well, you've forgotten to declare it. When you call CreateWindow it returns an HWND which you need to keep hold of if you want to communicate with the child window. You've forgotten to do this for all three of your child windows above. I just chose a variable name for illustrative purposes. Andy | |
|
|
|
| partytime4321 (28) | |||
This is what I did:
Is that right? Quick question, I am used to working with the console, and the windows API is pretty foreign to me... Does the GetWindowText function get only the first word (like cin), or the entire line (like cin.getline)? Thanks very much, Ryan | |||
|
|
|||
| partytime4321 (28) | |
| I got it all working when I moved the 3 HWND declarations to global... Thank you very much for your help! | |
|
|
|