| View previous topic :: View next topic |
| Author |
Message |
TraxMate Master Cheater
Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Sun Feb 08, 2009 5:37 pm Post subject: [C++] Listbox |
|
|
I want to add every letter in the alphabet to a listbox but I don't want to write every letter in it's own SendDlgItemMessage() so I tried to use a loop but it's just crashing the app. | Code: | LPCWSTR lpAlphabet = _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for(int i = 0; i < lstrlen(lpAlphabet); i++)
SendDlgItemMessage(hParent, IDC_KEYLIST, LB_ADDSTRING, 0, (LPARAM)lpAlphabet[i]); |
Any ideas why that crashes but not this? | Code: | LPCWSTR lpAlphabet = _T("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
for(int i = 0; i < lstrlen(lpAlphabet); i++)
SendDlgItemMessage(hParent, IDC_KEYLIST, LB_ADDSTRING, 0, (LPARAM)lpAlphabet); |
|
|
| Back to top |
|
 |
slippppppppp Grandmaster Cheater
Reputation: 0
Joined: 08 Aug 2006 Posts: 929
|
Posted: Sun Feb 08, 2009 6:40 pm Post subject: |
|
|
char* lpAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for( int i = 0; i <= lstrlen(lpAlphabet); i++ )
SendDlgItemMessage(hParent, IDC_KEYLIST, LB_ADDSTRING, 0, lpAlphabet[i]);
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Sun Feb 08, 2009 6:43 pm Post subject: |
|
|
It needs to be null terminated. You are adding a string after all. Here is how I would of done it.
| Code: |
HWND hWnd = GetDlgItem(hWndDlg,IDC_LIST1);
TCHAR lpAlphabet[] = TEXT("A");
for (int i = 'A'; i <= 'Z'; i++)
{
lpAlphabet[0] = i;
SendMessage(hWnd, LB_ADDSTRING, 0, (LPARAM)lpAlphabet);
}
|
|
|
| Back to top |
|
 |
TraxMate Master Cheater
Reputation: 0
Joined: 01 Mar 2008 Posts: 363
|
Posted: Mon Feb 09, 2009 12:15 am Post subject: |
|
|
| Thanks killersamurai. It works.
|
|
| Back to top |
|
 |
|