Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


select the new last item in the list.
To do this, implement the OnRemoveOne() event as follows:

void CTableWizardDlg::OnRemoveOne()
{
// TODO: Add your control notification handler code here
// Based on previous code, an item should be selected
// in the Selected Fields list
// Get the index of the currently selected item
int CurrentlySelected = m_SelectedFields.GetCurSel();

// Remove the item from the list
m_SelectedFields.DeleteString(CurrentlySelected);

// Find out whether the list has just been emptied or not
// If the Selected Fields list is empty,
if( m_SelectedFields.GetCount() == 0 )
{
// then disable the Remove buttons
m_RemoveOne.EnableWindow(FALSE);
m_RemoveAll.EnableWindow(FALSE);
}
else // Since the Selected Fields list still contains something
{
// Find out if the item that has just been removed was the last in the list
if( CurrentlySelected == m_SelectedFields.GetCount() )
{
// Select the last item in the list
m_SelectedFields.SetCurSel(m_SelectedFields.GetCount() - 1);
}
else // Otherwise
{
// Select the item that was above the one that was just deleted
m_SelectedFields.SetCurSel(CurrentlySelected);
}
}
}


  1. We also want the user to be able to remove an item by double-clicking it. Of course,
    this is not only optional but also application dependent because another program
    would have a different behavior when the item is double-clicked.
    Add an LBN_DBLCLK Event Handler for the IDC_SELECTEDFIELDS list box
    and name it OnDblClkSelectedFields

  2. Implement it as follows:


void CTableWizardDlg::OnDblClkSelectedFields()
{
// When the user double-clicks an item in the Selected Fields list,
// behave as if the < button has been clicked
OnRemoveOne();
}


  1. Test your program and return to MSVC

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

  3. The << button is used to simply empty the Selected Fields list. It does not perform
    any checking of any kind. The only thing we need to do is to disable the Remove
    buttons after the Selected Fields list has been emptied.
    Therefore, implement the function as follows:

Free download pdf