Solved

How do I disable the textchanged event of a textbox once it has been fired?

Posted on 2006-11-21
6
2,131 Views
Last Modified: 2008-02-01
I have VS2005. I have on a form a textbox(TextBox1) and a datagrid.  A barcode is entered into TextBox1 with a barcode scanner which fires the textchanged event and loads data from the database into the datagrid that coresponds to the barcode entered. I'm trying to set this up so I can enter more than one barcode to bring up more data for each barcode. I found using the textchanged event that when I clear just one number from the barcode in TextBox1 that the entry in the datagird clears. The textchanged event still fires clearing the entry from the datagrid. I would like to clear TextBox1 without clearing the entry in the datagrid. Is there a way to disable the textchanged event and then re-enable it when a new row is added to the datagrid?

Thanks
0
Comment
Question by:grouchyfd
6 Comments
 
LVL 17

Accepted Solution

by:
ZeonFlash earned 500 total points
ID: 17990597
You can add/remove the event handler like this:

RemoveHandler TextBox1_TextChanged, AddressOf TextBox1.TextChanged
AddHandler TextBox1_TextChanged, AddressOf TextBox1.TextChanged
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 17990702
An alternative to using text change event, my scanner simulates an enter keypress when scanned so this is what I use

    Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, ByVal keyData As System.Windows.Forms.Keys) As Boolean


        If msg.WParam.ToInt32() = CInt(Keys.Enter) Then
            If Len(txtscan.Text) = 9 Then
                Me.DialogResult = System.Windows.Forms.DialogResult.OK
                Return True
            Else
                MsgBox("Please Enter A Valid Sales Order")
            End If
        End If

        Return MyBase.ProcessCmdKey(msg, keyData)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 17990725
You can just set a flag to ignore the event:

Public Class Form1

    Private ClearFlag As Boolean = False

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If Not ClearFlag Then
            Debug.Print("TextChanged()")

        End If
    End Sub

    Private Sub ClearTextBox()
        ClearFlag = True
        TextBox1.Clear()
        ClearFlag = False
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ClearTextBox()
    End Sub

End Class
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Author Comment

by:grouchyfd
ID: 17990798
Here is the sub to enter the data from the database that coresponds to the barcode:

Private Sub txtBarcode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBarcode.TextChanged
        StockStatusTableAdapter.FillByBarcode(VStockStatus.StockStatus, txtBarcode.Text)

    End Sub
When I added the RemoveHandler event by itself, the datagrid would not load. When I add the AddHandler then the datagrid loads but then as soon as you backspace one digit of the barcode, it still clears out the data from the datagrid(the same as wihtout the RemoveHandler and AddHandler). What am I doing wrong here?
0
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 17992006
is this what you tried?

Private Sub txtBarcode_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtBarcode.TextChanged
        StockStatusTableAdapter.FillByBarcode(VStockStatus.StockStatus, txtBarcode.Text)
        removehandler txtBarcode.TextChanged, addressof txtBarcode_TextChanged
    End Sub
0
 

Author Comment

by:grouchyfd
ID: 18012028
I appologize for the long delay. Yes, that is what I tried.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error handling in asp.net site 5 28
SqlServer no dupes 25 35
Sql server insert 13 31
Get distinct values from excel or comm seperated file 4 25
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question