none
Combobox SelectedIndexChanged Event RRS feed

  • Question

  • Hi Bosses,

    I have created a window form with some combobox to which I associated code in the SelectedIndexChanged event. I noticed that the event is called during the loading of the form when. This is giving some error as the code in the event use some others resources (other controls and variables) that are not yet fully loaded. The same happened during the closing of the form. So is there any simple way to prevent the call of the event during the loading and the closing of the form? Or shoud I invent my own code to control that behavior? Thanks for nay helps.

    Saturday, December 30, 2017 9:09 AM

Answers

  • Hi Bosses,

    I have created a window form with some combobox to which I associated code in the SelectedIndexChanged event. I noticed that the event is called during the loading of the form when. This is giving some error as the code in the event use some others resources (other controls and variables) that are not yet fully loaded. The same happened during the closing of the form. So is there any simple way to prevent the call of the event during the loading and the closing of the form? Or shoud I invent my own code to control that behavior? Thanks for nay helps.

    A real simple way (literally one bit) is with a class-scoped  boolean:

    Public Class Form1
        Private _isLoading As Boolean = True
    
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
        End Sub
    
        Private Sub _
            Form1_Shown(sender As Object, _
                        e As System.EventArgs) _
                        Handles Me.Shown
    
            _isLoading = False
    
        End Sub
    
        Private Sub _
            ComboBox1_SelectedIndexChanged(sender As System.Object, _
                                           e As System.EventArgs) _
                                           Handles ComboBox1.SelectedIndexChanged
    
            If Not _isLoading Then
                ' Your code here...
            End If
    
        End Sub
    End Class

    You could also use RemoveHandler then later use AddHandler, but for the cost of just one single class-scoped boolean, it's hard to beat.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Proposed as answer by Cor Ligthert Saturday, December 30, 2017 10:33 AM
    • Marked as answer by Ekoue Sunday, December 31, 2017 10:59 AM
    Saturday, December 30, 2017 9:53 AM
  • Here is an AddHandler solution

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ComboBox1.DataSource = New List(Of String) From {"Karen", "Frank", "Cor", "Tommy"}
    
            AddHandler ComboBox1.SelectedIndexChanged,
                Sub(pSender As Object, pe As EventArgs)
                    Console.WriteLine(CType(pSender, ComboBox).Text)
                End Sub
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Cor Ligthert Saturday, December 30, 2017 2:07 PM
    • Marked as answer by Ekoue Sunday, December 31, 2017 11:00 AM
    Saturday, December 30, 2017 11:14 AM
    Moderator
  • Hi Bosses,

    I have created a window form with some combobox to which I associated code in the SelectedIndexChanged event. I noticed that the event is called during the loading of the form when. This is giving some error as the code in the event use some others resources (other controls and variables) that are not yet fully loaded. The same happened during the closing of the form. So is there any simple way to prevent the call of the event during the loading and the closing of the form? Or shoud I invent my own code to control that behavior? Thanks for nay helps.

    A real simple way (literally one bit) is with a class-scoped  boolean:

    Public Class Form1
        Private _isLoading As Boolean = True
    
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
        End Sub
    
        Private Sub _
            Form1_Shown(sender As Object, _
                        e As System.EventArgs) _
                        Handles Me.Shown
    
            _isLoading = False
    
        End Sub
    
        Private Sub _
            ComboBox1_SelectedIndexChanged(sender As System.Object, _
                                           e As System.EventArgs) _
                                           Handles ComboBox1.SelectedIndexChanged
    
            If Not _isLoading Then
                ' Your code here...
            End If
    
        End Sub
    End Class

    You could also use RemoveHandler then later use AddHandler, but for the cost of just one single class-scoped boolean, it's hard to beat.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Hi

    This is the way that I use to deal with those issues.


    Regards Les, Livingston, Scotland

    • Marked as answer by Ekoue Sunday, December 31, 2017 11:00 AM
    Saturday, December 30, 2017 12:41 PM

  • This is the way that I use to deal with those issues.


    Regards Les, Livingston, Scotland

    I did in past, but now I use the settings of the handler method. If you ask why, "I want to avoid global fields which have few places where they are used." 

    Success Cor

    • Marked as answer by Ekoue Sunday, December 31, 2017 11:01 AM
    Saturday, December 30, 2017 2:09 PM

All replies

  • Hi Bosses,

    I have created a window form with some combobox to which I associated code in the SelectedIndexChanged event. I noticed that the event is called during the loading of the form when. This is giving some error as the code in the event use some others resources (other controls and variables) that are not yet fully loaded. The same happened during the closing of the form. So is there any simple way to prevent the call of the event during the loading and the closing of the form? Or shoud I invent my own code to control that behavior? Thanks for nay helps.

    A real simple way (literally one bit) is with a class-scoped  boolean:

    Public Class Form1
        Private _isLoading As Boolean = True
    
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
        End Sub
    
        Private Sub _
            Form1_Shown(sender As Object, _
                        e As System.EventArgs) _
                        Handles Me.Shown
    
            _isLoading = False
    
        End Sub
    
        Private Sub _
            ComboBox1_SelectedIndexChanged(sender As System.Object, _
                                           e As System.EventArgs) _
                                           Handles ComboBox1.SelectedIndexChanged
    
            If Not _isLoading Then
                ' Your code here...
            End If
    
        End Sub
    End Class

    You could also use RemoveHandler then later use AddHandler, but for the cost of just one single class-scoped boolean, it's hard to beat.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    • Proposed as answer by Cor Ligthert Saturday, December 30, 2017 10:33 AM
    • Marked as answer by Ekoue Sunday, December 31, 2017 10:59 AM
    Saturday, December 30, 2017 9:53 AM
  • Here is an AddHandler solution

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
            ComboBox1.DataSource = New List(Of String) From {"Karen", "Frank", "Cor", "Tommy"}
    
            AddHandler ComboBox1.SelectedIndexChanged,
                Sub(pSender As Object, pe As EventArgs)
                    Console.WriteLine(CType(pSender, ComboBox).Text)
                End Sub
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Cor Ligthert Saturday, December 30, 2017 2:07 PM
    • Marked as answer by Ekoue Sunday, December 31, 2017 11:00 AM
    Saturday, December 30, 2017 11:14 AM
    Moderator
  • Hi Bosses,

    I have created a window form with some combobox to which I associated code in the SelectedIndexChanged event. I noticed that the event is called during the loading of the form when. This is giving some error as the code in the event use some others resources (other controls and variables) that are not yet fully loaded. The same happened during the closing of the form. So is there any simple way to prevent the call of the event during the loading and the closing of the form? Or shoud I invent my own code to control that behavior? Thanks for nay helps.

    A real simple way (literally one bit) is with a class-scoped  boolean:

    Public Class Form1
        Private _isLoading As Boolean = True
    
        Private Sub _
            Form1_Load(sender As System.Object, _
                       e As System.EventArgs) _
                       Handles MyBase.Load
    
        End Sub
    
        Private Sub _
            Form1_Shown(sender As Object, _
                        e As System.EventArgs) _
                        Handles Me.Shown
    
            _isLoading = False
    
        End Sub
    
        Private Sub _
            ComboBox1_SelectedIndexChanged(sender As System.Object, _
                                           e As System.EventArgs) _
                                           Handles ComboBox1.SelectedIndexChanged
    
            If Not _isLoading Then
                ' Your code here...
            End If
    
        End Sub
    End Class

    You could also use RemoveHandler then later use AddHandler, but for the cost of just one single class-scoped boolean, it's hard to beat.


    "A problem well stated is a problem half solved.” - Charles F. Kettering

    Hi

    This is the way that I use to deal with those issues.


    Regards Les, Livingston, Scotland

    • Marked as answer by Ekoue Sunday, December 31, 2017 11:00 AM
    Saturday, December 30, 2017 12:41 PM

  • This is the way that I use to deal with those issues.


    Regards Les, Livingston, Scotland

    I did in past, but now I use the settings of the handler method. If you ask why, "I want to avoid global fields which have few places where they are used." 

    Success Cor

    • Marked as answer by Ekoue Sunday, December 31, 2017 11:01 AM
    Saturday, December 30, 2017 2:09 PM

  • This is the way that I use to deal with those issues.


    Regards Les, Livingston, Scotland

    I did in past, but now I use the settings of the handler method. If you ask why, "I want to avoid global fields which have few places where they are used." 

    Success Cor

    Hi

    I have used the Handler method in the past, but, when I started to need to set the Handlers for several different controls when one global did the same job I stayed with the global method.


    Regards Les, Livingston, Scotland

    Saturday, December 30, 2017 2:26 PM
  • Many thanks

    You have all solved my problem. Both solutions will work an I will use them appropriately. Actually I created a Class object with 4 combo box for cascade selection. So it will be better to use the global boolean variable. When ever it one or two combo box, the add/remove handler instructions will do.

    Regards

    Ekoue

    Sunday, December 31, 2017 11:07 AM