VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ComboWithGrid"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
'--------------------------------------------------------------------------------------------------------------------------------------
'Class Name     :   ComboWithGrid
'Written By     :   Riyaz Shaikh
'Start Date     :   16th March, 2007
'Purpose        :   This can be used to manage datacombo control on particular column of Datagrid control.
'--------------------------------------------------------------------------------------------------------------------------------------

Option Explicit

Private WithEvents dbCombo As DataCombo                     'DataCombo control which has to be managed with grid
Attribute dbCombo.VB_VarHelpID = -1
Private WithEvents dtGrid As DataGrid
Attribute dtGrid.VB_VarHelpID = -1
Private lcColIndex As Integer                               'On which column combo has to be set
Private lcMustMatchWithList As Boolean                      'Whether entry in combo must match with its list or not


Public Event ManageTabStops(SetTabStop As Boolean)          'Set TabStop of all controls at form level in this event
Public Event LeavingCombo(ColIndex, SelectedItemID As Long) 'Entry in the combo has been validated and now focus is going out of it.
Public Event ColumnChanged(ColIndex As Integer)             'Current column of grid is changed.
Public Event EntryNotMatching(ColIndex As Integer)          'Entry in the combo is not matching and user trying to leave the control.



Private Sub dbCombo_GotFocus()
    StdFn.SetAutoListMembers dbCombo
    
    'If data in combo is already matching then drop down the list.
    If dbCombo.MatchedWithList Then
        SendKeys "{F4}"
    End If
    
    'Remove tab stop of all controls
    RaiseEvent ManageTabStops(False)
End Sub



Private Sub dbCombo_KeyDown(KeyCode As Integer, Shift As Integer)
    
    'Tab stop of all the controls is set to false; so we can capture tab key
    If KeyCode = vbKeyTab Then
        
        'Check whether contents in Combo should match with list or not.
        If lcMustMatchWithList And Trim(dbCombo.Text) <> "" Then
            If dbCombo.MatchedWithList Then
                dtGrid.Text = dbCombo.Text
                dbCombo.Visible = False
                
                RaiseEvent LeavingCombo(lcColIndex, Val(dbCombo.BoundText))
                dbCombo.BoundText = -1
                dbCombo.Text = ""
                
                'Go to next column
                dtGrid.Col = dtGrid.Col + 1
                RaiseEvent ColumnChanged(dtGrid.Col)
                
                dtGrid.SetFocus
            Else
                'Entry must match with list.
                dbCombo.SetFocus
                
                RaiseEvent EntryNotMatching(lcColIndex)
            End If
        
        Else
            dtGrid.Text = dbCombo.Text
            dbCombo.Visible = False
            
            RaiseEvent LeavingCombo(lcColIndex, Val(dbCombo.BoundText))
            dbCombo.BoundText = -1
            dbCombo.Text = ""
            
            'Go to next column
            dtGrid.Col = dtGrid.Col + 1
            RaiseEvent ColumnChanged(dtGrid.Col)
            
            dtGrid.SetFocus
        End If
    End If
End Sub



Private Sub dbCombo_LostFocus()
    dbCombo.BoundText = -1
    dbCombo.Text = ""
    
    'Set tab stop of all controls
    RaiseEvent ManageTabStops(True)
End Sub



Private Sub dbCombo_Validate(Cancel As Boolean)
    On Error Resume Next

    If lcMustMatchWithList Then
        If dbCombo.Text <> "" Then
            If Not dbCombo.MatchedWithList Then
                Cancel = True
                dbCombo.SetFocus
                dbCombo.SelStart = Len(dbCombo.Text)
                
                RaiseEvent EntryNotMatching(lcColIndex)
                Exit Sub
            End If
        End If
    End If
    
    dtGrid.Text = dbCombo.Text
    
    RaiseEvent LeavingCombo(lcColIndex, Val(dbCombo.BoundText))
    dbCombo.Visible = False
    dtGrid.SetFocus
    
    DoEvents
End Sub



Private Sub dtGrid_ColResize(ByVal ColIndex As Integer, Cancel As Integer)
    dtGrid.Text = dbCombo.Text
    dbCombo.Visible = False
End Sub



Private Sub dtGrid_KeyDown(KeyCode As Integer, Shift As Integer)
    If dtGrid.Col = lcColIndex And Not dbCombo.Visible And KeyCode = vbKeyF4 Then
        Call dtGrid_ButtonClick(lcColIndex)
    End If
End Sub



Private Sub dtGrid_Scroll(Cancel As Integer)
    dbCombo.Visible = False
End Sub



Private Sub dtGrid_ButtonClick(ByVal ColIndex As Integer)
   With dbCombo
       If ColIndex = lcColIndex Then
          .Top = dtGrid.RowTop(dtGrid.Row) + dtGrid.Top
          .Left = dtGrid.Left + dtGrid.Columns(ColIndex).Left
          .Width = dtGrid.Columns(ColIndex).Width
          .Visible = True
    
          If .Visible Then
             .Text = dtGrid.Text
             .ZOrder
             .SetFocus
             
             If Not .MatchedWithList Then
                SendKeys "{F4}"
             End If
             
             DoEvents
          End If
       End If
   End With
End Sub



Private Sub dtGrid_Change()
    If dtGrid.Col = lcColIndex Then
        With dbCombo
            .Visible = True
            .Text = dtGrid.Text
        
            .Top = dtGrid.RowTop(dtGrid.Row) + dtGrid.Top
            .Left = dtGrid.Columns(lcColIndex).Left
            .Width = dtGrid.Columns(lcColIndex).Width
            .ZOrder
            .SetFocus
        End With
    End If
End Sub



Private Sub dtGrid_Click()
    On Error Resume Next
    dbCombo.Visible = False
End Sub



Public Sub SetComboOnGrid(Combo As DataCombo, Grid As DataGrid, ColumnIndex As Integer, MustMatchWithList As Boolean)
    Set dbCombo = Combo
    Set dtGrid = Grid
    lcColIndex = ColumnIndex
    lcMustMatchWithList = MustMatchWithList
End Sub
