⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 duallistmanager.cpp

📁 一个出勤的数据库系统
💻 CPP
📖 第 1 页 / 共 3 页
字号:
//
//  Return value:
//
//		void 
//
//  Exceptions:		None
//
///////////////////////////////////////////////////////////////////////////////
void CDualListManager::AddItemToChoosenList(LPCTSTR lpszItem, long lItemData)
{
    m_ArrayChoosen.Add(lItemData);
    m_KeyMap.SetAt(lItemData, lpszItem);
}

///////////////////////////////////////////////////////////////////////////////
//
//  Method:			MoveAll
//
//  Purpose:		Moves all of the selected items from one listbox to the
//                  other.
//
//  Inputs:
//
//		CListBox & rLBFrom - listbox moving from
//
//		CArray<long, long> & rArrayFrom - array moving from
//
//		CArray<long, long> & rArrayTo - array moving to
//
//  Outputs:
//
//		None
//
//  Return value:
//
//		void 
//
//  Exceptions:		None
//
///////////////////////////////////////////////////////////////////////////////
void CDualListManager::MoveAll(CListBox & rLBFrom, CListBox & rLBTo, CArray<long, long> & rArrayFrom, CArray<long, long> & rArrayTo) 
{
    CString csName;

    // Move all of the remaining items in the listbox to the opposite array
    int iCount = rLBFrom.GetCount();
    for(int iIndex = 0; iIndex < iCount; ++iIndex)
    {
        long lVal = rLBFrom.GetItemData(iIndex);
        rArrayTo.Add(lVal);
    }

    // Clear the array of items we are clearing out
    rArrayFrom.RemoveAll();

    // Re-fill the listboxes with the contents of the array
    FillListboxes();

    // Select the first item in the to listbox
    SelectLBItem(rLBTo, 0);
}


///////////////////////////////////////////////////////////////////////////////
//
//  Method:			MoveSelected
//
//  Purpose:		Moves the selected items from one listbox and places them
//                  in the other listbox.
//
//  Inputs:
//
//		CListBox & rLBFrom - listbox moving from
//
//		CArray<long, long> & rArrayFrom - array moving from
//
//		CArray<long, long> & rArrayTo - array moving to
//
//		BOOL bRemoving - TRUE if we are removing items 
//                       FALSE if we are adding items
//
//  Outputs:
//
//		None
//
//  Return value:
//
//		void 
//
//  Exceptions:		None
//
///////////////////////////////////////////////////////////////////////////////
void CDualListManager::MoveSelected(CListBox & rLBFrom, CArray<long, long> & rArrayFrom, CArray<long, long> & rArrayTo, BOOL bRemoving)
{
    CArray<long, long>  SelectedArray;

    // Loops through all of the items in the from listbox 
    int iSelFirst = -1;
    int iCount    = rLBFrom.GetCount();
    for(int iIndex = 0; iIndex < iCount; ++iIndex)
    {
        // Check to see if this item is currently selected
        if( 0 < rLBFrom.GetSel( iIndex ) )
        {
            // Hang onto the first selected item in the list so we can keep
            // the item at that position selected.
            if(-1 == iSelFirst)
                iSelFirst = iIndex;

            // Get the item data for this item
            long lItemData = rLBFrom.GetItemData(iIndex);

            // Add the item data value to the list of selected items
            SelectedArray.Add(lItemData);

            // Add this item to the other array of items 
            rArrayTo.Add(lItemData);

            // Find this item in the array we are moving it out of and remove it
            int iCount2 = rArrayFrom.GetSize();
            for(int iIndex2 = 0; iIndex2 < iCount2; ++iIndex2)
            {
                if( lItemData == rArrayFrom[iIndex2] )
                {
                    rArrayFrom.RemoveAt(iIndex2);
                    break;
                }
            }
        }
    }

    // Re-fill the listboxes with the contents of the array
    FillListboxes(&SelectedArray, bRemoving);

    // Select one item in the from listbox at the location of the first 
    // selected item before the items were moved.
    if(-1 != iSelFirst)
    {
        SelectLBItem(rLBFrom, rLBFrom.GetCount() <= iSelFirst 
                              ? rLBFrom.GetCount() - 1 : iSelFirst);
    }
}

///////////////////////////////////////////////////////////////////////////////
//
//  Method:			MoveUpOrDown
//
//  Purpose:		Moves the hilighted items in the list of choosen items 
//                  either up or down depending on the value of the passed in
//                  flag.
//                  
//                  If more than one item is selected all of the items are
//                  moved as a block. If one or more items have reached the 
//                  top or bottom while other haven't those items stay at their
//                  current location and the other continue to move.
//
//  Inputs:
//
//		BOOL bMovingUp - TRUE if the items should be moved up
//                       FALSE if the items should be moved down
//
//  Outputs:
//
//		None
//
//  Return value:
//
//		void 
//
//  Exceptions:		None
//
///////////////////////////////////////////////////////////////////////////////
void CDualListManager::MoveUpOrDown(BOOL bMovingUp) 
{
    int                 iIndex, iIndex2, iCount, iCount2, iOffset, iInsertAt;
    int                 iIndexSelectedMarker = -1;
    long                lItemData;
    CString             csName;
    CArray<long, long>  SelectedArray;

    // Get the count of items in the list control
    iCount = m_ctrlChoosenList.GetCount();

    // Set the base loop index and the increment/decrement value based on the
    // direction the item are being moved (up or down).
    iIndex  = (TRUE  == bMovingUp) ? 0  : iCount - 1;
    iOffset = (TRUE  == bMovingUp) ? -1 : 1;
    
    // Loop through all of the items in the list.
    while( (TRUE  == bMovingUp && iIndex < iCount) || 
           (FALSE == bMovingUp && iIndex >= 0) )
    {
        // Check if this item is selected.
        if( 0 < m_ctrlChoosenList.GetSel( iIndex ) )
        {
            // Get the item data for this item
            lItemData = m_ctrlChoosenList.GetItemData(iIndex);

            // Build a list of the selected items so we can reselect them
            // later.
            SelectedArray.Add(lItemData);

            // Don't move selected items past other selected items
            if(-1 != iIndexSelectedMarker)
            {
                // Loop through the enabled list looking for the current item
                // which is identified by the item data value.
                iCount2 = m_ArrayChoosen.GetSize();

                for(iIndex2 = 0; iIndex2 < iCount2; ++iIndex2)
                {
                    // Find the index of this item in enabled list
                    if( lItemData == m_ArrayChoosen[iIndex2] )
                    {
                        // Remove the item from its current position
                        m_ArrayChoosen.RemoveAt(iIndex2);

                        // Reinsert the item in the array one space higher 
                        // than its previous position
                        iInsertAt = (iIndex2 + iOffset) < 0 ? 0 : iIndex2 + iOffset;
                        m_ArrayChoosen.InsertAt(iInsertAt, lItemData);
                        break;
                    }
                }
            }
        }
        // If this item wasn't selected save the index so we can check it later
        // so we don't move past the any selected items.
        else if(-1 == iIndexSelectedMarker)
        {
            iIndexSelectedMarker = iIndex;
        }

        iIndex = (TRUE  == bMovingUp) ? iIndex + 1 : iIndex - 1;
    }

    // Re-fill the listboxes with the contents of the array
    FillListboxes(&SelectedArray);
}

///////////////////////////////////////////////////////////////////////////////
//
//  Method:			FillListboxes
//
//  Purpose:		Fills both listboxes with the current contents of their
//                  associated arrays. This method also reselects the items
//                  in the passed in array of selected items in appropriate
//                  list.
//
//  Inputs:
//
//		CArray<long
//
//		long> * pSelectedArray
//
//		BOOL bRemoving
//
//  Outputs:
//
//		None
//
//  Return value:
//
//		void 
//
//  Exceptions:		None
//
///////////////////////////////////////////////////////////////////////////////
void CDualListManager::FillListboxes(CArray<long, long> * pSelectedArray, BOOL bRemoving)
{
    int     iIndex, iIndex2, iCount;
    CString csName;

    // Clear and refill the listbox of disabled commands from the disabled
    // command array.
    m_ctrlAvailableList.ResetContent();
    iCount = m_ArrayAvailable.GetSize();
    for(iIndex = 0; iIndex < iCount; ++iIndex)
    {
        m_KeyMap.Lookup(m_ArrayAvailable[iIndex], csName);
        iIndex2 = m_ctrlAvailableList.AddString(csName);
        m_ctrlAvailableList.SetItemData(iIndex2, m_ArrayAvailable[iIndex]);
    }

    // Clear and refill the listbox of enabled commands from the enabled
    // command array.
    m_ctrlChoosenList.ResetContent();
    iCount = m_ArrayChoosen.GetSize();
    for(iIndex = 0; iIndex < iCount; ++iIndex)
    {
		long l=m_ArrayChoosen[iIndex];
        m_KeyMap.Lookup(m_ArrayChoosen[iIndex], csName);
        iIndex2 = m_ctrlChoosenList.AddString(csName);
        m_ctrlChoosenList.SetItemData(iIndex2, m_ArrayChoosen[iIndex]);
    }

    // Now reselect the items that were selected before we moved them.
    if(NULL != pSelectedArray)
    {
        iCount = pSelectedArray->GetSize();
        for(iIndex = 0; iIndex < iCount; ++iIndex)
        {
            m_KeyMap.Lookup(pSelectedArray->GetAt(iIndex), csName);
            if(TRUE == bRemoving)
            {
                iIndex2 = m_ctrlAvailableList.FindStringExact(-1, csName);
                SelectLBItem(m_ctrlAvailableList, iIndex2);
            }
            else
            {
                iIndex2 = m_ctrlChoosenList.FindStringExact(-1, csName);
                SelectLBItem(m_ctrlChoosenList, iIndex2);
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -