Results 1 to 2 of 2

Thread: MSHFlexGrid + Strange Events

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2011
    Posts
    31

    MSHFlexGrid + Strange Events

    Hi everyone,

    I have experienced a problem of events in the big VB6 project of my company that I did not resolve. To help you understand the problem, I have created a small projet which highlights the problem.

    My small projet is made up of two forms. There are three controls on the main form (Name : frmForm1) : a MSHFlexGrid control (Name : grdGrid), a command button control (Name : cmdButton) and a textbox control (Name : txtField). I have set the TabStop property to False for cmdButton.

    The second form (Name : frmForm2) is empty with a yellow background color. The second form is shown from the main form as a modal form.

    Here is the code contained in the main form :
    Code:
    Option Explicit
    
    Private mblnFirstTime   As Boolean '< False when the grid get the focus after the first time.
    
    Private Sub Form_Load()
        ' Define the initial active cell.
        grdGrid.Col = 0
        grdGrid.Row = 1
        
        ' Initialise the form-level boolean.
        mblnFirstTime = True
    End Sub
    
    Private Sub grdGrid_GotFocus()
        ' Check if the grid get the focus for the first time.
        If Not (mblnFirstTime) Then
            txtField.Visible = False
            grdGrid_KeyPress vbKeyTab
        Else
            mblnFirstTime = False
        End If
    End Sub
    
    Private Sub grdGrid_KeyPress(KeyAscii As Integer)
        If (KeyAscii = vbKeyTab) Then
            ' Move the active cell into the right column.
            grdGrid.Col = grdGrid.Col + 1
            
            ' Show the field and set the focus to it.
            txtField.Visible = True
            txtField.SetFocus
        End If
    End Sub
    
    Private Sub cmdButton_Click()
        ' Display a modal form.
        frmForm2.Show vbModal
        
        ' Set the focus to the field.
        txtField.SetFocus
    End Sub
    What I expect :
    I launch the program. The grdGrid get the focus. The boolean mblnFirstTime is set to False. Then I click on cmdButton. The form frmForm2 is displayed, I close it then txtField get the focus.

    What I experience :
    The process describe above does not happen. After clicking on cmdButton, frmForm2 is displayed as expected. But, when I close frmForm2 then an error is raised. The error window tells me that grdGrid.Col has got its maximum value. It means that grdGrid_KeyPress is called as many times as possible. grdGrid_GotFocus is consequently called as many times as possible too. I am finally wondering : Why does the grid got the focus as many times as possible ?

    The following images show my experienced problem :

    http://img857.imageshack.us/i/mshfgstangeevent1.png/

    http://img703.imageshack.us/i/mshfgstangeevent2.png/

    http://img20.imageshack.us/i/mshfgstangeevent3.png/

    I suspect the problem coming from the execution order of events but I did not find any interesting information about it on the Internet.

    I would really appreciate any help.

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: MSHFlexGrid + Strange Events

    With the button's TabStop=false, when the modal form closes, VB will set focus back to the visible/enabled control that has a TabStop=True with the lowest TabIndex property. So, in your case, closing the modal form automatically focuses on your grid which then calls the _KeyPress event which makes txtField visible and sets focus to it. Is it possible the tab key is processed by your txtField then sending focus back to the grid control & an infinite loop happens?

    You can add some Debug.Print statements in the GotFocus events of both the grid & textbox to get a feel for what is happening.
    I don't know if this will help, but maybe set KeyAscii=0 before exiting the event?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width