views:

43

answers:

3

Consider a simple VB.Net form with a couple radio buttons and a checkbox.

Each of the radio buttons has a CheckedChanged hanlder setup that performs some action based on the state of the checkbox.

My problem is, when I initialize on the default radiobutton to be checked (from the designer properties window) the CheckedChanged event is fired for that radio button, but the Checkbox hasn't been initialized yet so I either get a null pointer exception or the wrong value is used in the handler. Either way I don't want that handler code to be run unless the user picks a radio button after the form has been loaded.

I currently get arround this by not initializing the radio button, but I need to set that default eventually and the best place is from the designer. I also can but a boolean field that's not set to true until the form is fully loaded and not process the events if that is false, but it's a dirty hack.

What can I do to prevent that handler from running it's code?

+2  A: 

"I also can but a boolean field that's not set to true until the form is fully loaded and not process the events if that is false, but it's a dirty hack."

It's also the easist and best way to do it!

Lets say .NET provides a neat way to turn an and off all the event handlers until the form is loaded. Even just the ones YOU are handling. It would still not be sufficiently flexible to disable what you wanted to enable but disable what you didn't. Often form setups happen and you want the events to fire. Also the form won't build right if no events fire.

FastAl
Still feels dirty :)
CodeFusionMobile
I had to take a shower after I wrote this answer.
FastAl
+2  A: 

The easy Solution declare a Initializing variable:

  Private Initializing as boolean = True

  Private Sub rb_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbNuevos.CheckedChanged, RbDesaparecidos.CheckedChanged, RbModificados.CheckedChanged, RbNoDesap.CheckedChanged, RbDesHoy.CheckedChanged, RbChT.CheckedChanged
      if Initializing then return

      'Your Code

  End Sub

  Public Sub New()

       ' Llamada necesaria para el Diseñador de Windows Forms.
       InitializeComponent()    
       ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

       initializing = false
  end sub

Most sofisticated: Remvove the "Handles" from the Method, and use AddHandler on the new method.

  Public Sub New()

       ' Llamada necesaria para el Diseñador de Windows Forms.
       InitializeComponent()    
       ' Agregue cualquier inicialización después de la llamada a InitializeComponent().

       AddHandler RbChT.CheckedChanged, AddressOf rb_CheckedChanged
  end sub
x77
To add the new method: Write : "Sub New" and press return
x77
+1  A: 

To make it feel slightly less dirty, if you initialize the controls in the constructor of the form you might be able to use the forms IsHandleCreated property rather than your own bool to check if it should actually validate or not.
I would think that normally you wouldn't want to validate anything before it's been shown for the first time and handle isn't created until it is.

ho1
Yes this is less dirty, and it works! I had the same problem, and checking for the forms `IsHandleCreated` in the events solved everything!
awe