Results 1 to 7 of 7

Thread: Update event of a checkbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Update event of a checkbox

    Hello,

    maybe this is a very simple question, and I am blind.

    I have 2 checkboxes that I am filling with values from a select of a database but let the checkbox empty / not showing any value at the start. I would like to update a data grid once the user selects one of those values.

    But I do NOT want that this event is already triggered by the initial filling of the checkbox. Can this be avoided programmatically?

    Many thanks

    Christian

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,478

    Re: Update event of a checkbox

    The CheckedChanged event of the CheckBox will be raised if you set the Checked property so there's nothing you can do about that. There are two ways this is usually handled. One option is to not handle the event until the initialisation is complete, which means using AddHandler to attach the event handler after that. The other option is to use a Boolean field that you toggle after the initialisation and check the value of in the event handler and only act if it has been toggled.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2021
    Posts
    2

    Re: Update event of a checkbox

    Thank you so much for the quick response, but I am not sure if I fully understand this. Can you give me maybe an example for one of the options?

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,478

    Re: Update event of a checkbox

    You posted three minutes after I did. How much effort did you really put into trying to understand it. There's really not much there. Declare a field, set its value, check its value. What's not to understand? How about you give it a proper go and come back if you really can't work it out, rather than you can't be bothered to work it out?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,475

    Re: Update event of a checkbox

    If you are talking about the Boolean, it always feels a bit dirty, but it ends up looking something like this in the CheckedChanged event handler:
    Code:
    If yourBoolean Then
     'Take Action.
    End If
    In that way, the code in the event handler only happens when the Boolean is set. What feels dirty about it is that it isn't contained within the control, but that's life.
    My usual boring signature: Nothing

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    107,478

    Re: Update event of a checkbox

    Quote Originally Posted by Shaggy Hiker View Post
    What feels dirty about it is that it isn't contained within the control, but that's life.
    If one was really keen, one could make it part of the control. You could add a Boolean property, override the OnCheckedChanged method and then only call the base method, which is what raises the event, if the property is set, e.g.
    vb.net Code:
    1. Public Class CheckBoxEx
    2.     Inherits CheckBox
    3.  
    4.     Public Property SuppressCheckedChanged As Boolean = False
    5.  
    6.     Protected Overrides Sub OnCheckedChanged(e As EventArgs)
    7.         If Not SuppressCheckedChanged Then
    8.             MyBase.OnCheckedChanged(e)
    9.         End If
    10.     End Sub
    11.  
    12. End Class
    You can then set SuppressCheckedChanged to True before the initialisation and to False after. Doing it this way means that you have to set the property for each control, rather than one flag for all.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    36,475

    Re: Update event of a checkbox

    I'm not that keen.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width