Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

Chapter 20: List-Based Controls Visual C++ and MFC Fundamentals



  1. Execute the program to test it. Double-click various items in the Sample Fields list
    box and observe the behavior.

  2. After using it, close the dialog box and return to MSVC

  3. Add a BN_CLICKED Event Handler for the >> button and name it OnSelectAll

  4. We need to let the user select all items in the Sample Fields list and copy them to the
    Selected Fields list. As previously, we need to avoid duplicating the names of fields
    in the same table. Therefore, to add the items to the list, first select one at a time,
    look for it in the Selected Fields list. If it doesn't exist, then add it. If it already exists,
    ignore it and consider the next item. This behavior can be performed in a for loop
    that is used to navigate the items in an array.
    Implement the OnSelectAll() event as follows:


void CTableWizardDlg::OnSelectAll()
{
// TODO: Add your control notification handler code here
// This string will represent an item in the Sample Fields list
CString Item;

// This "for" loop will be used to navigate the Sample Fields list
for(int i = 0; i < m_SampleFields.GetCount(); i++)
{
// Consider an item in the Sample Fields list
// Store it in the above declared Item variable
m_SampleFields.GetText(i, Item);

// Look for the item in the Selected Fields list
// Make sure you start at the beginning of the list, which is item 0
int Found = m_SelectedFields.FindString(0, Item);

// If the item is not yet in the Selected Fields list, prepare to add it
if( Found == -1 )
{
// Add the new item at the end of the Selected Fields list
m_SelectedFields.AddString(Item);

// Select the last item of the Selected Fields list
m_SelectedFields.SetCurSel(m_SelectedFields.GetCount() - 1);
} // Consider the next item
}

// Since there is at least one item in the Selected Fields list,
// enable the Remove buttons
m_RemoveOne.EnableWindow();
m_RemoveAll.EnableWindow();
}


  1. Test your program and return to MSVC

  2. Add a BN_CLICKED Event Handler for the < button and name it OnRemoveOne

  3. The < button works by removing, from the Selected Fields list box, the item that is
    selected. To do this, we first need to make sure that an item is selected. All the
    previous code took care of selecting an item in each list. The problem is that, this
    time, the selected item is removed. Therefore, we have the responsibility of selecting
    a new item.
    When an item has been removed, we will select the item that was under it. What if
    we had removed the item that was at the bottom of the list? In that case, we will

Free download pdf