Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleUse ComboBoxes and TextBoxes to let the user edit grid values
Keywordsgrid, edit, ComboBox, Textbox
CategoriesControls
 
When the user double-clicks a cell, call subroutine StartEdit. That routine determines which column is selected. If the column calls for a ComboBox, StartEdit initializes the ComboBox's values and calls subroutine GridEditCombo. If the column calls for a TextBox, StartEdit calls subroutine GridEditText

If the user types a character in a column that uses a TextBox, the MSFlexGrid1_KeyPress event handler calls subroutine GridEditText, passing it the character so it can start editing with it.

 
Private Sub MSFlexGrid1_DblClick()
    StartEdit
End Sub

Private Sub StartEdit()
    If MSFlexGrid1.Col = 2 Then
        Combo1.Clear
        Combo1.AddItem "Paranoia City"
        Combo1.AddItem "Langonsville"
        Combo1.AddItem "Codertown"
        Combo1.AddItem "Bugvanna"
        Combo1.AddItem "Abend"
        On Error Resume Next
        Combo1.Text = MSFlexGrid1.Text
        On Error GoTo 0

        GridEditCombo
    ElseIf MSFlexGrid1.Col = 3 Then
        Combo1.Clear
        Combo1.AddItem "CA"
        Combo1.AddItem "VT"
        Combo1.AddItem "MN"
        Combo1.AddItem "WY"
        Combo1.AddItem "TX"
        On Error Resume Next
        Combo1.Text = MSFlexGrid1.Text
        On Error GoTo 0

        GridEditCombo
    Else
        GridEditText Asc(" ")
    End If
End Sub

Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
    If MSFlexGrid1.Col <> 2 And _
       MSFlexGrid1.Col <> 3 _
    Then
        GridEditText KeyAscii
    End If
End Sub
 
Subroutines GridEditCombo and GridEditText position a ComboBox or a TextBox over the selected cell, respectively. They make the control visible and give it focus.
 
Private Sub GridEditCombo()
    ' Position the ComboBox over the cell.
    Combo1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Combo1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Combo1.Width = MSFlexGrid1.CellWidth
    Combo1.Visible = True
    Combo1.SetFocus
End Sub

Private Sub GridEditText(ByVal KeyAscii As Integer)
    ' Position the TextBox over the cell.
    Text1.Left = MSFlexGrid1.CellLeft + MSFlexGrid1.Left
    Text1.Top = MSFlexGrid1.CellTop + MSFlexGrid1.Top
    Text1.Width = MSFlexGrid1.CellWidth
    Text1.Height = MSFlexGrid1.CellHeight
    Text1.Visible = True
    Text1.SetFocus

    Select Case KeyAscii
        Case 0 To Asc(" ")
            Text1.Text = MSFlexGrid1.Text
            Text1.SelStart = Len(Text1.Text)
        Case Else
            Text1.Text = Chr$(KeyAscii)
            Text1.SelStart = 1
    End Select
End Sub
 
If the user use is editing with a TextBox and presses Escape, Return, the down arrow, or the up arrow, the KeyDown event handler takes action. If the user pressed Escape, the program hides the TextBox. If the user presses the up or down arrow, the program moves the grid's selected row up or down one.

In all of these cases, the program sets focus to the grid control. That fires the GotFocus event, which calls subroutine EndEdit.

If the user selects a choice from the ComboBox, the program simply sets focus to the grid control. Again that fires the GotFocus event, which calls subroutine EndEdit.

 
Private Sub Text1_KeyDown(KeyCode As Integer, Shift As _
    Integer)
    Select Case KeyCode
        Case vbKeyEscape
            ' Leave the text unchanged.
            Text1.Visible = False
            MSFlexGrid1.SetFocus

        Case vbKeyReturn
            ' Finish editing.
            MSFlexGrid1.SetFocus

        Case vbKeyDown
            ' Move down 1 row.
            MSFlexGrid1.SetFocus
            DoEvents
            If MSFlexGrid1.Row < MSFlexGrid1.Rows - 1 Then
                MSFlexGrid1.Row = MSFlexGrid1.Row + 1
            End If

        Case vbKeyUp
            ' Move up 1 row.
            MSFlexGrid1.SetFocus
            DoEvents
            If MSFlexGrid1.Row > MSFlexGrid1.FixedRows Then
                MSFlexGrid1.Row = MSFlexGrid1.Row - 1
            End If

    End Select
End Sub

Private Sub MSFlexGrid1_LeaveCell()
    EndEdit
End Sub

Private Sub MSFlexGrid1_GotFocus()
    EndEdit
End Sub

' Make the change.
Private Sub Combo1_Click()
    MSFlexGrid1.SetFocus
End Sub
 
Subroutine EndEdit checks whether the ComboBox is visible. If it is, the program sets the cell's new value and hides the ComboBox. Similarly if the TextBox is visible, the program sets the cell's new value and hides the TextBox. If neither control is visible, the grid's value remains unchanged.
 
Private Sub EndEdit()
    If Combo1.Visible Then
        MSFlexGrid1.Text = Combo1.Text
        Combo1.Visible = False
    ElseIf Text1.Visible Then
        MSFlexGrid1.Text = Text1.Text
        Text1.Visible = False
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated