Thread: Getting the contents of text boxes even if empty

  1. #1
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257

    Getting the contents of text boxes even if empty

    Hey,

    I want to extract user input from edit boxes into an array. The code I have only works if all the boxes are filled. I want it to keep going if the edit box is empty and just not put that value into the vector. Here's waht I got:
    Code:
                    structLink templink;
    
    	CEdit* pGetEdit[512];
    	
    	for(i=0; i<FAD.nExpected; i++)
    	{
    		
    		pGetEdit[i] = (CEdit*) GetDlgItem(i+100);
    
    		int len = pGetEdit[i]->LineLength();
    
    		if(len != 0)
    		{
    			pGetEdit[i]->GetLine(1,templink.ParamName.GetBuffer(len), len);
    			templink.Index = i;
    			vars.vectParmSelected.push_back(templink);
    		}
    
    	}
    It gives me a run-time error that memory could not be read.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    One thing to look into is the fact that in your call to GetLine() you're always specifying 1 for the index of the line from which you want to get the text. I would stick with GetWindowTextLength() and GetWindowText() instead of LineLength() and GetLine() if you are retrieving all of the edit controls' text.

  3. #3
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>CEdit* pGetEdit[512];

    don't need an array of pointers to the edits, just reuse the same one.

    >>pGetEdit[i]->GetLine(1,templink.ParamName.GetBuffer(len), len);


    If you use GetLine()
    the edit must have ES_MULTILINE or the call is ignored,
    you must put the max buffer size as a WORD in the buffer you send in,
    the returned buffer does not contain a null terminator ('\0'),


    >>if(len != 0)

    you handle what happens if the edit has text.

    Write something if the edit does not have text.

    else // (if len ==0)
    {
    //put an empty string into the buffer (set first element to '\0')
    //update storage
    "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

  4. #4
    Registered User
    Join Date
    May 2005
    Location
    Toronto, Canada
    Posts
    257
    >> the edit must have ES_MULTILINE or the call is ignored

    I got this from MSDN: nIndex
    Specifies the line number to retrieve from a multiple-line edit control. Line numbers are zero-based; a value of 0 specifies the first line. This parameter is ignored by a single-line edit control.

    and it does work if the edit box has something in it.

    I did adjust the code to re-use the same pointer and initialised the length.

    >> you must put the max buffer size as a WORD in the buffer you send in

    I'm not sure what you mean, but I am aware of the null terminator. I may not know whether a different call ignores that fact.
    I've tried to the best of my knowledge to implement all the suggestions and here's what I got:
    Code:
    CEdit* pGetEdit;
    	i=0;
    
    	int len = 1;	
    	
    	do
    	{
    		pGetEdit = (CEdit*) GetDlgItem(i+100);
    		
    		len = pGetEdit->GetWindowTextLength();
    
    		if(len > 0)
    		{
    			pGetEdit->GetWindowText(templink.ParamName);
    			vars.vectParmSelected.push_back(templink);
    		}
    	
    		i++;
    	}
    	while(i<FAD.nExpected);
    and I still get the run time error when I have an empty field.

  5. #5
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>This parameter is ignored by a single-line edit control.

    No matter what size the edit appears on the screen, nor how many 'lines' it is displaying it is still a single line edit unless you specified the ES_MULTILINE style when you created it.


    >>and I still get the run time error when I have an empty field.
    Code:
    if(len > 0)
    {
    //blah blah...
    }
    else // len==0
    {
           //try something like.....
           templink.ParamName[0]='\0';//empty string
           vars.vectParmSelected.push_back(templink);//what you do normally
    }
    "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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. I'd like to load a text file's contents cleanly at compile time
    By CharlieLewaska in forum C Programming
    Replies: 3
    Last Post: 01-31-2006, 11:10 AM
  3. Replies: 3
    Last Post: 05-25-2005, 01:50 PM
  4. Ok, Structs, I need help I am not familiar with them
    By incognito in forum C++ Programming
    Replies: 7
    Last Post: 06-29-2002, 09:45 PM
  5. Problem with static text in dialog boxes
    By Clyde in forum Windows Programming
    Replies: 11
    Last Post: 05-28-2002, 12:51 PM