Click here to Skip to main content
Licence CPOL
First Posted 2 Apr 2008
Views 45,419
Downloads 1,080
Bookmarked 19 times

Edit MS Flex Grid in Two Ways

By | 2 Apr 2008 | Article
Edit MS Flex Grid with or without a TextBox or ComboBox.

Introduction

This is my second article on CodeProject. This is about how to edit an MS Flex Grid. I was using a TextBox and ComboBox to edit a flex grid at run time. For that, I had to add a reference to Microsoft.VisualBasic.Compatibility and some complex coding to move the TextBox or ComboBox on the grid. This is a good idea if some one wants to edit a grid with some predefined items using a ComboBox.

But if you want to edit text, then there is a simple way to do it: use the Text property of the MS Flex Grid control.

Using the code

To edit the MS Flex Grid without using a TextBox, we have to use the Text property and the KeyPress event of the MS Flex Grid control.

Private Sub AxMSFlexGrid1_KeyPressEvent(ByVal sender As Object, _
        ByVal e As AxMSFlexGridLib.DMSFlexGridEvents_KeyPressEvent) _
        Handles AxMSFlexGrid1.KeyPressEvent

    Select Case e.keyAscii
    Case 30 To 136
        AxMSFlexGrid1.Text += Chr(e.keyAscii)
    Case 8
        If AxMSFlexGrid1.Text <> "" Then
            AxMSFlexGrid1.Text = Mid(AxMSFlexGrid1.Text, 1, _
                                         Len(AxMSFlexGrid1.Text) - 1)
    Case 13 And AxMSFlexGrid1.Col <> 2
        AxMSFlexGrid1.Col = 2
    Case 13 And AxMSFlexGrid1.Col = 2
        AxMSFlexGrid1.Rows = AxMSFlexGrid1.Rows + 1
        AxMSFlexGrid1.Col = 1
        AxMSFlexGrid1.Row = AxMSFlexGrid1.Row + 1
    End Select
End Sub

Using the second technique, I used a function name moveTextBox to move the TextBox to the appropriate position, and to set the text in the grid, I used the Text property of the grid control.

Private Sub moveTextBox()
    TxtEntry.Visible = True
    TxtEntry.Left = VB6.TwipsToPixelsX(AxMSFlexGrid2.CellLeft + _
                    VB6.TwipsToPixelsX(AxMSFlexGrid2.Left)) + 17
    TxtEntry.Top = VB6.TwipsToPixelsY(AxMSFlexGrid2.CellTop + _
                   VB6.TwipsToPixelsY(AxMSFlexGrid2.Top)) + AxMSFlexGrid2.Top
    TxtEntry.Width = VB6.TwipsToPixelsX(AxMSFlexGrid2.CellWidth)
    TxtEntry.Height = VB6.TwipsToPixelsY(AxMSFlexGrid2.CellHeight)
    TxtEntry.BringToFront()
    TxtEntry.Focus()
End Sub

On the TextBox's KeyDown event, I assigned the TextBox's Text property to the Flex Grid's Text property.

Private Sub TxtEntry_KeyDownEvent(ByVal sender As Object, _
            ByVal e As System.Windows.Forms.KeyEventArgs) _
            Handles TxtEntry.KeyDown
    If AxMSFlexGrid2.Col = 1 Then
        If e.KeyCode = Keys.Enter Then
            AxMSFlexGrid2.Text = TxtEntry.Text
            TxtEntry.Clear()
            AxMSFlexGrid2.Col = 2
        End If
    End If
End Sub

On the EnterCell event of the grid control, I have called this function:

Private Sub AxMSFlexGrid2_EnterCell(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles AxMSFlexGrid2.EnterCell
    If AxMSFlexGrid2.Col = 2 Then
        Call moveCombo()
        If AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 2) <> "" Then
            CmbEntry.Text = AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 2).ToString
        Else
            CmbEntry.Text = ""
        End If
    Else
        CmbEntry.Visible = False
    End If
    If AxMSFlexGrid2.Col = 1 Then
        Call moveTextBox()
        If AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 1) <> "" Then
            TxtEntry.Text = AxMSFlexGrid2.get_TextMatrix(AxMSFlexGrid2.Row, 1).ToString
        Else
            TxtEntry.Clear()
        End If
    Else
        TxtEntry.Visible = False
    End If
End Sub

Thanks and happy coding..

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Suresh Suthar

Software Developer (Senior)
HOC
India India

Member

Still Alive.....

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMsFlexGrid Events In Vb6.0 Pinmembersahista mohammad22:40 28 Feb '11  
GeneralPlease Help Me Using MS Flexgrid in VB .NET 2003 PinmemberAbhijeet Ghosh21:18 26 Nov '09  
GeneralIts nice for VB6 PinmemberAtanu Maity3:22 5 Apr '08  
GeneralRe: Its nice for VB6 Pinmembersuresh suthar3:12 9 Apr '08  
GeneralFlexGrid - Yuck! PinmemberPeterTheGreat2:48 2 Apr '08  
GeneralRe: FlexGrid - Yuck! PinmemberAbhay Mishra2:59 2 Apr '08  
GeneralRe: FlexGrid - Yuck! PinmemberJoe Sonderegger4:06 2 Apr '08  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 2 Apr 2008
Article Copyright 2008 by Suresh Suthar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid