Dropdown with combo box closes unexpectedly when managing a tab

I have a modal form with one instance of the .NET 2.0 built-in tab control. The tab control has multiple pages, and one of them has a combo box that is not populated until the user activates it the first time. When this happens, I handle the DropDown event and start a process that takes a few seconds, then I add the items returned by that process to the combo box.

It works great, except that immediately after the list portion of the combo box has dropped, it closes immediately, as if some other control was taking focus. I've narrowed it down to the fact that there is a tab control in the form, and the process that retrieves the items for the combo box takes over 4 seconds. If I create a completely empty form with just a combo box, I can't see it.

Needless to say, this is strange outside of faith. Any idea why the tab control would interfere with the control that currently has focus?

EDIT:

Here is the event handler code for this combo box. Basically I am creating a list of SQL servers on the network. The thing that takes a few seconds is the GetDataSources call.

Private Sub cmbServer_DropDown(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbServer.DropDown
    Dim oTable As DataTable
    Dim lstServers As List(Of String)
    Dim lstAliases As List(Of String)

    Try
        If cmbServer.Items.Count = 0 Then
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor
            oTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources
            lstServers = New List(Of String)

            For Each oRow As DataRow In oTable.Rows
                If oRow("InstanceName").ToString = "" Then
                    lstServers.Add(oRow("ServerName").ToString)
                Else
                    lstServers.Add(oRow("ServerName").ToString & "\" & oRow("InstanceName").ToString)
                End If
            Next oRow

            'Retrieve any server aliases on the client and add them to the server list
            lstAliases = GetSQLServerAliases()
            If lstAliases IsNot Nothing Then
                For Each sAlias As String In lstAliases
                    lstServers.Add(sAlias)
                Next sAlias
            End If

            lstServers.Sort()
            For Each sServer As String In lstServers
                cmbServer.Items.Add(sServer)
            Next sServer
        End If
    Catch ex As Exception
        ErrHandler("frmRefreshDB", "cmbServer_DropDown", ex.Source, ex.Message, ex.InnerException)
    Finally
        System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default

        If oTable IsNot Nothing Then
            oTable.Dispose()
        End If
    End Try
End Sub

      

0


a source to share


1 answer


I was able to resolve this issue.

I first added a breakpoint to the LostFocus event in the combo box and looked at the stack trace as suggested by Steve Dignan, but that didn't find anything. One feature of my form is that this combo box is usually disabled and only activated when the user checks the field on the same bookmark. The solution was to explicitly set focus on the combo box when the field was checked, like this:



Private Sub chkAltServer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles chkAltServer.CheckedChanged
    Try
        If chkAltServer.Checked Then
            UnlockControl(cmbServer)
            cmbServer.Focus()
        Else
            LockControl(cmbServer)
        End If
    Catch ex As Exception
        ErrHandler("frmOptions", "chkAltServer_CheckedChanged", ex.Source, ex.Message, ex.InnerException)
    End Try
End Sub

      

The combo box dropdown showed fine.

0


a source







All Articles