Random results when exit of a frame within a listbox

andresandres83

New Member
Joined
Jun 26, 2019
Messages
3
Hello, I want to insert items in a list and for that I have put a text box inside a frame that I place over the desired item in the list. All this because you cannot put a textbox on a list in Zorder 0.
The operative to follow is: First you have to select an item from a list, then press the cmdAdd button to insert a new row. Then write in the created spaces and then click on any other element to trigger the events.
The results I get are not always the same, sometimes the ListBox_Exit event trigger first and sometimes the Frame_Exit. I cannot find the reason.
I send source code and screenshots of the userform with objects and results.
There must be data to fill the lists in the first 4 rows of columns A and B of Sheet1.

Code:
    Option Explicit
    Private mlngListIndex As Long
    Private mbNotExecute As Boolean
    
    Private Sub UserForm_Initialize()
        With UserForm1.lstLeft
            .List = Sheets("Sheet1").Range("A1:A4").Cells.Value2
        End With
        With UserForm1.lstRight
            .List = Sheets("Sheet1").Range("B1:B4").Cells.Value2
        End With
            
    End Sub
    
    Private Sub lstLeft_Click()
        Debug.Print "lstLeft_Click"
        If mbNotExecute Then Exit Sub
        
        With UserForm1
            If .fraLeft.Visible Then Exit Sub
            mbNotExecute = True
            .lstRight.ListIndex = .lstLeft.ListIndex
            mbNotExecute = False
        End With
    End Sub
    
    Private Sub lstRight_Click()
        Debug.Print "lstRight_Click"
        If mbNotExecute Then Exit Sub
        
        With UserForm1
            If .fraLeft.Visible Then Exit Sub
            mbNotExecute = True
            .lstLeft.ListIndex = .lstRight.ListIndex
            mbNotExecute = False
        End With
    
    End Sub
    
    Private Sub fraLeft_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Debug.Print "fraLeft_Exit"
    End Sub
    
    Private Sub fraRight_Exit(ByVal Cancel As MSForms.ReturnBoolean)
        Debug.Print "fraRight_Exit"
    End Sub
    
    Private Sub cmdAdd_Click()
        With UserForm1
            If .lstLeft.ListIndex <> -1 Then
                .lstLeft.AddItem "", .lstLeft.ListIndex
                .lstRight.AddItem "", .lstLeft.ListIndex - 1
                .lstLeft.ListIndex = .lstLeft.ListIndex - 1
                mlngListIndex = .lstLeft.ListIndex
                Call Edit
            End If
        End With
    
    End Sub
    
    Private Sub Edit()
        Dim ObjTop As Single
        With UserForm1
            If .lstRight.ListIndex <> -1 Then
                ObjTop = .lstLeft.ListIndex * 10 + .lstLeft.Top + 1
                .fraLeft.Visible = True
                .fraLeft.ZOrder 0
                .fraLeft.Top = ObjTop
                .fraLeft.Left = .lstLeft.Left + 2
                .fraLeft.Height = 10
                .fraLeft.Width = .lstLeft.Width - 3
                .txtLeft.Top = -3
                .txtLeft.Left = -3
                .txtLeft.Width = .fraLeft.Width + 3
                .txtLeft.Height = 16
                .txtLeft.Text = .lstLeft.List(.lstLeft.ListIndex)
                
                .fraRight.Visible = True
                .fraRight.ZOrder 0
                .fraRight.Top = ObjTop
                .fraRight.Left = .lstRight.Left + 2
                .fraRight.Height = 12
                .fraRight.Width = .lstRight.Width - 2
                .fraRight.Height = 10
                .txtRight.Visible = True
                .txtRight.Top = -3
                .txtRight.Left = -2
                .txtRight.Width = .fraRight.Width + 2
                .txtRight.Height = 16
                .txtRight.Text = .lstRight.List(.lstRight.ListIndex)
            End If
        End With
        
    End Sub

Input in Sheet1

QJRDf.jpg


userform with objets, names and properties

ccqyq.jpg


Results, 4 examples of random results

ByRtP.jpg
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Everything happens because I used the UserForm1 "Show" method in "vbModeless" mode. If I use it with "vbModal" the events trigger well.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top