How can I allow a user to type into a msflexgrid? i have tree columns that represent day, week, and month value and would like the user to supply in the grid. Any ideas on this matter would be greatly appereciated.
Printable View
How can I allow a user to type into a msflexgrid? i have tree columns that represent day, week, and month value and would like the user to supply in the grid. Any ideas on this matter would be greatly appereciated.
Code:Private Sub MSFlexGrid1_KeyPress(KeyAscii As Integer)
MSFlexGrid1.TextMatrix(MSFlexGrid1.RowSel, MSFlexGrid1.ColSel) = MSFlexGrid1.TextMatrix(MSFlexGrid1.RowSel, MSFlexGrid1.ColSel) & Chr$(KeyAscii)
End Sub
Mad Compie,
You are the man. I thought I would have to revert to a DBGrid. Something so hard, yet so simple. Thanks, you saved me a hell of alot of redoing...
Mad Compie: or anyone who may reply,
New question same subject.
As before, my three input columns will be numeric, but how can i make it so that the user can use the backspace key and delete key for edit purposes. What I've done so far works, except if the user must change the typed in value.
Please help.
I had the same problem as you and I found a solution finally :)
You have to use a textbox that you dont make visible in a first time.
You place the textbox at the same Left and Top of your MSFlexGrid.
When the user click on the cell you want, you have to display the textbox but before place it over the cell :Code:TextBox.Visible = False
TextBox.Top = MSFlexGrid.Top
TextBox.Left = MSFlexGrid.Left
And you have to play with the KeyPress event of the TextBox.Code:TextBox.Left = TextBox.Left + MSFlexGrid.CellLeft
TextBox.Top = TextBox.Top + MSFlexGrid.CellTop
TextBox.Visible = True
TextBox.SetFocus
If "Enter" (13) is pressed by the user, the textbox return to his initial place, is invisible, and the cell contains the text write in the textbox.
You can do a lot a things such as move the textbox to the next cell when the user press the up/down key or left/right key.Code:Private Sub TextBox_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyReturn
MSFlexGrid.TextMatrix(MSFlexGrid.Row, MSFlexGrid.Col) = TextBox.Text
TextBox.Visible = False
TextBox.Left = MSFlexGrid.Left
TextBox.Top = MSFlexGrid.Top
Case vbKeyBack
TextBox.Text = Left(TextBox.Text, Len(TextBox.Text) - 1)
End Select
End Sub
Nicolas Joannidès
Visual Basic, C, ASP
[email protected]
There is also an API that handles numeric key presses:
Code:'This project needs a TextBox
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Const GWL_STYLE = (-16)
Const ES_NUMBER = &H2000&
Public Sub SetNumber(NumberText As TextBox, Flag As Boolean)
Dim curstyle As Long, newstyle As Long
'retrieve the window style
curstyle = GetWindowLong(NumberText.hwnd, GWL_STYLE)
If Flag Then
curstyle = curstyle Or ES_NUMBER
Else
curstyle = curstyle And (Not ES_NUMBER)
End If
'Set the new style
newstyle = SetWindowLong(NumberText.hwnd, GWL_STYLE, curstyle)
'refresh
NumberText.Refresh
End Sub
Private Sub Form_Load()
SetNumber Text1, True
Me.Caption = "Now, try typing some letters into the textbox"
End Sub
How can we do to limit only numbers in the grids but not in the textbox, can we do that by using api? because i need to move the textbox from grid to grid... and needs lots of code... hope i can use api to control the entry and use what was mentioned (the best codes) above that can save lots of time
dragon
I don't think there is any API to do this. You should write your own code to convert (to filter) the TextBox Text property to the grid TextMatrix property.