Home

Windows Controls: The List Box

   

Introduction

These are the steps to use a list box in a Microsoft Windows application. This exercise uses Microsoft Visual C++.

 

Practical LearningPractical Learning: Introducting the Date Time Picker

  1. Start Microsoft Visual Studio or Visual C++
  2. On the main menu, click File -> New Project...
  3. In the left list, click Visual C++ Project.
    In the right list, click Win32 Project
  4. Set the Name to ListBoxControl
  5. Click OK
  6. In the first page of the wizard, click Next
  7. In the second page, click Windows Application and click Empty Project
     
    Win32 Application Wizard
  8. Click Finish

Practical LearningPractical Learning: Creating a Resource Header

  1. On the main menu, click Project -> Add New Item...
  2. In the right list, click Header File
  3. Set the Name to Resource
  4. Click Add
  5. In the empty document, type: #define IDD_PRIMARYFAMILY_DLG 1012 and press Enter

Practical LearningPractical Learning: Creating a Resource File

  1. On the main menu, click Project -> Add New Item...
  2. In the right list, click Text File
  3. Set the Name to Exercise.rc (make sure you include the extension)
  4. Click Add
  5. In the Solution Explorer, right-click Exercise.rc and click Open With...
  6. In the Open With dialog box, click Source Code (Text) Editor
     
    Open With
  7. Click OK
  8. In the empty document, type the following:
    #include <afxres.h>
    #include "Resource.h"
    
    IDD_PRIMARYFAMILY_DLG DIALOG 260, 200, 110, 105
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Family Affairs"
    FONT 8, "MS Shell Dlg"
    BEGIN
        DEFPUSHBUTTON   "OK", IDOK, 48, 84, 50, 14
    END

Practical LearningPractical Learning: Creating the Source Code

  1. On the main menu, click Project -> Add New Item...
  2. In the right list, click C++ File (.cpp)
  3. Set the Name to Exercise
  4. Click Add
  5. In the empty document, type the following:
    #include <windows.h>
    #include "Resource.h"
    
    //---------------------------------------------------------------------------
    HWND hWnd;
    HINSTANCE hInstGlobal;
    LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	hInstGlobal = hInstance;
    
    	DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_PRIMARYFAMILY_DLG),
    	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    
    		return TRUE;
    
    	case WM_COMMAND:
    		switch(wParam)
    		{
    		case IDOK:
    			EndDialog(hWndDlg, 0);
    			return TRUE;
    		}
    		break;
    
    	case WM_CLOSE:
                PostQuitMessage(WM_QUIT);
            break;
    	}
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
  6. Execute the application to see the result
     
    List Box
  7. Close the dialog box and return to your programming environment
 
 
 

Practical LearningPractical Learning: Programmatically Creating the Control

  1. Change the Exercise.cpp source file as follows:
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    		CreateWindowEx(0,
                    	       L"LISTBOX",
                           	       TEXT("FamilyListing"),
                           	       WS_BORDER | WS_CHILD | WS_VISIBLE,
                           	       10,10,140,120,
    			       hWndDlg,
                           	       NULL,
                                   hInstGlobal,
                                   NULL);
    		return TRUE;
    
    	case WM_COMMAND:
    		switch(wParam)
    		{
    		case IDOK:
    			EndDialog(hWndDlg, 0);
    			return TRUE;
    		}
    		break;
    
    	case WM_CLOSE:
                PostQuitMessage(WM_QUIT);
            break;
    	}
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
  2. Execute the application to see the result
     
    List Box
  3. Close the dialog box and return to your programming environment

Practical LearningPractical Learning: Manually Creating a List Box

  1. Access the Resource.h file
  2. Add the following line at the end and press Enter:
    #define IDC_PRIMARYFAMILY_LB  1014
  3. Access the Exercise.
  4. rc file and change it as follows:
    #include <afxres.h>
    #include "Resource.h"
    
    IDD_PRIMARYFAMILY_DLG DIALOG 260, 200, 110, 105
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Family Affairs"
    FONT 8, "MS Shell Dlg"
    BEGIN
        DEFPUSHBUTTON   "OK", IDOK, 48, 84, 50, 14
        LISTBOX         IDC_PRIMARYFAMILY_LB, 10, 10, 90, 80
    END
    
  5. Access the Exercise.cpp
  6. Delete the code in the WM_INITDIALOG event:
    #include windows.h
    #include "Resource.h"
    
    //---------------------------------------------------------------------------
    HWND hWnd;
    HINSTANCE hInstGlobal;
    LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	hInstGlobal = hInstance;
    
    	DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_PRIMARYFAMILY_DLG),
    	          hWnd, reinterpret_cast(DlgProc));
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
    	switch(msg)
    	{
    	case WM_INITDIALOG:
    		
    		return TRUE;
    
    	case WM_COMMAND:
    		switch(wParam)
    		{
    		case IDOK:
    			EndDialog(hWndDlg, 0);
    			return TRUE;
    		}
    		break;
    
    	case WM_CLOSE:
                PostQuitMessage(WM_QUIT);
            break;
    	}
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
  7. Execute the application to see the result
  8. Close the dialog box and return to your programming environment

Practical LearningPractical Learning: Adding Items to the List Box

  1. Change the WN the Exercise.rc file and change it as follows (it also shows different ways to add an item):
    #include <windows.h>
    #include <Windowsx.h>
    #include "Resource.h"
    
    //---------------------------------------------------------------------------
    HWND hWnd;
    HINSTANCE hInstGlobal;
    LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
    //---------------------------------------------------------------------------
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    				   LPSTR lpCmdLine, int nCmdShow)
    {
    	hInstGlobal = hInstance;
    
    	DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_PRIMARYFAMILY_DLG),
    	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));
    
    	return FALSE;
    }
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HWND lbxPrimaryFamily;
    	
        lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
    
        switch(msg)
        {
        case WM_INITDIALOG:
    		
    	ListBox_AddString(lbxPrimaryFamily, L"Father");
    	ListBox_AddString(lbxPrimaryFamily, L"Mother");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)L"Daughter");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Son"));
    	SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Bother"));
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Sister"));
    	return TRUE;
    
        case WM_COMMAND:
    	switch(wParam)
    	{
    	case IDOK:
    	    EndDialog(hWndDlg, 0);
    	    return TRUE;
    	}
    	break;
    
        case WM_CLOSE:
            PostQuitMessage(WM_QUIT);
            break;
        }
    
        return FALSE;
    }
    //---------------------------------------------------------------------------
  2. Execute the application to see the result
     
    List Box
  3. Close the dialog box and return to your programming environment

Practical LearningPractical Learning: Handling Events

  1. Access the Exercise.
  2. rc file
  3. To prepare to handle events, change the file as follows:
    #include <afxres.h>
    #include "Resource.h"
    
    IDD_PRIMARYFAMILY_DLG DIALOG 260, 200, 110, 105
    STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
    CAPTION "Family Affairs"
    FONT 8, "MS Shell Dlg"
    BEGIN
        DEFPUSHBUTTON   "OK", IDOK, 48, 84, 50, 14
        LISTBOX         IDC_PRIMARYFAMILY_LB, 10, 10, 90, 80, LBS_NOTIFY
    END
    
  4. Access the Exercise.cpp
  5. To handle events, change the procedure as follows:
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HWND lbxPrimaryFamily;
    	
        lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
    
        switch(msg)
        {
        case WM_INITDIALOG:
    		
    	ListBox_AddString(lbxPrimaryFamily, L"Father");
            ListBox_AddString(lbxPrimaryFamily, L"Mother");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)L"Daughter");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Son"));
    	SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
    	 	   (LPARAM)(LPCTSTR)TEXT("Bother"));
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Sister"));
    	return TRUE;
    
        case WM_COMMAND:
    	switch(LOWORD(wParam))
    	{
    	case IDC_PRIMARYFAMILY_LB:
    		{
    		switch (HIWORD(wParam))
    			
    		return TRUE;
    
    	case IDOK:
    		EndDialog(hWndDlg, 0);
    		return TRUE;
    	}
    	break;
    
        case WM_CLOSE:
            PostQuitMessage(WM_QUIT);
            break;
        }
    
        return FALSE;
    }
    //---------------------------------------------------------------------------

Practical LearningPractical Learning: Double-Clicking an Item

  1. When the user double-clicks an item, to get the index of the item that was double-clicked, type the following:
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HWND lbxPrimaryFamily;
    	
        lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
    
        switch(msg)
        {
        case WM_INITDIALOG:
    		
            ListBox_AddString(lbxPrimaryFamily, L"Father");
            ListBox_AddString(lbxPrimaryFamily, L"Mother");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)L"Daughter");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Son"));
    	SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
    	 	   (LPARAM)(LPCTSTR)TEXT("Bother"));
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Sister"));
    	return TRUE;
    
        case WM_COMMAND:
    	switch(LOWORD(wParam))
    	{
    	case IDC_PRIMARYFAMILY_LB:
    	{
    	    switch (HIWORD(wParam))
    	    {
    		case LBN_DBLCLK:
    		    int iSelectedIndex;
    				
    		    iSelectedIndex = ListBox_GetCurSel(lbxPrimaryFamily);
    		    return TRUE;
    	    }
    	}
    	return TRUE;
    
    	case IDOK:
    	    EndDialog(hWndDlg, 0);
    	    return TRUE;
    	}
            break;
    
        case WM_CLOSE:
            PostQuitMessage(WM_QUIT);
            break;
        }
    
        return FALSE;
    }
    //---------------------------------------------------------------------------
  2. To get the text of the item that was double-clicked, change the code as follows:
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HWND lbxPrimaryFamily;
    	
        lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
    
        switch(msg)
        {
        case WM_INITDIALOG:
    		
            ListBox_AddString(lbxPrimaryFamily, L"Father");
            ListBox_AddString(lbxPrimaryFamily, L"Mother");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)L"Daughter");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Son"));
    	SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
    	 	   (LPARAM)(LPCTSTR)TEXT("Bother"));
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Sister"));
    	return TRUE;
    
        case WM_COMMAND:
    	switch(LOWORD(wParam))
    	{
    	case IDC_PRIMARYFAMILY_LB:
    	{
    	    switch (HIWORD(wParam))
    	    {
    	    case LBN_DBLCLK:
    	        int iSelectedIndex;
    		TCHAR strSelectedItem[32];
    				
    		iSelectedIndex = ListBox_GetCurSel(lbxPrimaryFamily);
                    ListBox_GetText(lbxPrimaryFamily, iSelectedIndex, (LPCTSTR)strSelectedItem);
    
    		MessageBox(NULL, strSelectedItem, L"Exercise", 0);
    		return TRUE;
    	    }
    	}
    	return TRUE;
    
    	case IDOK:
    	    EndDialog(hWndDlg, 0);
    	    return TRUE;
    	}
            break;
    
        case WM_CLOSE:
            PostQuitMessage(WM_QUIT);
            break;
        }
    
        return FALSE;
    }
    //---------------------------------------------------------------------------
  3. Execute the application to see the result
  4. Close the dialog box and return to your programming environment

Practical LearningPractical Learning: Deleting an Item

  1. When the user double-clicks an item, to delete that item, type the following:
    //---------------------------------------------------------------------------
    LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        HWND lbxPrimaryFamily;
    	
        lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);
    
        switch(msg)
        {
        case WM_INITDIALOG:
    		
            ListBox_AddString(lbxPrimaryFamily, L"Father");
            ListBox_AddString(lbxPrimaryFamily, L"Mother");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)L"Daughter");
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Son"));
    	SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
    	 	   (LPARAM)(LPCTSTR)TEXT("Bother"));
            SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0,
             	   (LPARAM)(LPCTSTR)TEXT("Sister"));
    	return TRUE;
    
        case WM_COMMAND:
    	switch(LOWORD(wParam))
    	{
    	case IDC_PRIMARYFAMILY_LB:
    	{
    	    switch (HIWORD(wParam))
    	    {
    	    case LBN_DBLCLK:
    		// Getting the index of the item that was double-clicked
    		int iSelectedIndex;
    				
    		iSelectedIndex = ListBox_GetCurSel(lbxPrimaryFamily);
    
    		// Identifying the string that was double-clicked
    		/*
    		TCHAR strSelectedItem[32];
    		ListBox_GetText(lbxPrimaryFamily,
    			        iSelectedIndex,
    			        (LPCTSTR)strSelectedItem);
    
    		MessageBox(NULL, strSelectedItem, L"Exercise", 0);*/
    
    		// Deleting the item that was double-clicked
    		ListBox_DeleteString(lbxPrimaryFamily, iSelectedIndex);
    
    		return TRUE;
    	    }
    	}
    	return TRUE;
    
    	case IDOK:
    	    EndDialog(hWndDlg, 0);
    	    return TRUE;
    	}
            break;
    
        case WM_CLOSE:
            PostQuitMessage(WM_QUIT);
            break;
        }
    
        return FALSE;
    }
    //---------------------------------------------------------------------------
  2. Execute the application to see the result
  3. Double-click an item in the list box
  4. Close the dialog box and return to your programming environment
 
 
   
 

Home  

 

Resource Header:

#define IDD_PRIMARYFAMILY_DLG 1012
#define IDC_PRIMARYFAMILY_LB  1014

Resource File:

#include <afxres.h>
#include "Resource.h"

IDD_PRIMARYFAMILY_DLG DIALOG 260, 200, 110, 105
STYLE DS_MODALFRAME | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "Family Affairs"
FONT 8, "MS Shell Dlg"
BEGIN
    DEFPUSHBUTTON   "OK", IDOK, 48, 84, 50, 14
	LISTBOX         IDC_PRIMARYFAMILY_LB, 10, 10, 90, 80, LBS_NOTIFY
END

Source Code:

#include <windows.h>
#include <Windowsx.h>
#include "Resource.h"

//---------------------------------------------------------------------------
HWND hWnd;
HINSTANCE hInstGlobal;
LRESULT CALLBACK DlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine, int nCmdShow)
{
	hInstGlobal = hInstance;

	DialogBox(hInstGlobal, MAKEINTRESOURCE(IDD_PRIMARYFAMILY_DLG),
	          hWnd, reinterpret_cast<DLGPROC>(DlgProc));

	return FALSE;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK DlgProc(HWND hWndDlg, UINT msg, WPARAM wParam, LPARAM lParam)
{
    HWND lbxPrimaryFamily;
	
    lbxPrimaryFamily = GetDlgItem(hWndDlg, IDC_PRIMARYFAMILY_LB);

    switch(msg)
    {
    case WM_INITDIALOG:
		
        ListBox_AddString(lbxPrimaryFamily, L"Father");
        ListBox_AddString(lbxPrimaryFamily, L"Mother");
        SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)L"Daughter");
        SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Son"));
	    SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Bother"));
        SendMessage(lbxPrimaryFamily, LB_ADDSTRING, 0, (LPARAM)(LPCTSTR)TEXT("Sister"));
	return TRUE;

    case WM_COMMAND:
	switch(LOWORD(wParam))
	{
	case IDC_PRIMARYFAMILY_LB:
	{
	    switch (HIWORD(wParam))
	    {
		case LBN_DBLCLK:
		    int iSelectedIndex;
		    TCHAR strSelectedItem[32];
				
		    iSelectedIndex = ListBox_GetCurSel(lbxPrimaryFamily);
		    ListBox_GetText(lbxPrimaryFamily, iSelectedIndex, (LPCTSTR)strSelectedItem);

		    MessageBox(NULL, strSelectedItem, L"Exercise", 33);
		    return TRUE;
	    }
	}
	return TRUE;

	case IDOK:
	    EndDialog(hWndDlg, 0);
	    return TRUE;
	}
        break;

    case WM_CLOSE:
        PostQuitMessage(WM_QUIT);
        break;
    }

    return FALSE;
}
//---------------------------------------------------------------------------