Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-21
6
Medium Priority
?
2,421 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 1500 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 86

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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Loops Section Overview
Screencast - Getting to Know the Pipeline

972 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