Results 1 to 4 of 4

Thread: [RESOLVED] [2005] RevoveHandler Question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Resolved [RESOLVED] [2005] RevoveHandler Question

    I am having a little trouble with event handlers:

    I would like to disable (remove) the combobox event handler shown below so that the event will not fire when the value of the combobox is changed programmatically. The event was originally added using AddHandler.

    Code:
           
    1   RemoveHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
            
    2   Me.ComboBox1.SelectedValue = ""
    3   Me.ComboBox2.SelectedValue = ""
    4   Me.ComboBox9.SelectedValue = ""
    5   Me.SchoolFilterList.Text = ""
    
    6   AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
    The event for ComboBox1.SelectedIndexChanged still fires when the selected valve for combobox1 is set to “” in line 6. I can not see any code that might be ADDING multiple eventhandlers.

    Any thoughts on how to keep the combobox from firing during programatic changes are appricated in advance.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2005] RevoveHandler Question

    declare a form level boolean variable:

    vb Code:
    1. dim ignore as boolean = false

    check if ignore = true in your ComboBox1_SelectedIndexChanged event and only execute the code if its false. to not execute the code, set ignore = true before changing ComboBox1.SelectedIndex

  3. #3
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: [2005] RevoveHandler Question

    Did you add this ComboBox at runtime or in the design-view? I might be wrong on this but, I think that when adding a control at design-time, eventhandlers are automatically added for certain events. So if you're using AddHandler to add another eventhandler at runtime you might end up with multiple eventhandlers even though it wouldnt look like it.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    88

    Thumbs up Re: [2005] RevoveHandler Question

    The comboboxes are added at Design time. I commented out the Handles statement and used the AddHandler statement after the form loaded up. For example:


    Code:
    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DateTimePicker2.ValueChanged  
    
    	Becomes
    
    Private Sub DateTimePicker2_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) 'Handles DateTimePicker2.ValueChanged

    Then I use:

    Code:
    AddHandler DateTimePicker2.ValueChanged, AddressOf DateTimePicker2_ValueChanged
    My hope was that I could use the RemoveHandler statemen to control the event handling. Is this wrong?

    Regardless- I used Paul’s suggestion and it worked fine. Thanks

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