0

I have a bunch of panels that I am adding to a single parent panel and I want to add event listeners to all of the panels but not until after they have all been added to the parent (becuase I don't want the event listeners firing each time a new panel gets added). So I am using the following code:

   Dim temp_object As question_bar = Nothing
   For Each q As Object In review_holder.Controls
       If TypeOf q Is question_bar Then
          temp_object = q
          AddHandler temp_object.Resize, AddressOf temp_object.resize_me
       End If
   Next

   For Each q As Object In review_holder.Controls
      If TypeOf q Is question_bar Then
         temp_object = q
         temp_object.resize_me()
      End If
   Next

But I noticed that the resize_me() subroutine is getting fired twice for each control. I only want it to fire once. So I traced it out using this code

MsgBox((New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name)

and I see that each time it gets called the calling methods are both this subroutine and _Lambda$_365. What the heck is that? How do I find out where that is coming from?

BTW, this is a winforms app using VS2012.

EDIT ------------------------------------------------------------------------

Public Sub resize_me()

 MsgBox((New System.Diagnostics.StackTrace).GetFrame(1).GetMethod.Name)

 If Me.minimized = True Then
    Me.Height = 0
    Exit Sub
 End If

 number_panel.Width = my_parent.number_width
 number_text.Width = my_parent.number_width
 number_separator.Left = number_panel.Right
 question_panel.Left = number_separator.Right
 question_panel.Width = question_panel.Parent.Width * initial_question_width + (question_padding * 2)


End Sub
John
  • 1,310
  • 3
  • 32
  • 58
  • Why the second loop? When you add your control to your panel the Resize event handler is automatically called so you don't need to call it again in the second loop – Steve Jun 08 '15 at 19:23
  • The control is previously added to parent control. The first loop simply adds the event handler to them, it doesn't call the event. If I leave off the second loop, then none of the panels get resized. – John Jun 08 '15 at 19:36
  • Then what is your code inside the resize_me method? – Steve Jun 08 '15 at 19:46
  • Just some basic height and width changes: (edited to add code to question) – John Jun 08 '15 at 19:48
  • I agree with @Steve, second loop isn't needed. Just move the `temp_object.resize_me()` call inside the if statement on the first loop after adding the event handler. also, it's quite possible that the controls are calling the `resize_me()` method as when you change the width it could be firing the `Resize` event of the control, calling your subroutine again. – RianBattle Jun 08 '15 at 19:56

1 Answers1

0

Well changing size properties when you are inside a resize event could explain why your code is recalled again a second time. Usually I try to avoid this kind of situations but this is not always possible. In these cases then a global variable that acts as a flag to block the reentry could save the day

Dim insideResize As Boolean

Public Sub resize_me()

 if insideResize = True Then
     Exit Sub
 End if

 insideResize = True
 Try
    If Me.minimized = True Then
       Me.Height = 0
       Exit Sub
    End If

    number_panel.Width = my_parent.number_width
    number_text.Width = my_parent.number_width
    number_separator.Left = number_panel.Right
    question_panel.Left = number_separator.Right
    question_panel.Width = question_panel.Parent.Width * initial_question_width + (question_padding * 2)
  Finally
    insideResize = False
  End Try

End Sub

To stay on the safe side with this patterns remember to always use a Try/Finally block to be sure that when you exit from the Resize event the global flag is correctly set back to false.

Steve
  • 213,761
  • 22
  • 232
  • 286
  • Thanks for the code. It seems like a good idea and I have added it to my code, but unfortunately nothing has changed. It is still getting called twice. The reason the call to the resize event is in a second loop is because I need to wait until all of the controls have been added so that they all end up the same size. If I call it it on the first panel and the second panel is larger than the first, then I need to call it on the first panel again. If I wait until all are in, then I simply need to call it once for each and they all take on the same size. – John Jun 08 '15 at 20:07