Thread: C Under Windows Problem

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    7

    C Under Windows Problem

    Hello Its Not Related To PHP I Need Some help In C
    I am Trying to Create Windows Programs With C
    Would Anyone lease Tell Me How To Get The Text In a edit box ??
    And This Is The Edit Box
    Code:
    C:
    .............................................................
    hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE,
          "EDIT",
          "",
          WS_BORDER|WS_VISIBLE|WS_CHILD|ES_MULTILINE|ES_AUTOVSCROLL|WS_VSCROLL,
          0,0,370,200,
          hwndMain,
          NULL,
          hInst,
          NULL);
    ...................................................
    I Wanna Track The Text In The Edit Box When Someone Clicks On A Button
    C:
    Code:
    ..................................................................
    	case WM_LBUTTONDOWN:
    		m_edit_val = hwndEdit.dwStyle;
    		MessageBox(0,"Text On The Edit box Would Be Here","Title",MB_OK);
    		break;
    ........................................................
    So How Can I Do It ??

    My CPP File Has Been Attached

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Hi. You can get text from a control using GetWindowText. Here is a simple example:
    Code:
    char edit_text[512];
    GetWindowText(hwndEdit, edit_text, 512);

  3. #3
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Oh! Ya
    Now I Understand
    GetWindowText Justs gets The Texts And Put It In a Var.
    So If I Use
    Code:
    char* edit_text[];
    GetWindowText(hwndEdit, edit_text, strlen(edit_text));
    Would It Work ??

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by neel_basu
    Oh! Ya
    Now I Understand
    GetWindowText Justs gets The Texts And Put It In a Var.
    So If I Use
    Code:
    char* edit_text[];
    GetWindowText(hwndEdit, edit_text, strlen(edit_text));
    Would It Work ??
    No it wouldn't
    1 you don't provide size for the buffer
    2. you edit_text has a wrong type
    3. you cannot use strlen on notinitialized string
    4. even if you initialize the buffer with the empty string strlen will give you 0 instead of the buffer size

    What you can change in the sample is:
    Code:
    char edit_text[512];
    GetWindowText(hwndEdit, edit_text, sizeof(edit_text));
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    No, for the reasons pointed out by vart. In my example, the size of the array is 512, and GetWindowText will return a maximum of 512 characters, as indicated by the last argument.

    If you need to return an unknown number of characters and therefore can not use a fixed size array, you can allocate memory dynamically. This function shows how to do that:
    Code:
    char* AllocAndGetText(HWND hwndControl)
    {
    	int count = GetWindowTextLength(hwndControl);
    	char* result = malloc(count * sizeof(char));
    
    	if (result != NULL)
    	{
    		GetWindowText(hwndControl, result, count);
    	}
    
    	return result;
    }
    and the function would be used in the following manner:
    Code:
    char* edit_text = AllocAndGetText(hwndEdit);
    
    /* Use text in edit_text */
    
    /* Free memory when you're done with it. */
    free(edit_text);
    However, usually there is a reasonable maximum number of characters you would expect and you can use an array.

    This tutorial is very good if you haven't seen it.
    Last edited by anonytmouse; 12-25-2006 at 05:56 AM.

  6. #6
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    But char edit_text[512] Will Limit The Lenth Of The string But I Wanna Let The User Type
    Text In The Textbox AS LONG He/She Wants Then How Can I Do It ??

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  8. #8
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    OK Can I Do It Like This ??
    Code:
    char edit_text[GetWindowTextLength(hwndEdit)];
    GetWindowText(hwndEdit, edit_text, GetWindowTextLength(hwndEdit));

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    not in C
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    In some compilers. Run-time sized arrays are a feature of C99. The main Windows compiler, MSVC, does not support C99 (it supports the previous version of C, C89). Also, C++ does not support C99 features, although some compilers allow it. So in summary:
    - Try it and see.
    - Even if it does work, it will make your code incompatible with other compilers.

  11. #11
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    No It Doesn't Work In MSVC++

  12. #12
    Registered User
    Join Date
    Dec 2006
    Posts
    7
    Here Is An Another
    Code:
    ..................................................................
    	case WM_LBUTTONDOWN:
    		m_edit_val = hwndEdit.dwStyle;
    		MessageBox(0,"Text On The Edit box Would Be Here","Title",MB_OK);
    		break;
    ........................................................
    But If Use case BN_CLICKED:
    It Simply Doesn't Work

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Because BN_CLICKED is a notification, not a message. It's an event code in a WM_COMMAND sent by a button.

    You really just write random code and hope it works, don't you?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    24
    Code:
    CMyEdit : public CEdit
    {
            public:
    	CString GetText()
    	{
    		CString text;
    		this->GetWindowText(text);
    		return text;
    	}
    };
    
    CMyEdit *Field = new CMyEdit();
    ...
    AfxMessageBox(Field->GetText());

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This ain't MFC ...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IPv6 ping in windows...problem..lots of ode:(
    By Neill KElly in forum C Programming
    Replies: 3
    Last Post: 04-27-2009, 11:50 PM
  2. Exit Windows problem
    By cfrost in forum Windows Programming
    Replies: 7
    Last Post: 08-18-2004, 05:21 PM
  3. FlashWindowEx not declared?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 05-17-2003, 02:58 AM
  4. Application Termination Problem in Windows XP
    By wasabee in forum Windows Programming
    Replies: 2
    Last Post: 04-11-2003, 12:53 PM
  5. C++ Gurus, UNIX vs. Windows ascii problem perhaps?
    By puck in forum C++ Programming
    Replies: 6
    Last Post: 03-28-2003, 10:33 PM