Solved

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

Posted on 2006-11-21
6
2,043 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
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.
This video discusses moving either the default database or any database to a new volume.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now