Results 1 to 7 of 7

Thread: [2005] Getting rid of a repeat?

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    32

    [2005] Getting rid of a repeat?

    Hi, I have written some coding that will not allow the dtp to select a date before today. It changes the date to todays date and shows a msgbox say "This date has passed, please select another".

    I need to insert it into the valuechanged sub, so that it checks everytime the dtp changes.

    What happens is --> that the msgbox appear twice. I have thought about it and realise that as it the script realises that the date is before todays date , it changes the value to todays date it and as it changes it runs the script again to check if the date is before todays and finds the previous date and the msgbox appears again.

    VB Code:
    1. Private Sub Ffrom_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Ffrom.ValueChanged
    2.        If Ffrom.Value < Now() Then
    3.             Ffrom.Value = Now()
    4.             MsgBox("This date has passed, please select another")
    5.         End If
    6.  
    7.     End Sub

    I may be wrong, I have only been started learning vb a few weeks ago.

    Thanks,

    Mutasim
    dim Mutasim as vbnewbie = stupid

    hehe!



  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Getting rid of a repeat?

    Use a flag that will allow/disallow the event to run Here is an example
    VB Code:
    1. Private m_AllowEvent As Boolean = True
    2.  
    3. Private Sub Dtp_ValueChanged(sender as object, e as eventargs) Handles dtp.ValueChanged
    4.    If m_AllowEvent = True AndAlso Dtp.Value < Now Then 'first checks if the flag is true if it is then compare dates
    5.        m_AllowEvent = False 'set the flag to false.  This way the event won't be raised again when you fix the date
    6.        'Do your stuff
    7.    End If
    8.    m_AllowEvent = True 'set the flag back to true so the event can be raised again
    9. End Sub
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2005] Getting rid of a repeat?

    your diagnose of the problem is spot on... the problem arises because NOW() includes the time, down to the milisecond.... what you should be comparing is the date portion. Because even if you set it to a date, it won't have the time, so it assumes midnight, which is less than Now() (which is the current date time) and you get the message.

    try this:
    VB Code:
    1. If Ffrom.Value < System.Data.Date.Today Then

    I think that's the right namespace path.....

    -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??? *

  4. #4

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    32

    Re: [2005] Getting rid of a repeat?

    Quote Originally Posted by techgnome
    your diagnose of the problem is spot on... the problem arises because NOW() includes the time, down to the milisecond.... what you should be comparing is the date portion. Because even if you set it to a date, it won't have the time, so it assumes midnight, which is less than Now() (which is the current date time) and you get the message.

    try this:
    VB Code:
    1. If Ffrom.Value < System.Data.Date.Today Then

    I think that's the right namespace path.....

    -tg
    It says 'Date' is not a member of 'Data'. and it doesn't solve it. Thanks for trying though. After reading your post i tried,
    VB Code:
    1. now.date()
    and this still doesn't solve it.

    Thanks,

    Mutasim
    dim Mutasim as vbnewbie = stupid

    hehe!



  5. #5

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    32

    Re: [2005] Getting rid of a repeat?

    Quote Originally Posted by mpdeglau
    Use a flag that will allow/disallow the event to run Here is an example
    VB Code:
    1. Private m_AllowEvent As Boolean = True
    2.  
    3. Private Sub Dtp_ValueChanged(sender as object, e as eventargs) Handles dtp.ValueChanged
    4.    If m_AllowEvent = True AndAlso Dtp.Value < Now Then 'first checks if the flag is true if it is then compare dates
    5.        m_AllowEvent = False 'set the flag to false.  This way the event won't be raised again when you fix the date
    6.        'Do your stuff
    7.    End If
    8.    m_AllowEvent = True 'set the flag back to true so the event can be raised again
    9. End Sub
    This seems to work, but the only problem is that an error occur and I think if solved it will work,

    Quote Originally Posted by VB ERROR
    Private' is not valid on a local variable declaration.
    I have now emmbedded the code itno a differnet sub, if you could please have a look and see if you notice anything wrong:

    VB Code:
    1. Sub check()
    2.         Private m_AllowEvent As Boolean = True
    3.         Dim days As Integer = Tto.Value.Day - Ffrom.Value.Day + 1
    4.         If m_AllowEvent = True AndAlso Ffrom.Value < Now.Date() Or _
    5.                   Tto.Value < Now.Date() Then
    6.             m_AllowEvent = False
    7.             Ffrom.Value = Now.Date()
    8.             Tto.Value = Now.Date()
    9.             MsgBox("This date has passed, please select another")
    10.             m_AllowEvent = True
    11. else if ....
    dim Mutasim as vbnewbie = stupid

    hehe!



  6. #6
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,521

    Re: [2005] Getting rid of a repeat?

    m_AllowEvent has to be declared at the class level, like I showed.
    Visual Studio Team Edition 2005
    GDI+ Links: Bob Powell VB.Net Heaven
    API Links: All API Pinvoke.Net
    VB6 to VB.Net: Visual Basic 6 to .NET Function Equivalents (Thread)

  7. #7

    Thread Starter
    Member
    Join Date
    Jul 2006
    Posts
    32

    Re: [2005] Getting rid of a repeat?

    Quote Originally Posted by mpdeglau
    m_AllowEvent has to be declared at the class level, like I showed.
    Hi, I added to to class level instead of in the sub and it hasn't fixed the problem.

    Will anyone have a look at the problem for me? PM me and I will send you the project

    Thanks,

    Mutasim
    dim Mutasim as vbnewbie = stupid

    hehe!



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