Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. DateTimePicker ValueChanged event fires twice

DateTimePicker ValueChanged event fires twice

Scheduled Pinned Locked Moved C#
question
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    PaulaM
    wrote on last edited by
    #1

    Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; } The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paula

    N M L 3 Replies Last reply
    0
    • P PaulaM

      Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; } The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paula

      N Offline
      N Offline
      Nader Elshehabi
      wrote on last edited by
      #2

      Hello I can think of another way to do the job. Why don't you change the MaxDate property? something like: DateTimePicker1.MaxDate = DateTime.Now; It'll work even without messages -sometimes they are annoying to beginner users- If you insist on showing a MessageBox, just ignore the first time:), maybe: bool SecondTime = false; void MyEventHandler(params) { TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0 && SecondTime) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; SecondTime = false; return; } SecondTime = true; }

      1 Reply Last reply
      0
      • P PaulaM

        Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; } The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paula

        M Offline
        M Offline
        MCSD Gandalf
        wrote on last edited by
        #3

        Paula, The reason it is firing twice is because you are changing it twice; once when you select on the form and another with your line of code just before your return where you change it back to the current date. HTH

        1 Reply Last reply
        0
        • P PaulaM

          Hi,i have a DateTimePicker DateTimePicker1 in my Form,when user enters a future date a message "Date cannot be in the future" displays in my DateTimePicker ValueChanged event i have this code TimeSpan ts = (DateTime)DateTimePicker1.Value - DateTime.Now; if (ts.Milliseconds > 0) { MessageBox.Show"Date cannot be in the future".); DateTimePicker1.Value = DateTime.Now; return; } The message Displays twice,which makes me think the event fires twice. how can i make it fire just once? or is there something i am mssing? thanks:) regards paula

          L Offline
          L Offline
          LongRange Shooter
          wrote on last edited by
          #4

          The one time I experienced something like that, I found that I had done a subscription to the event twice for that event handler. So do a search in your code for += and make sure you haven't done that.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups