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

后端 未结 10 2395
清歌不尽
清歌不尽 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:43

    Just in case anyone is still searching for this the event is fired upon initializing the form BUT the form is not yet visible, Also Say that you have a foreign key relationship upon which you have a default value needed issue that gets fired every row update too. So the following code worked for me....

        if (Visible && !(e.ColumnIndex == 0))
            {
                phoneEdited = true;
                MessageBox.Show("A Phone entry has been entered");
            }
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-03 07:48

    "I also can put 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.

    0 讨论(0)
  • 2020-12-03 07:48

    I put a public variable in the Module1 file Dim Public bolForm_LoadingTF as Boolean = True

    In each formLoad event I put bolForm_LoadingTF = True

    In each control with an OnSelectedIndexChanged event I put if bolForm_LoadingTF = True then Exit Sub

    At the end of the form load event I put bolForm_LoadingTF = False

    I am probably breaking a bunch of rules but this works for me.

    0 讨论(0)
  • 2020-12-03 07:52

    For radiobutton see Hans Olsson answer

    For numeric up down, do it like this

    Private Sub myNumeric_ValueChanged(sender As Object, e As EventArgs) Handles myNumeric.ValueChanged
            If myNumeric.Value >= 0 AndAlso myNumeric.IsHandleCreated Then
                'Do the work
            End If
    End Sub
    

    The keyword is myNumeric.Value and IsHandleCreated

    0 讨论(0)
  • 2020-12-03 07:53

    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.

    Code Example:

    Private Sub myRadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles myRadioButton.CheckedChanged
    If myRadioButton.Checked AndAlso myRadioButton.IsHandleCreated Then
        'Do Work
    End If
    End Sub
    
    0 讨论(0)
提交回复
热议问题