Solved

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

Posted on 2006-11-21
6
2,211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

734 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