Pour pouvoir changer le texte d’une grille, il faut lui adjoindre un TextBox et/ou un ComboBox.
Pour un TextBox:
Créer un contrôle TextBox appelé TxtCell
Passer sa propriété Visible à False
Insérer les codes suivants dans les évènements de TxtCell:
Private Sub TxtCell_GotFocus()
TxtCell.SelStart = 0
TxtCell.SelLength = Len(TxtCell.Text)
End Sub
Private Sub TxtCell_LostFocus()
TxtCell.Visible = False
End Sub
Private Sub TxtCell_KeyPress(KeyAscii As Integer)
If KeyAscii <> 13 Then Exit Sub
KeyAscii = 0
‘Mon code programme
Grid.SetFocus
End Sub
Pour un ComboBox:
Créer un contrôle ComboBox appelé ComCell
Passer sa propriété Visible à False
Insérer les codes suivants dans les évènements de ComCell:
Private Sub ComCell_LostFocus()
ComCell.Visible = False
End Sub
Private Sub ComCell_Click()
If ComInhibe Then Exit Sub
‘Mon code programme
Grid.SetFocus
End Sub
Dans le Grid:
Private Sub Grid_DblClick()
With Grid
Select Case .Col
Case 1, 2, 3, 4, 6 ‘Si cette colonne fait apparaître un textbox
TxtCell.Left = .CellLeft + .Left – 15
TxtCell.Top = .CellTop + .Top – 15
TxtCell.Width = .CellWidth + 30
TxtCell.Height = .CellHeight
TxtCell.Text = .TextMatrix(.Row, .Col)
TxtCell.Visible = True
TxtCell.SetFocus
Case 5 ‘Si cette colonne fait apparaître un combobox
ComInhibe = True
RemplitLeCom .TextMatrix(0, .Col) ‘Procédure à écrire pour remplir le combobox
ComCell.Left = .CellLeft + .Left – 15
ComCell.Top = .CellTop + .Top – 15
If .CellWidth > 900 Then
ComCell.Width = .CellWidth + 30
Else
ComCell.Width = 900
End If
ComCell.ListIndex = TrouveLIndex(.TextMatrix(.Row, .Col))
ComCell.Visible = True
ComCell.SetFocus
ComInhibe = False
Case Else
End Select
End With
End Sub
Function TrouveLIndex(QuelTexte) As Integer
TrouveLIndex = -1
For i = 0 To ComCell.ListCount – 1
If QuelTexte = ComCell.List(i) Then
TrouveLIndex = i
Exit For
End If
Next i
End Function