Thread: Edit Box Questions PT. II

  1. #1
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644

    Edit Box Questions PT. II

    Ok, here's another edit box question:

    How would I save all the text inside of the edit box into a file, and also open the file, and put all that text in the edit box.

    And 1 more: How would I look at the first line, and second line to see if a certain string of text is there, and if so, replace it?

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>How would I save all the text inside of the edit box into a file, and also open the file, and put all that text in the edit box.<<

    Create/open a file (api): CreateFile. To read from a file (api): ReadFile. To write to a file (api): WriteFile.
    If your are more comfortable or familiar with c/c++ file manipulation functions/streams then use those. Use GetWindowText and SetWindowText to get and set the edit control's contents as a whole.

    >>How would I look at the first line, and second line...<<

    EM_GETLINE.

    >>...and if so, replace it?<<

    EM_SETSEL followed by EM_REPLACESEL (you may get an EN_ERRSPACE message if the edit control doesn't have the memory/capacity to add extra text).
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  3. #3
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Ken Fitlike
    Create/open a file (api): CreateFile. To read from a file (api): ReadFile. To write to a file (api): WriteFile.
    If your are more comfortable or familiar with c/c++ file manipulation functions/streams then use those. Use GetWindowText and SetWindowText to get and set the edit control's contents as a whole.

    >>How would I look at the first line, and second line...<<

    EM_GETLINE.

    >>...and if so, replace it?<<

    EM_SETSEL followed by EM_REPLACESEL (you may get an EN_ERRSPACE message if the edit control doesn't have the memory/capacity to add extra text).
    I've tried GetWindowText, but it only returned 0 because it got the title bar.

    Everytime I tried the SETSEL and REPLACESEL, it'd insert it at the first line (I want something to be inserted on the second line, no matter what).

    If you want, IM me on AIM at evanescence s0ul and I'll send you the code.

  4. #4
    jasondoucette.com JasonD's Avatar
    Join Date
    Mar 2003
    Posts
    278
    Originally posted by Quantrizi
    I've tried GetWindowText, but it only returned 0 because it got the title bar.
    From the GetWindowText Function MSDN page:

    The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied.

    Make sure you are passing it the handle of the edit control, and not the main program window handle.

  5. #5
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by JasonD
    From the GetWindowText Function MSDN page:

    The GetWindowText function copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied.

    Make sure you are passing it the handle of the edit control, and not the main program window handle.
    I have, I got
    Code:
    // Declared before any other code
    HWND hEdit; 
    char *szBuffer;
    
    /* create code for edit box */
    GetWindowText(hEdit, szBuffer, sizeof(hEdit));
    And I have it output the data to a file, and it returns 0 *meaning it got the window title bar*

  6. #6
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    >>char *szBuffer;<<

    I hope you've allocated memory for that before using it...

    To get the length of the text use GetWindowTextLength and then use new/malloc to allocate sufficient memory.

    Then use GetWindowText as described by msdn and clarified by JasonD.

    edit: On second thoughts, this is nonsense:

    >>GetWindowText(hEdit, szBuffer, sizeof(hEdit));<<

    So do something like this instead:
    Code:
    int TxtLen=GetWindowTextLength(hEdit);
    TCHAR *buffer=new TCHAR[TxtLen+1]; /*or use malloc if you prefer */
    GetWindowText(hEdit,buffer,TxtLen);
    //do stuff with 'buffer'
    delete [] buffer;  /* or use free if you used malloc */
    Last edited by Ken Fitlike; 08-11-2003 at 04:04 PM.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  7. #7
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by Ken Fitlike
    >>char *szBuffer;<<

    I hope you've allocated memory for that before using it...

    To get the length of the text use GetWindowTextLength and then use new/malloc to allocate sufficient memory.

    Then use GetWindowText as described by msdn and clarified by JasonD.

    edit: On second thoughts, this is nonsense:

    >>GetWindowText(hEdit, szBuffer, sizeof(hEdit));<<

    So do something like this instead:
    Code:
    int TxtLen=GetWindowTextLength(hEdit);
    TCHAR *buffer=new TCHAR[TxtLen+1]; /*or use malloc if you prefer */
    GetWindowText(hEdit,buffer,TxtLen);
    //do stuff with 'buffer'
    delete [] buffer;  /* or use free if you used malloc */
    Ok, that gets what I need, now, where the '//do stuff w/ 'buffer'' is, I'd do all the seeking and stuff, right?

    << EDIT >>: Ok, here's my code:
    Code:
    int GetText()
    {
    	int TxtLen=GetWindowTextLength(hEdit);
    	
    	if(TxtLen == 0)
    	{
    		FormatMessage( 
    			FORMAT_MESSAGE_ALLOCATE_BUFFER | 
    			FORMAT_MESSAGE_FROM_SYSTEM | 
    			FORMAT_MESSAGE_IGNORE_INSERTS,
    			NULL,
    			GetLastError(),
    			MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
    			(LPTSTR) &lpError,
    			0,
    			NULL);
    		szError=new char[strlen(lpError) + 128];
    		strcpy(szError, lpError);
    		strcat(szError, "While using GetWindowTextLength");
    		MessageBox(NULL, szError, "Error", MB_ICONERROR);
    		return 1;
    	}
    	else
    	{
    		TCHAR *buffer=new TCHAR[TxtLen+1]; /*or use malloc if you prefer */
    		TCHAR *buff = new TCHAR[TxtLen];
    
    		GetWindowText(hEdit,buffer,TxtLen);
    		
    		SendMessage(hEdit, EM_GETLINE, 0, (LPARAM)buff);
    		if(buff == "'{$PBASIC 2.0}")
    		{
    			SendMessage(hEdit, EM_SETSEL, 0, 0);
    			SendMessage(hEdit, EM_REPLACESEL, (WPARAM)true, (LPARAM)"'{$PBASIC 2.5}");
    		}
    
    		//Log what GetWindowText gets from the buffer
    		FILE *log;
    		log = fopen("log.txt", "w");
    		fprintf(log, "");
    		fprintf(log, "%s", buffer);
    		fclose(log);
    		
    		delete [] buff;
    		delete [] buffer;  /* or use free if you used malloc */
    	}
    	return 0;
    }
    No errors or nothing (AFAIK) are happening, but it will not replace. I think it deals w/ the SET & REPLACE parts.
    Last edited by Quantrizi; 08-11-2003 at 04:55 PM.

  8. #8
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    Take a look at the Forgers Win32 API tutorial here: http://www.winprog.org/tutorial/

    It is a good tutorial and explains almost everything you want to know.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  9. #9
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by sean345
    Take a look at the Forgers Win32 API tutorial here: http://www.winprog.org/tutorial/

    It is a good tutorial and explains almost everything you want to know.

    - Sean
    I searched through-out the whole thing, and couldn't find anything about replacing text, finding text, and so forth

  10. #10
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    I know the Forgers Win32 tutorial contains information on how to save and load text from a file to an edit control. I'm not sure if it goes into anything about searching.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  11. #11
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by sean345
    I know the Forgers Win32 tutorial contains information on how to save and load text from a file to an edit control. I'm not sure if it goes into anything about searching.

    - Sean
    Ok, thank you. I can save/open files successfully now (sorry about judging the site before, I must've skimmed over the saving link).

  12. #12
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    The save and open stuff is under using files and common dialogs. Also, have you checked out MSDN. The link below will take you to information about edit controls. It is very useful:
    http://msdn.microsoft.com/library/de...operations.asp

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  13. #13
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by sean345
    The save and open stuff is under using files and common dialogs. Also, have you checked out MSDN. The link below will take you to information about edit controls. It is very useful:
    http://msdn.microsoft.com/library/de...operations.asp

    - Sean
    Found it, thanks.

    I'll check out the MSDN stuff (although, I'm pretty sure I've already tried that stuff, thanks though).

  14. #14
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    You're not getting the correct HWND for the edit

    GetWindowText(hEdit,buffer,TxtLen);

    as the hEdit is incorrect

    try

    hEdit = GetDlgItem(hMainDlg,ID_EDIT); //use the ID you gave the edit and the HWND in the callback

    PS If you are going to dynamically allocate memory, don't forget to error check and free it.

    for this case a set buffer should suffice (depending on the size of your edit )

    Code:
    char         szBuffer[256]={0}; //init to zero
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  15. #15
    I am the worst best coder Quantrizi's Avatar
    Join Date
    Mar 2002
    Posts
    644
    Originally posted by novacain
    You're not getting the correct HWND for the edit

    GetWindowText(hEdit,buffer,TxtLen);

    as the hEdit is incorrect

    try

    hEdit = GetDlgItem(hMainDlg,ID_EDIT); //use the ID you gave the edit and the HWND in the callback

    PS If you are going to dynamically allocate memory, don't forget to error check and free it.

    for this case a set buffer should suffice (depending on the size of your edit )

    Code:
    char         szBuffer[256]={0}; //init to zero
    Ok, here's how I create the edit box:
    Code:
    		// code to create the text (edit box) box
    		hEdit = CreateWindow("EDIT",NULL,WS_CHILD | WS_VISIBLE | ES_AUTOHSCROLL |
    								ES_AUTOVSCROLL | ES_LEFT | ES_MULTILINE | ES_NOHIDESEL |
    								WS_VSCROLL | WS_HSCROLL | WS_BORDER | ES_WANTRETURN,
    								0, 0, rect.right, rect.bottom, hwnd, (HMENU)EDIT_BOX,
    								((LPCREATESTRUCT)lParam)->hInstance, NULL);
    And here's my handle to the edit box:
    Code:
    #define EDIT_BOX 0x00EB    // Handle to edit box <-- from GameTutorials.com
    HWND hEdit = NULL;		  // This is a handle to the "edit box" we're going to make <-- What I use
    I think I'm doing this wrong, because I think I have to use the EDIT_BOX, please tell me if I'm wrong or not.

    >> EDIT <<: If I use EDIT_BOX, it gives me an error about not being able to convert from 'const int' to 'HWND __*', and about the SendMessage not being to convert it from 'const int' to 'HWND __*'
    Last edited by Quantrizi; 08-12-2003 at 06:43 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. edit box
    By beene in forum Windows Programming
    Replies: 3
    Last Post: 11-11-2006, 04:40 AM
  2. WS_HSCROLL in ES_READONLY edit box error
    By Homunculus in forum Windows Programming
    Replies: 4
    Last Post: 02-13-2006, 08:46 AM
  3. How to program a "back" button with MFC
    By 99atlantic in forum Windows Programming
    Replies: 3
    Last Post: 04-26-2005, 08:34 PM
  4. Edit Control Questions
    By Jaken Veina in forum Windows Programming
    Replies: 2
    Last Post: 04-18-2005, 05:34 PM
  5. Edit box question
    By learning110 in forum Windows Programming
    Replies: 6
    Last Post: 03-28-2003, 08:16 PM