Results 1 to 7 of 7

Thread: [RESOLVED] Multiple text boxes using one timer?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Resolved [RESOLVED] Multiple text boxes using one timer?

    Is it possible to have multiple textbox use one timer. What I have is a tabcontrol that shows a datagrid. I have text boxes to filter the grid. so I use a timer control for the keyup on the textbox. Just wanted to clean up the code and maybe get rid of 10 timers. lol
    Code:
     Private Sub TxtMachineSearchQuick_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtMachineSearchQuick.KeyUp
    
            'Restart the filter delay.
            Me.Timer1.Stop()
            Me.Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick        
            Me.Timer1.Stop()
            LicensesBindingSource.Filter = "Machine Like '" & TxtMachineSearchQuick.Text & "%'"
        End Sub
    
    
        Private Sub TxtUserIDSearchQuick_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TxtUserIDSearchQuick.KeyUp
            'Restart the filter delay.
            Me.Timer2.Stop()
            Me.Timer2.Start()
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer2.Tick
    
            Me.Timer2.Stop()
            LicensesBindingSource.Filter = "UserID Like '" & TxtUserIDSearchQuick.Text & "%'"
        End Sub
    That si for one tab that has two textboxes on it. can they use the same timer? of course each textbox would fire alone.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Multiple text boxes using one timer?

    Generally, yes. The problem you may run into is if two of the textboxes were changed in short order. I'm not quite sure why you are using the timers in the first place, as they don't seem to do anything other than delay a reasonable response, but is the delay long enough that a person might trigger a second one before the first one has finished? If that's the case, then you will have trouble, as the second trigger will restart the first, and it will be a bit hard to figure out what to do when the timer ticks.

    You will probably need an integer variable at form scope, or an enum. In each of the KeyUp event handlers, when you start the timer, also set that variable to a unique value. An enum would be good for this, but in this case you could also just say: 1 = UserID, 2=Machine, 3=Whatever,etc...

    In the timer tick event, you'd have a select case statement that switched on that form level variable and did a different filter for each value. If the timer ticked too fast, you could OR together values (you'd have to use 1, 2, 4, 8, 16, etc. rather than 1, 2, 3, 4) and set up the code in the timer tick to filter on both. That seems unlikely to be necessary, though.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Multiple text boxes using one timer?

    Thanks Shaggy,

    Yes, the timer is all for a slight delay in the datagrid view for each filter. And yes, each box can override what the last one did. it doesn't mess anything up, just changes the view as one would expect

    I will look into the enum. Filtering on more than one is a good idea as it has a slight chance of happening, I will have to see about making that work, thanks for the thought, lol

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Multiple text boxes using one timer?

    Filtering on more than one wouldn't be too hard to implement. The key is that each value would have to be a power of 2 so that they could be ORed together. You can do that in an enum easily enough:

    Public Enum Filters
    UserID = 1
    Machine = 2
    SomeOther = 4
    End Enum

    What gets different is that in the timer tick you wouldn't be able to use a select case the same way. What you might do is a series of If statements:

    Code:
    If (myFilters And 1) > 0 Then
     'Filter on UserID
    End If
    
    If (myFilters And 2) > 0 Then
     'Filter on machine.
    End If
    
    If (myFilters And 4) > 0 Then
     'Filter on something else.
    End If
    The key difference is that if the value can only be one thing, then a select case would be the way to go, but if it was a bunch of things ORed together (or if you had a Boolean for each of the different values rather than ORing them into an integer, which is an option), then they aren't exclusive and you have to check each one individually.
    My usual boring signature: Nothing

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: Multiple text boxes using one timer?

    By the way, why are you pausing?
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: Multiple text boxes using one timer?

    Thanks shaggy, doing the enum worked like a charm.

    without the pause it happens so fast it doesn't look like it is working, if there are a lot of results returned.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    Re: [RESOLVED] Multiple text boxes using one timer?

    That's a pretty amusing problem to have.
    My usual boring signature: Nothing

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width