How to prevent value changed events from firing on form initialization in .NET?

后端 未结 10 2441
清歌不尽
清歌不尽 2020-12-03 07:01

Consider a simple .NET form with a couple of radio buttons and a checkbox.

Each of the radio buttons has a CheckedChanged handler setup that performs some action bas

10条回答
  •  悲&欢浪女
    2020-12-03 07:44

    The easy solution is to declare an 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 sophisticated: Remove 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
    

提交回复
热议问题