Thread: Knowing if a listbox entry was selected?

  1. #1
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477

    Knowing if a listbox entry was selected?

    Hey there.

    I tried searching, but to no avail as specifying the search query is a bit hard in this case..

    Anyway, I have a listbox with dynamic content that's read from a file, along with some inactive edit controls that are supposed to be modified by the application depending on which entry is selected.

    I do know how I can use the LB_GETSEL message and so on, but I was wondering how I could know if an entry was selected? I used Spy++ from Visual Studio, but couldn't really get any relevant messages that weren't just generic Mousedown/up.

    How can I know if an entry in the listbox was selected so I can retrieve the contents of it?

    All help greatly appreciated.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Look at the LBN_SELCHANGE notification message.

  3. #3
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Thanks for that. I got it working.

    I have, however, run into another problem.

    I'm trying to dynamically allocate an array, and I was initially allocating the memory when a button was clicked and then freeing it on the WM_CLOSE message.

    However, when I run the program(In debug mode, using VS 2008) and then close the dialog window, it gives me an error about the handle pointer being uninitialized, despite the fact I allocate memory for it, after which it gives me access violation errors where some debug, heap-checking function tries to read the memory and can't.

    I've also noticed that the at the point where the memory gets freed(before it's passed) the pointer contains 0xcccccccc, instead of the address it was assigned from malloc which I can see if I break after malloc.

    Here's the code(of the Dialogproc)

    Code:
    BOOL CALLBACK DlgProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	SomeHandle *rfidHandle;
    
    
    
    	switch(Message)
    	{
    		case WM_INITDIALOG:
    		{
    			int ret;
    
    			if((ret = ReadSerialFile(hWnd)) == ERROR_OCCURRED)
    			{
    				MessageBox(hWnd, SERIAL_FILE_NOT_FOUND, "File not found!", MB_OK|MB_ICONWARNING);
    
    				break;
    			}
    		
    
    			return TRUE;
    		}
    		
    		
    		case WM_CLOSE:
    		{
                            free(rfidHandle);
                            rfidHandle = NULL;
    			EndDialog(hWnd, 0);
    			
    			break;
    		}
    
    
    		case WM_COMMAND:
    		{
    			switch(LOWORD(wParam))
    			{
    				case IDC_REREADSERIALS:
    				{
    					int ret;
    					SendDlgItemMessage(hWnd, IDC_SERIALS, LB_RESETCONTENT, 0, 0);
    					
    					if((ret = ReadSerialFile(hWnd)) == ERROR_OCCURRED)
    						MessageBox(hWnd, SERIAL_FILE_NOT_FOUND, "Not Found!", MB_OK|MB_ICONWARNING);
    
    
    					break;
    				}
    				
    
    				case IDC_SERIALS:
    				{
    					switch(HIWORD(wParam))
    					{
    						case LBN_SELCHANGE:
    						{
    							unsigned int serialNumber = 0, selPos = 0;
    
    							selPos = SendDlgItemMessage(hWnd, IDC_SERIALS, LB_GETCURSEL, 0, 0);
    
    							serialNumber = SendDlgItemMessage(hWnd, IDC_SERIALS, LB_GETITEMDATA, (WPARAM)selPos, 0);
    
    							SetDlgItemInt(hWnd, IDC_SERIALEDIT, serialNumber, 0);
    
    							break;
    						}
    
    						default:
    							break;
    
    					}
    
    					break;
    				}
    
    				case IDC_ATTACH:
    				{
    					int noListBoxEntries = SendDlgItemMessage(hWnd, IDC_SERIALS, LB_GETCOUNT, 0, 0);
    
    					rfidHandle = malloc(noListBoxEntries * sizeof(SomeHandle));
    					
    					break;
    				}
    				
    
    
    				default:
    					return FALSE;
    			}
    
    			break;
    		}
    
    		default:
    			return FALSE;
    
    	}
    
    
    
    	return TRUE;	
    }
    Last edited by IceDane; 04-03-2008 at 02:05 PM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But you know, rfidHandle isn't static so it will be destroyed when WindowProc returns so when WM_CLOSE is received, you'll be trying to free an unitialized pointer.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Ex scientia vera
    Join Date
    Sep 2007
    Posts
    477
    Quote Originally Posted by Elysia View Post
    But you know, rfidHandle isn't static so it will be destroyed when WindowProc returns so when WM_CLOSE is received, you'll be trying to free an unitialized pointer.
    Ah, sweet, thanks for that.

    Made it static and it worked. I didn't think of that.. Never really used static variables before.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Static variables are variables who retain their value in the function they're defined until the program end. But they can only be seen inside their respective functions, so they're like global variables, yet unlike them. You probably want to initialize your pointer to NULL anyway and up your warnings to max.
    On level 4, visual studio might give a warning about using a possibly uninitialized variable.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Listbox search
    By Elsindes in forum Windows Programming
    Replies: 4
    Last Post: 04-09-2007, 03:47 PM
  2. Binary Search Tree
    By penance in forum C Programming
    Replies: 4
    Last Post: 08-05-2005, 05:35 PM
  3. How to cast a ListBox item to an int for a switch statment?
    By Swaine777 in forum C++ Programming
    Replies: 8
    Last Post: 09-26-2004, 08:52 PM
  4. Getting FULL filename from listbox
    By Garfield in forum Windows Programming
    Replies: 8
    Last Post: 01-27-2002, 08:28 AM
  5. Getting selected item in ListBox?
    By BubbleMan in forum Windows Programming
    Replies: 1
    Last Post: 09-19-2001, 06:23 PM