Data won't show on form until user clicks on form RRS feed

  • Question

  • ok I don't get this at all. Please help.

    I have a MDI windows app. I have one child form let's call Form1. In Form1 I have this code:

    Public Sub New(ByVal Barcode As String, ByVal FirstDate As String) 

            FromForm2 = True
            FI_TransDate = FirstDate

            ' This call is required by the Windows Form Designer.

            'IF it comes here means that it came from Issue
            'Now query the db to get all the info you need then fill in all the fields on Form1.

                con = New SqlConnection(connectionString)

                'Fill the UOM Drop Down with all System UOM
                sql = "Select Barcode, Location, Amount, UOM, UseCode, Source, EmpMyID, Comments, MSDSID " & _
                    "FROM db WHERE Barcode = '" & Barcode & "'"
                If con.State = ConnectionState.Closed Then
                End If

                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                cmd.CommandText = sql

                dr = cmd.ExecuteReader

                If dr.HasRows = True Then
                    txtUsgBarcode.Text = Trim(dr("Barcode").ToString)
                    FI_Amount = dr("Amount")
                    FI_UOM = dr("UOM").ToString
                    FI_Location = dr("Location").ToString
                    FI_UseCode = dr("UseCode").ToString
                    FI_Source = dr("Source").ToString
                    FI_EmpMyID = dr("EmpMyID").ToString
                    FI_Comments = dr("Comments").ToString
                    FI_MSDSID = dr("MSDSID").ToString

                End If

                If con.State = ConnectionState.Open Then
                End If
            Catch ex As Exception
                Dim sBaseClass As New BaseClass

                sBaseClass.DisplayError("frmUsage_New()", ex)

            End Try


        End Sub

    NOTICE THE "txtUsgAmount.Focus()" CALL AT THE END OF THE METHOD. On Form1 there are 2 textboxes txtUsgBarcode and txtUsgAmount txtUsgBarcode gets the focus by setting the TabIndex to 0. Why is this? Because when user normally open Form1 we want the txtUsgBarcode to have the focus.

    On the child Form2 I have this code:

    Private Sub Button1_Click(sender As Object, e As System.EventArgs) Handles lvIssNoDates.Click
            Dim vLU As New Form1(sBarcode, sIssueDate)
           vLU.MdiParent = Me.MdiParent
        End Sub

    So when the user clicks on Button1 on Form2 it sends "sBarcode" and "sIssueDate" to Form1.

    And as you can see from the "Public Sub New" at the top Form1 it uses "sBarcode" and "sIssueDate" from Form2 to get information from the database.

    Now after the code goes through the "Public Sub New" at the top Form1 it then hits the Form2 Load method.

    OK here is where it get strange. If I set a breakpoint any where in the "Public Sub New" at the top Form1 or any where in the Form2 Load method and then run the code and then step line by line through the code when the code gets to the end of the Form2 Load method it directly goes to the txtBarcode_LostFocus (Remember the "txtUsgAmount.Focus()" at the end of the "Public Sub New"). Which makes since to me, Form1 loads so txtBarcode gets focus but then in code "txtUsgAmount.Focus()" is called and that is why txtBarcode_LostFocus gets called.

    That is what I want to happen. BUT if I remove all the breakpoints but set one on txtBarcode_LostFocus and then run the code and click on Button1on Form2 just like  I did before txtBarcode_LostFocus DOES NOT GET HIT!!!!!!

    And on the screen, Form1 appears but does not fill in any information on the fields as like it would do if txtBarcode_LostFocus was hit.

    And another strange thing. Like I said Form1 appears on the screen with no info on the screen. But if I click anywhere on the app with my mouse txtBarcode_LostFocus gets hit and all the information fills in on Form1????

    If I click anywhere on the code in VS 2010, txtBarcode_LostFocus gets hit and all the information fills in on Form1????


    Thank you

    Saturday, April 6, 2013 4:51 PM


  • TextBox.Focus will return a value of true if the focus was successfully set.  It almost always would return a value of true, but, perhaps you should check the return value.

    Have you considered moving this statement from the constructor to the Shown event handler?  Perhaps it is just my style, but that is what I would normally do.


    • Proposed as answer by Bob Wu-MT Thursday, April 11, 2013 7:27 AM
    • Marked as answer by Mike Feng Sunday, May 5, 2013 5:57 AM
    Sunday, April 7, 2013 11:06 AM