Checkbox events when Form is opened and closed fire when form is reloaded

妖精的绣舞 提交于 2019-12-07 10:00:25

It is not common to have to tell checks and radios to ignore events while loading the form. You just need an Ignore or Loaded flag:

Public Class Form1
   Private ignore As Boolean = True
   ...

 Private Sub Form1_Load(...
    ' do normal stuff

    ignore = False               ' should be the ONLY place it is set
 End Sub

Private Sub CheckBox2_CheckedChanged(...
   If ignore Then Exit Sub

End Sub

The Form Designer code will fire events as it creates the form and controls, which CAN be handy for initializing stuff but often it causes trouble. Some controls will even get the same event twice. There isnt really a "reload" action for forms. If you hide them, Show() won't fire the Load event again.

You can avoid the flag and manually add the handlers for the troublesome controls when the form loads, but that can be tedious if there are lots of them. Flags can be abused and misused, but if it is set in that one spot only, its fine.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!