GrabTextFromEdit()

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • qhimq
    New Member
    • May 2007
    • 48

    GrabTextFromEdit()

    Code:
    char* GrabTextFromEdit(int nResourceID, HWND hwndMain) 
    {
        int len = GetWindowTextLength(GetDlgItem(hwndMain, nResourceID));
        if (len > 0) 
    	{
            char* buffer;
            buffer = (char*)GlobalAlloc(GPTR, len + 1);
            GetDlgItemText(hwndMain, nResourceID, buffer, len + 1);
            return buffer;
        }
    }
    Hi,

    If use this function to get text from an edit control made from a dialog that is empty, I get what I want, a char* filled with "(null)". I thought that was nice, but then when I use this same function to get text from an edit control made on runtime (createwindow) then I get a null pointer as a return.

    Have any ideas on how to recognize if the edit box is empty?

    Sure, I could use the catch/try, but, long story short, in this situation I don't want to use it.
  • qhimq
    New Member
    • May 2007
    • 48

    #2
    ah, Ok i figured it out.

    What i'm attempting to do, is once the user inputs a value in the textbox and presses enter, the program adds another row of inputs and stuff. I am programming it to add an infinite amount of inputs, so I figured that I would just have the text box resource number be added by 1 each new edit box, and be calculated it's position by the preceding resource number's position.

    So I have it attempt to get the text from a textbox that might exist. Then it checks if the resource exists. From that we can find out whether the textbox is either non-existent in the resources or existent in the resources but empty.

    I don't have anything complete but here is the foundation.

    Code:
    void AddRow(HWND hwnd)
    {
    	bool con=true;
    	char* str;
    	try{ 
    	str=GrabTextFromEdit(902, hwnd); 
    	if( str == 0 )
             throw "Memory allocation failure!";
    	else
    		printf("add new row");
    	}
    	catch(...){
    	con=false;
    	printf("\n NULL CHAR \n  ,");
    	}
    	
    	
    	if(con)
    		printf(str);
    	
    	bool resFound=false;
    	if(GetDlgItem(hwnd, 902)==0)
    	{
    		printf(" resource not found ");
    		resFound=false;
    	}	
    	else
    	{
    		printf(" resource found ");
    		resFound=true;
    	}
    	
    	if(resFound && !con)
    	{
    		printf("\n empty field \n");
    	}
    	
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Originally posted by qhimq
      char* GrabTextFromEdi t(int nResourceID, HWND hwndMain)
      {
      int len = GetWindowTextLe ngth(GetDlgItem (hwndMain, nResourceID));
      if (len > 0)
      {
      char* buffer;
      buffer = (char*)GlobalAl loc(GPTR, len + 1);
      GetDlgItemText( hwndMain, nResourceID, buffer, len + 1);
      return buffer;
      }
      }
      Not all paths return a char*. If len is not greater than 0, nothing is returned. How are you getting this to compile???

      Also, since this is C you should be using malloc() rather than GlobalAlloc() which ius much slower.

      Comment

      • Savage
        Recognized Expert Top Contributor
        • Feb 2007
        • 1759

        #4
        Originally posted by weaknessforcats
        Not all paths return a char*. If len is not greater than 0, nothing is returned. How are you getting this to compile???

        Also, since this is C you should be using malloc() rather than GlobalAlloc() which ius much slower.
        I'm not sure that he is uing C,look in his last post.There is try{ and }catch things which are specific to C++..

        Savage

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Originally posted by Savage
          I'm not sure that he is uing C,look in his last post.There is try{ and }catch things which are specific to C++..
          Yeah, I saw that. I also saw the typecast in GlobalAlloc() and the fact that everything else is in C.

          In fact, try/catch might not work. Microsoft uses structured exceptions __try/__except and there's no throw. You use RaiseException( ) instead.

          Comment

          • qhimq
            New Member
            • May 2007
            • 48

            #6
            This looks better:

            Code:
            char* GrabTextFromEdit(int nResourceID, HWND hwndMain) 
            {
            	HWND temp=GetDlgItem(hwndMain, nResourceID);
            	if(temp==0)
            		return "(none)";//resource not found
                int len = GetWindowTextLength(temp);
                if (len > 0) 
            	{
                    char* buffer;
                    buffer = (char*)GlobalAlloc(GPTR, len + 1);
                    GetDlgItemText(hwndMain, nResourceID, buffer, len + 1);
                    return buffer;
                }
            	return "(empty)";
            }
            
            
            void AddRow(HWND hwnd)
            {//check next resource to see if its there.  if resource found then check next resource etc. if not found add the resource.  
            ////if resource is empty do nothing.
            	char* str=GrabTextFromEdit(903, hwnd); 
            	
            	if(lstrcmp(str, "(none)") == 0)
            		printf("resource not found: So add it\n");
            	else if(lstrcmp(str, "(empty)") == 0)
            		printf("empty field\n");
            	else
            		printf("Found: %s : Check next resource if we can Add\n", str);
            }

            Comment

            • qhimq
              New Member
              • May 2007
              • 48

              #7
              *shrug* it compiles. Its c++ to my understanding.

              Comment

              • weaknessforcats
                Recognized Expert Expert
                • Mar 2007
                • 9214

                #8
                Originally posted by qhimq
                char* GrabTextFromEdi t(int nResourceID, HWND hwndMain)
                {
                HWND temp=GetDlgItem (hwndMain, nResourceID);
                if(temp==0)
                return "(none)";//resource not found
                int len = GetWindowTextLe ngth(temp);
                if (len > 0)
                {
                char* buffer;
                buffer = (char*)GlobalAl loc(GPTR, len + 1);
                GetDlgItemText( hwndMain, nResourceID, buffer, len + 1);
                return buffer;
                }
                return "(empty)"; <<<<< 0 ???
                }
                Might I suggest you return a zero rather than a pointer to a literal "empty"?

                Whereever you use the return from this function you will have to compare "empty" to the char* using something like strcmp(). By returning a 0 all you need do is check the pointer for null before using it.

                Comment

                • qhimq
                  New Member
                  • May 2007
                  • 48

                  #9
                  I did that the first time, however I didn't want to confuse myself with that later.

                  Because I need to know if the resource is either empty or non-existent. These two cases are completely different for my program flow.

                  Comment

                  Working...