Visual C++ and MFC Programming 2nd Edition

(Martin Jones) #1

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


void CTableWizardDlg::OnDblClkSampleFields()
{
// TODO: Add your control notification handler code here
OnSelectOne();
}


  1. Test the application and return to MSVC

  2. One of the problems we have at this time is that the user can select the same item
    more than once, which is not practical for a database's table. Therefore, when the
    user selects an item, we first need to check whether the Selected Fields list box
    already contains the item. If it does, we will not allow adding the item. We will
    provide an alternate solution shortly. Also, once at least one item has been added to
    the Selected Fields list, we can enable the Remove buttons.
    Change the content of the OnSelectOne() function as follows:


void CTableWizardDlg::OnSelectOne()
{
// Variable that identifies what item is selected in the sample fields
CString SourceSelected;

// Find out what item is selected
// Store it in the above variable
m_SampleFields.GetText(m_SampleFields.GetCurSel(), SourceSelected);

// Find out if the Selected Fields list is empty
if( m_SelectedFields.GetCount() == 0 )
{
// Since the list is empty, add the selected item
m_SelectedFields.AddString(SourceSelected);
// Select the newly added item
m_SelectedFields.SetCurSel(0);
}
else // Since the list is not empty
{
// Look for the selected item in the Selected Fields list
int Found = m_SelectedFields.FindString(0, SourceSelected);

// If the item is not yet in the Selected Fields list, prepare to add it
if( Found == -1 )
{
// Because there is always an item selected in any of our list,
// get the index of the currently selected item
int CurrentlySelected = m_SelectedFields.GetCurSel();

// Add the new item under the selected one
m_SelectedFields.InsertString(CurrentlySelected + 1,
SourceSelected);

// Select the newly added item
m_SelectedFields.SetCurSel(CurrentlySelected + 1);
}
}

// Since an item has been selected, enable the Remove buttons
m_RemoveOne.EnableWindow();
m_RemoveAll.EnableWindow();
}
Free download pdf