CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: Create TextBox Win32 VS2008 C++

  1. #1
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Create TextBox Win32 VS2008 C++

    How can I create TextBoxes for input in C++?

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Create TextBox Win32 VS2008 C++

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Apr 2009
    Posts
    598

    Re: Create TextBox Win32 VS2008 C++

    The link above is to CreateWindow(). You can also use CreateWindowEx(), e.g.
    Code:
     hwnd_ed_123 = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", "ED_123",
                                  WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_LEFT,
                                  x, y, w, h,
                                  hwnd, (HMENU)(123),
                                  (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), NULL);
    A more commonly used method is to define you edit box in your resource file, rather than to create it "on the fly" with CreateWindow() or CreateWindowEx().
    See your documentation about "resource files".

  4. #4
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Re: Create TextBox Win32 VS2008 C++

    Man, I'm so stupid! >.< Thanks olivthill2, it worked!

    EDIT: How can I get input from the Textbox?
    Last edited by serpentinez; June 4th, 2010 at 05:18 PM.

  5. #5
    Join Date
    Apr 2010
    Posts
    13

    Re: Create TextBox Win32 VS2008 C++

    I use Dev-C++, im not sure whether there is a difference from that to Visual Studio but here is how i do it.

    First create something suitable for the textbox:

    This would most likely be used in the resource file which olivthill2 mentioned earlier.

    Code:
    #define IDC_TEXT 101
    Then create the control:

    From: olivthill2

    Code:
    HWND hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "edit", "ED_123",
                                  WS_CHILD | WS_VISIBLE | WS_TABSTOP | WS_BORDER | ES_LEFT,
                                  x, y, w, h,
                                  hwnd, (HMENU) IDC_TEXT,
                                  (HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), NULL);
    Then you would need to store the data from the text field into memory:

    Code:
    char szInput[MAX_PATH];
    
    GetWindowText(GetDlgItem(hwnd, IDC_TEXT), szInput, MAX_PATH);
    Doing this should create a text box, then put the text from the text box into 'szInput'

    I am also learning so if anyone would like to add something (if its needed) thanks.

    RJK
    Last edited by RJK; June 6th, 2010 at 06:05 PM.

  6. #6
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Re: Create TextBox Win32 VS2008 C++

    Yup, that does it! Thanks RJK! Just one curious question. People are always saying to put #define's and stuff like that into resource files. Do they mean .rc resource files? You can't put any code into a .rc file, do they mean a header file named, "resource.h"?

  7. #7
    Join Date
    Apr 2009
    Posts
    598

    Re: Create TextBox Win32 VS2008 C++

    Personally, I prefer to avoid having too many files. If I don't have many #define, then I write them in a ".c" file.

  8. #8
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Re: Create TextBox Win32 VS2008 C++

    And they are compatible together? How would you do that anyways, and how do you do it if you have many #define?

  9. #9
    Join Date
    Apr 2010
    Posts
    13

    Re: Create TextBox Win32 VS2008 C++

    serpentinez, i believe that is what they mean, but the file it usually called resource (as this is where you will be getting your resources) so they typically name it 'resource.h'.

    Again, just like olivthill 2 said, if you dont have too many controls then there is no need for a resource file, but if you would it will go something like this (My Method) :

    resource.h file:
    Code:
    //---------Buttons----------
    #define IDB_BUTTON 100
    #define IDB_CALC 101
    #define IDB_TEST 102
    #define IDB_LOL 103
    #define IDB_DUNNO 104
    //-----------------------------
    
    //--------Edit Boxes--------
    #define IDC_EDIT 200
    #define IDC_EDITTWO 201
    #define IDC_EDITTHREE 202
    #define IDC_EDITFOUR 203
    //-----------------------------
    
    //------Combo Boxes------
    #define ICB_DROPDOWN 300
    //-----------------------------
    //etc........................
    That is usually how i lay mine it, it makes it much easier to view and edit the file.

    Then you would implement 'resource.h' into your 'AppName.cpp':

    Code:
    #include "resource.h" //To use this 'resource.h' needs to be in the same directory as 'AppName.cpp'
    And thanks serpentinez for the reply, mostly appreciated.

    And again, if anyone see anything wrong, or a better way of doing something with my code, please feel free to explain.

    RJK
    Last edited by RJK; June 8th, 2010 at 05:57 PM.

  10. #10
    Join Date
    Apr 2010
    Location
    Udeliketano
    Posts
    44

    Re: Create TextBox Win32 VS2008 C++

    I've seen the actually textbox created using a #define. How would you do this? Thanks for all your help in this!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)