VS 2017 Event 'Disable'-VBForums
Results 1 to 5 of 5

Thread: Event 'Disable'

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    9

    Question Event 'Disable'

    Hi Folks,
    In this Windows Forms application I have code which causes numerous events in different places, one example would be it changes the text being displayed in a cell of an unbound DataGridView. This will obviously cause an event and the appropriate handler will start executing, but is there a way to stop that happening?
    I can clearly set a global variable before the text change and then exit the handler if it finds the variable set, but this seems very messy, and error prone, but, tbh, I can't think of anything better.
    Thanks

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    31,469

    Re: Event 'Disable'

    That's one way... the other is to no use the handles clause to hook up the event handler, and to do it yourself, in code with AddHandler... then when you want to "disable" the event, you use RemoveHandler to unhook it.
    Example:
    Code:
    AddHandler Button1.Click, AddressOf Button1_Click
    
    ...
    RemoveHandler Button1.Click, AddressOf Button1_Click
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    9

    Re: Event 'Disable'

    That's an interesting idea tg, thanks

  4. #4
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,462

    Re: Event 'Disable'

    Sadly, having a variable that tells you to ignore the event is sometimes the only approach. This is a super common problem with TextChanged handlers. Usually it looks something like:
    Code:
    Sub TextBox1_TextChanged(...)
        If SomeCondition() Then
            TextBox1.Text &= "a"
        End If
    End Sub
    In some circumstances, that'll infinite loop. So we tend to guard it like this:
    Code:
    Private _isCorrectingTextBox As Boolean = False
    
    Sub TextBox1_TextChanged(...)
        If _isCorrectingTextBox Then
            Return
        End If
    
        If SomeCondition() Then
            _isCorrectingTextBox = True
            TextBox1.Text &= "a"
            _isCorrectingTextBox = False
        End If
    End Sub
    It's a pain in the butt, but it is what it is.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  5. #5
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,498

    Re: Event 'Disable'

    Quote Originally Posted by Sitten Spynne View Post
    Sadly, having a variable that tells you to ignore the event is sometimes the only approach. This is a super common problem with TextChanged handlers. Usually it looks something like:
    Code:
    Sub TextBox1_TextChanged(...)
        If SomeCondition() Then
            TextBox1.Text &= "a"
        End If
    End Sub
    In some circumstances, that'll infinite loop. So we tend to guard it like this:
    Code:
    Private _isCorrectingTextBox As Boolean = False
    
    Sub TextBox1_TextChanged(...)
        If _isCorrectingTextBox Then
            Return
        End If
    
        If SomeCondition() Then
            _isCorrectingTextBox = True
            TextBox1.Text &= "a"
            _isCorrectingTextBox = False
        End If
    End Sub
    It's a pain in the butt, but it is what it is.
    The same approach can be used in TextChanged too, the following code behave the same as your code which use _isCorrectingTextBox flag

    Code:
        Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
            If TextBox1.Text.Length < 8 Then
                RemoveHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged
                TextBox1.Text &= "a"
                AddHandler TextBox1.TextChanged, AddressOf TextBox1_TextChanged
            End If
        End Sub



Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width