Re: VB6 - enforcing data entry in a DataGrid column

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Mark G. Meyers (mmeyers[at]hydromilling.com)
Date: 07/01/04


Date: Thu, 1 Jul 2004 11:26:42 -0400

I am in the process of putting a combobox over grid cells in various
MSFlexGrid controls - same kind of thing.

In the process, I have accumulated a few snippets on steps in this
process...

' Window message to open a drop-down combobox
Public Const CB_SHOWDROPDOWN = &H14F

Public Declare Function SendMessage Lib "user32" _
  Alias "SendMessageA" (ByVal hWnd As Long, _
  ByVal wMsg As Long, ByVal wParam As Long, _
  lParam As Any) As Long

        ' test - set focus to combobox
        Dim cc As ComboBox
        Set cc = cAppFrame.cCtrlPgmHppComboBox(0)
        cc.SetFocus
        Call SendMessage(cc.hWnd, CB_SHOWDROPDOWN, True, 0&)

    ' test put up the combo box over grid 1,1
    Dim cc As ComboBox
    Set cc = cAppFrame.cCtrlPgmHppComboBox(0)
    Set cc.Container = cAppFrame.cCtrlPgmTabFrame(3)
    cc.AddItem ("fee"): cc.AddItem ("fie")
    cc.AddItem ("foe"): cc.AddItem ("fum")
    cc.ListIndex = 3
    mcHppGrid.Row = 1: mcHppGrid.col = 1
    cc.left = mcHppGrid.CellLeft + mcHppGrid.left
    cc.top = mcHppGrid.CellTop + mcHppGrid.top
    cc.Width = mcHppGrid.CellWidth
    cc.ZOrder (0): cc.Visible = True

' --- form module code ----------

' Head Profile Periods Grid
Private Sub cCtrlPgmHppGrid_Click()
    ' need to get the row, column in the grid, and
    ' pass the user's update request along
    Call grDrawCtrlPgm.HppGrid.UpdateCellRequest( _
                cCtrlPgmHppGrid.MouseRow, cCtrlPgmHppGrid.MouseCol)
End Sub

' Called when the user has supplied a new value
' Pass along what is in the combobox to the grid column
' and make the combobox go away
Private Sub cGuiGridComboBox_Click()
    ' get the value and get rid of it
    ' grid row and column should be right
    ' pass along the new value to the grid column
    Call grGuiGridColumn.UpdateCell(gnGuiGridRow, cGuiGridComboBox.Text)
    Call cGuiGridComboBox_LostFocus
End Sub

Private Sub cGuiGridComboBox_LostFocus()
    ' get rid of the combobox
    cGuiGridComboBox.Visible = False
End Sub

Private Sub cGuiGridComboBox_KeyPress(KeyAscii As Integer)
    Select Case KeyAscii
    Case 27
        ' if the escape key is pressed, leave the grid as is
        cGuiGridComboBox.Visible = False
    Case 13
        ' treat enter as user being done with it (has new value)
        Call cGuiGridComboBox_Click
    End Select
End Sub

Some of the code hasn't been fully tested/used yet, but a variety of issues
have been dealt with.

cAppFrame is the main form, where all the grids are.

The combobox will produce a Click event when it's value changes, and I also
check keystrokes in the combo box to see if the user hit the escape key. In
the app, I have one generic combobox (and one textbox), and use these for
each of 3 or 4 grids on the main form (in different tabstrips).

So what I'm doing is placing the combobox over the cell in the grid. I have
noticed that Height is a read-only property, so that has to be right in
design mode.

I keep one or two global variables cooresponding to my GuiGrid/GuiGridColumn
objects. So combobox event handlers know who to give the new value to,
there is a global ref to which 'GuiGridColumn' object to call with the new
value, and there is a global Long with the row number to pass in. My
GuiGridColumn object then puts the new value into the grid.

Hope that helps -
Mark


Quantcast