dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] Timer sub issue

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    55

    Resolved [RESOLVED] Timer sub issue

    Hello to the community. I have a small issue with the code below. The first two parts are the same and the third it's a sub routine. My problem it this.
    With the first code if an error occurs, it pops up a warning message (txtval sub routine) only once.
    With the second code if an error occurs, the warning message goes to a ''loop'' and it keeps poping up unless I close the program. How I can fix that? I need the warning message to pop just once.

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    If CheckBox1.Checked = True Then
      Try
      Call txtval(sender)
    
      MY CODE
      MY CODE
      MY CODE
    
    Catch ex As Exception
       MsgBox("Runtime error. Invalid output. Check again input variables", MsgBoxStyle.Exclamation, "ERROR")
    
       End Try
    End If
    
    End Sub


    Code:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    
    If CheckBox1.Checked = True Then
      Try
      Call txtval(sender)
    
      MY CODE
      MY CODE
      MY CODE
    
    Catch ex As Exception
       MsgBox("Runtime error. Invalid output. Check again input variables", MsgBoxStyle.Exclamation, "ERROR")
    
       End Try
    End If
    
    End Sub

    Code:
        
    Sub txtval(ByVal sender As System.Object)
    
    For Me.J = 0 To 3
    If Not IsNumeric(txt(J).Text) Or txt(J).Text = "" Then
    MsgBox("Please enter only numbers and fill in all neccessary fields!", MsgBoxStyle.Critical, "WARNING")
    Exit Sub
    End If
    Next J
    End Sub

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,565

    Re: Timer sub issue

    This is why I stop the timer at the top of the Tick event... and turn it back on at the bottom... prevents messages from stacking up.

    But I don't think that's quite what you meant... and as far as showing it once and only once per app run... think about it for a moment... If you want to display the message box once and only once, what would be the logical thing to do? Create a flag, set it to false by default... then you can check the flag, if it isn't set, then show the message box and turn the flag on.... the next time it comes back around, the flag will be set and the message box won't be shown.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,320

    Re: Timer sub issue

    Stop the timer when the message occurs.

    So, in the catch statement you could just have something like Timer1.Stop, but that's only one thing to change.

    It seems to me that you probably want that third sub to be a function that returns a boolean, though. Basically, it is just checking some textboxes, but if it returns False after the message shows, and True if the message never shows, then you can stop the timer as needed. For example, you could do this:

    Code:
       
    Function txtval() As Boolean
    Dim d As Double
    
    For Me.J = 0 To 3
     If Not Double.TryParse(txt(j).Text,d) Then
      MsgBox("Please enter only numbers and fill in all neccessary fields!", MsgBoxStyle.Critical, "WARNING")
      Return False
     End If
     Next J
    
    Return True
    End Function
    Then when you call it in the timer code you could just change the line to this:

    Timer1.Enabled = txtVal()

    because setting Enabled to false is the same as stopping the timer, so if the function returns False, the timer will be stopped.

    I also changed up your check. .TryParse is more efficient than the double check that you were performing. It attempts the conversion and returns true if it works and false if it fails. If it works, then the converted value is put into the second argument. Since you aren't using that, it is discarded, but it still has to be there.

    EDIT: I knew I'd be too slow on that one, but what is interesting is that TG and I both read the same question and came up with three different interpretations (I had two different ones, myself).
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2010
    Location
    Greece
    Posts
    55

    Re: Timer sub issue

    Thanks guys! It works fine.

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
  •  



Featured


Click Here to Expand Forum to Full Width