0

I have grid and those grid is populate on Form's load event. At the end line of that event i am hooking method handler for my SelectionChanged event of this grid. I want to get current selected row's zero cell's 1 value. Unfortunately when i run program my SelectionChanged event method handler is called infinite times... And i have no idea why is that.

So its basically like this:

 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

some code which populating data to grid...

'here hooking up method after data is there already to not fire it up during grid population
   AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged

End Sub

and this is event handler method itself:

  Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs)
        RemoveHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged

        If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
            gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
            ItemPanelImgs.Items.Clear()
            'Dim images As New List(Of Article_Image)
            Dim selectedNummer As String = String.Empty

            selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()

            'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
            'ItemPanelImgs.DataSource = images
        End If

        AddHandler gridArtikels.SelectionChanged, AddressOf gridArtikels_SelectionChanged
    End Sub

P.S I am using to be concrete supergrid control from DotnetBar devcomponenets but it shouldn't be diffrent from ordinary controls behaviour.

What could be wrong here?

For those whom would like to debug here is sample app

EDIT: I also tried this way but its still going to infinitive loop...

  Public IgnoreSelectionChanged As Boolean = False

 Private Sub gridArtikels_SelectionChanged(sender As Object, e As GridEventArgs) Handles gridArtikels.SelectionChanged

        If IgnoreSelectionChanged Then Exit Sub


        IgnoreSelectionChanged = True

        If gridArtikels.PrimaryGrid.Rows.Count > 0 Then
            gridArtikels.PrimaryGrid.SetSelectedRows(0, 1, True)
            ItemPanelImgs.Items.Clear()
            'Dim images As New List(Of Article_Image)
            Dim selectedNummer As String = String.Empty

            selectedNummer = gridArtikels.PrimaryGrid.SelectedRows(0).Cells(1).Value.ToString()

            'images = ArtikelsAndTheirVariationsFinal.GetImagesForArticle(selectedNummer)
            'ItemPanelImgs.DataSource = images

        End If

        IgnoreSelectionChanged = False

    End Sub
  • I would think that by default, the handler is already added before the form.Load runs. You should remove the handler before the populating code runs and then add it again when it has finished. – David Wilson Apr 07 '16 at 12:22
  • @David Wilson good point but unfortunetly not - i checked :( –  Apr 07 '16 at 12:30
  • OK. Have you stepped though your code to see where it is firing by mistake? :) – David Wilson Apr 07 '16 at 12:44
  • Are you creating this control dynamically in code or is it an actual control on the form? Regardless, I'm not sure why you're adding the handlers you are, where you are. – Dave Brown Apr 07 '16 at 14:06
  • Is it even possible to remove and re-add an event handler, from within that same event handler? Presumably the runtime is currently enumerating the attached handlers in some way, so updating the list could be troublesome. Why not try setting a flag "IgnoreSelectionChanged" in your form at the beginning of the SelectionChanged handler. That seems more robust than continually removing and adding the handlers. – Stuart Whitehouse Apr 07 '16 at 14:39
  • @Stuart Whitehouse i tried as you mentioned but its still going to infinitive loop - check my main post in edit section how i tried it - if its as you mentioned maybe you could download program from provided link and check. I have no other clues whats going on. –  Apr 07 '16 at 17:37

0 Answers0