none
Visual studio 10 auto execute the event on load (Windows Form) RRS feed

  • Question

  • Hi,

    i have a problem with my Visual Studio 10, when im trying to run the Windows Form. It was OK before, but now everytime i run in the debug mode, the VS auto fire the event during the form load. The event like "_SelectedIndexChanged" was OK before, but now it suddenly fire up upon form load causing an error in invalid cast. i suspect it related to my VS environtment, but i don't know why. 

    When i rebuild and produce installation file (setup.exe), and install the application (that i develop), its running perfectly. Just dont know why during the debug mode the VS fire the event itself.

    Please help.

    The Event auto fire up upon form load cause the error:

        Private Sub cbxReportname_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbxReportname.SelectedIndexChanged
            Try
                Dim varSelcReport As Integer = Integer.Parse(cbxReportname.SelectedValue)
                Dim msgs As String = glodQueri.GetDefaultDataString("REPORTMSG", "T_SHM_DEFAULT_REPORT", " Where REPORTID = " & varSelcReport)
                lb_message.Text = msgs.ToString
                MessageBox.Show(msgs)
            Catch ex As Exception
                lb_message.Text = ""
            End Try
        End Sub

    Immediate window exception:

    A first chance exception of type 'System.FormatException' occurred in mscorlib.dll
    A first chance exception of type 'System.FormatException' occurred in Microsoft.VisualBasic.dll
    A first chance exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

     


    • Edited by John.Avex Wednesday, August 28, 2019 3:23 AM
    Wednesday, August 28, 2019 3:18 AM

Answers

  • Hi John.Avex,

    Welcome to MSDN forum.

    According to your description, maybe you could try "SelectionChangeCommitted" event to do this function, then check whether it throw exception:

        Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
            Dim senderComboBox As ComboBox = CType(sender, ComboBox)
    
            'judge if the combobox is null
            If senderComboBox.SelectionLength > -1 Then
                Dim varSelcReport As Integer = Integer.Parse(cbxReportname.SelectedValue)
                MessageBox.Show(varSelcReport)
            End If
        End Sub

    And about exceptions, please refer more from here: A first chance exception and Avoiding first chance exception

    It seems that it is normal behavior for dbugger, and you don't need to worry them, and you could try the methods from link to ignore them.

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    • Marked as answer by John.Avex Thursday, August 29, 2019 1:05 AM
    Wednesday, August 28, 2019 10:20 AM

All replies

  • Hi,
    I'm afraid you need to explain what "glodQueri.GetDefaultDataString" is and what values are in "cbxReportname".
    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Wednesday, August 28, 2019 7:22 AM
  • Hi,

    sorry about that. 

    The problem is the Form_Load event auto execute the "System.EventArgs" :

    "Private Sub cbxReportname_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbxReportname.SelectedIndexChanged"

    causing it to run the:

    Try
                Dim varSelcReport As Integer = Integer.Parse(cbxReportname.SelectedValue)    <<- error here
            Catch ex As Exception
                lb_message.Text = ""
    End Try

    the error because it convert to cbxReportname.selected value to integer. 

    In my Form_Load, it only execute the SQL query to bound dataset to "cbxReportname".  The "SelectedIndexChanged" should not execute automatically during form_load right? it was run perfectly before, even i already build the installer (.exe) for it, some how after Windows update (Windows 10 Pro), it suddenly got this error. i believe it related to the VS 2010 debug environtment, but i dont know.

    it is like this:

        Private Sub frmReportMaster_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            loadReportItem()
        End Sub

        Private Sub loadReportItem()
            Dim dset As New DataSet
            Dim strqueri As String = " SELECT     REPORTID, REPORTNAME FROM T_REPORTS  "
            glodbcon.ExecuteStringQuery(strqueri, dset)
            Dim dt As DataTable = New DataTable()
            cbxReportname.DataSource = dset.Tables(0)
            cbxReportname.DisplayMember = "REPORTNAME"
            cbxReportname.ValueMember = "REPORTID"
        End Sub

        Private Sub cbxReportname_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles cbxReportname.SelectedIndexChanged
            Try
                Dim varSelcReport As Integer = Integer.Parse(cbxReportname.SelectedValue)            
                MessageBox.Show(varSelcReport )
            Catch ex As Exception
                lb_message.Text = ""
            End Try
        End Sub


    • Edited by John.Avex Wednesday, August 28, 2019 7:42 AM
    Wednesday, August 28, 2019 7:42 AM
  • Hi,

    I guess "cbxReportname.SelectedValue" is null or blank when loadReportItem is executed, and it cannot be parsed to Integer.
    Please check what value is in cbxReportname.SelectedValue, using MsgBox or DebugWriteline.

    Regards,

    P.S.
    I hope you will insert a Code Block for your source code.

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Wednesday, August 28, 2019 8:34 AM
  • Hi,

    now is use Remove/Add Handler and its working.

        Private Sub frmReportMaster_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            RemoveHandler cbxReportname.SelectedIndexChanged, AddressOf cbxReportname_SelectedIndexChanged
            loadCbxReportMaster() 
            AddHandler cbxReportname.SelectedIndexChanged, AddressOf cbxReportname_SelectedIndexChanged
        End Sub

    but if i have many combobox that have event, i have to enter it one by one. 

    Or if there is any other solution?

    Thanks btw, guess have to use that method as at now.

    Wednesday, August 28, 2019 8:59 AM
  • Hi John.Avex,

    Welcome to MSDN forum.

    According to your description, maybe you could try "SelectionChangeCommitted" event to do this function, then check whether it throw exception:

        Private Sub ComboBox1_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox1.SelectionChangeCommitted
            Dim senderComboBox As ComboBox = CType(sender, ComboBox)
    
            'judge if the combobox is null
            If senderComboBox.SelectionLength > -1 Then
                Dim varSelcReport As Integer = Integer.Parse(cbxReportname.SelectedValue)
                MessageBox.Show(varSelcReport)
            End If
        End Sub

    And about exceptions, please refer more from here: A first chance exception and Avoiding first chance exception

    It seems that it is normal behavior for dbugger, and you don't need to worry them, and you could try the methods from link to ignore them.

    Any feedback will be expected.

    Best Regards,

    Dylan


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com

    • Marked as answer by John.Avex Thursday, August 29, 2019 1:05 AM
    Wednesday, August 28, 2019 10:20 AM
  • Hi,

    first of all, thank you all.

    It is the Debug setting that cause the error, first chance exception. now i can continue my project. thanks Dylan Zhu.

    Just for info for anyone have same problem:

    Visual Studio 2010:

    Debug > Execptions > Common Language Runtime Execptions > System > System.ApplicationException (<- check this ->)

    Thanks.


    • Edited by John.Avex Thursday, August 29, 2019 1:16 AM
    Thursday, August 29, 2019 1:15 AM