Results 1 to 8 of 8

Thread: counting and then sending a message

  1. #1

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    counting and then sending a message

    I have added a list box on a dialog box with the name "IDLIST"
    Now I want to change the text of that dialog when the item(s) are selected in the list box. I used the following code:

    Code:
    int count;
    HWND hlist = GetDlgItem(hwnd, IDCLIST);
    count = SendMessage(hlist, LB_GETSELCOUNT, 0,0);
    int *buffer1 = GlobalAlloc(GPTR, sizeof(int) * count);
    SendMessage(hlist, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buffer1);
    SetWindowText(hwnd, buffer1);
    GlobalFree(buffer1);
    But it gives me these errors:

    cannot convert from 'void *' to 'int *'
    error C2664: 'SetWindowTextA' : cannot convert parameter 2 from 'int *' to 'const char *'
    Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
    error C2440: '=' : cannot convert from 'void *' to 'char *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast


    Do you know why its giving me these errors and how can I fix them.
    Baaaaaaaaah

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    int *buffer1 = GlobalAlloc(GPTR, sizeof(int) * count);
    SendMessage(hlist, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buffer1);
    SetWindowText(hwnd, buffer1);
    GlobalFree(buffer1);
    Remember how C++ won't let you arbitrarily assign void* pointers to things? It happens here By the way, you should use new[]/delete[] rather than the Global functions.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    But What is the correct code you use

    And
    Remember how C++ won't let you arbitrarily assign void* pointers to things
    I did not understand what you meant by that.
    Baaaaaaaaah

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    In the other thread I explained about the whole int* = void* C/C++ thing.

    I'm too tired to do any code now, but you need to use int* buffer1 = new int[count] and delete[] buffer1 in there instead of GlobalAlloc/GlobalFree.

    Also, you need to convert the integers to a string first (look around on the forum).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827
    I am unfimiliar with new and delete thingies.

    When I use this function:
    Code:
    SendMessage(hlist, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buffer1);
    Will it return me the value as an integer or a string (like all the text currently selected)

    If it returns as string then why not I just use something like this:

    Code:
    int count;
    HWND hlist = GetDlgItem(hwnd, IDCLIST);
    count = SendMessage(hlist, LB_GETSELCOUNT, 0,0);
    char* buffer1 = (char*)new int[count];
    SendMessage(hlist, LB_GETSELITEMS, (WPARAM)count, (LPARAM)buffer1);
    SetWindowText(hwnd, buffer1);
    delete[] buffer1;
    return 0;
    It runs fine. But when I select something from the listbox, it gives me an error message. I have included the picture of that error message box in that attachment.
    Attached Images Attached Images  
    Baaaaaaaaah

  6. #6
    denniswrenn
    Guest
    Are you using a multiple selection List Box, or a single selection listbox? If it's the 2nd choice, then you're going about this all wrong.

  7. #7

    Thread Starter
    PowerPoster abdul's Avatar
    Join Date
    Dec 2000
    Location
    Ontario,Canada
    Posts
    2,827

    It is a single selection listbox

    Yes it is the second choice but I thought that it would work with both of them(single and muliselection) because if there is just one selection then it may count just 1.

    Am I doing rong. If yes, then how do I do the other way?
    Baaaaaaaaah

  8. #8
    denniswrenn
    Guest
    Took me a while to whip up this bit of code, but it works
    Code:
    		int count = 0;
    		int i = 0;
    		int sel = 0;
    		int textlen = 0;
    		TCHAR* str;
    		HWND hlist = GetDlgItem(hwnd, IDCLIST);
    
    		count = SendMessage(hList, LB_GETCOUNT,0, 0);
    		for(i = 0; i < count; i++)
    		{
    			if(SendMessage(hList,LB_GETSEL,i, 0) > 0)
    			{
    				sel = i;
    				break;
    			}
    		}
    		textlen = SendMessage(hList,LB_GETTEXTLEN,sel, 0);
    		str = new TCHAR[textlen];
    		SendMessage(hList, LB_GETTEXT, sel, (long)str);
    		SetWindowText(hwnd, str);
    Don't have much experience using delete[], so you'll have to add that in there yourself...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width