By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,745 Members | 1,873 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,745 IT Pros & Developers. It's quick & easy.

GrabTextFromEdit()

P: 48
Expand|Select|Wrap|Line Numbers
  1. char* GrabTextFromEdit(int nResourceID, HWND hwndMain) 
  2. {
  3.     int len = GetWindowTextLength(GetDlgItem(hwndMain, nResourceID));
  4.     if (len > 0) 
  5.     {
  6.         char* buffer;
  7.         buffer = (char*)GlobalAlloc(GPTR, len + 1);
  8.         GetDlgItemText(hwndMain, nResourceID, buffer, len + 1);
  9.         return buffer;
  10.     }
  11. }
  12.  
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.
Jun 18 '07 #1
Share this Question
Share on Google+
8 Replies


P: 48
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.

Expand|Select|Wrap|Line Numbers
  1. void AddRow(HWND hwnd)
  2. {
  3.     bool con=true;
  4.     char* str;
  5.     try{ 
  6.     str=GrabTextFromEdit(902, hwnd); 
  7.     if( str == 0 )
  8.          throw "Memory allocation failure!";
  9.     else
  10.         printf("add new row");
  11.     }
  12.     catch(...){
  13.     con=false;
  14.     printf("\n NULL CHAR \n  ,");
  15.     }
  16.  
  17.  
  18.     if(con)
  19.         printf(str);
  20.  
  21.     bool resFound=false;
  22.     if(GetDlgItem(hwnd, 902)==0)
  23.     {
  24.         printf(" resource not found ");
  25.         resFound=false;
  26.     }    
  27.     else
  28.     {
  29.         printf(" resource found ");
  30.         resFound=true;
  31.     }
  32.  
  33.     if(resFound && !con)
  34.     {
  35.         printf("\n empty field \n");
  36.     }
  37.  
  38. }
  39.  
Jun 18 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
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;
}
}
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.
Jun 18 '07 #3

Savage
Expert 100+
P: 1,764
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
Jun 18 '07 #4

weaknessforcats
Expert Mod 5K+
P: 9,197
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.
Jun 18 '07 #5

P: 48
This looks better:

Expand|Select|Wrap|Line Numbers
  1. char* GrabTextFromEdit(int nResourceID, HWND hwndMain) 
  2. {
  3.     HWND temp=GetDlgItem(hwndMain, nResourceID);
  4.     if(temp==0)
  5.         return "(none)";//resource not found
  6.     int len = GetWindowTextLength(temp);
  7.     if (len > 0) 
  8.     {
  9.         char* buffer;
  10.         buffer = (char*)GlobalAlloc(GPTR, len + 1);
  11.         GetDlgItemText(hwndMain, nResourceID, buffer, len + 1);
  12.         return buffer;
  13.     }
  14.     return "(empty)";
  15. }
  16.  
  17.  
  18. void AddRow(HWND hwnd)
  19. {//check next resource to see if its there.  if resource found then check next resource etc. if not found add the resource.  
  20. ////if resource is empty do nothing.
  21.     char* str=GrabTextFromEdit(903, hwnd); 
  22.  
  23.     if(lstrcmp(str, "(none)") == 0)
  24.         printf("resource not found: So add it\n");
  25.     else if(lstrcmp(str, "(empty)") == 0)
  26.         printf("empty field\n");
  27.     else
  28.         printf("Found: %s : Check next resource if we can Add\n", str);
  29. }
  30.  
Jun 18 '07 #6

P: 48
*shrug* it compiles. Its c++ to my understanding.
Jun 18 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
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)"; <<<<< 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.
Jun 19 '07 #8

P: 48
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.
Jul 2 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.