-
Jan 30th, 2018, 01:48 PM
#1
Thread Starter
Junior Member
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
-
Jan 30th, 2018, 02:30 PM
#2
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
-
Jan 30th, 2018, 02:32 PM
#3
Thread Starter
Junior Member
Re: Event 'Disable'
That's an interesting idea tg, thanks
-
Jan 30th, 2018, 03:01 PM
#4
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.
-
Jan 30th, 2018, 03:40 PM
#5
Re: Event 'Disable'
 Originally Posted by Sitten Spynne
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|