Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C# - Problem with an event double firing
New Posts  All Forums:Forum Nav:

C# - Problem with an event double firing

post #1 of 6
Thread Starter 
Okay, here's a tricky issue, and I could use a hand:

I'm using a Silverlight DatePicker control

Required Behavior: The user can click on any day in the calendar. BUT whatever day they pick, it will always select the first day of that week instead of the actual date they clicked.

Here's what I've currently got for that, and it works:
Code:
private void dateControl_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
{
    if (!dateControl.SelectedDate.Value.DayOfWeek.Equals(date))
    {
        dateControl.SelectedDate = dateControl.SelectedDate.Value.AddDays(dateControl.FirstDayOfWeek - dateControl.SelectedDate.Value.DayOfWeek);
        date = dateControl.SelectedDate.Value;
    }
}

As you can see, the issue clearly stems from the fact that I am updating the value within its own event listener. But there's no way to avoid that due to the behavioral requirement.
That's why I've got the deferred "date" variable to attempt to solve the problem, but that didn't do any good.

Here's what happens:
If they manually type in a value, then it only fires the SelectedDateChanged event once and all is good.

The problem is when they use the calendar to select a date. It actually fires the event 4 times:
First time happens as soon as they click on the date in the calendar
Second firing happens as this line runs: "dateControl.SelectedDate = ..." //this firing is expected an normal
Third firing happens when the calendar closes! It actually sets the dateControl.SelectedDate back to the original value the user selected //THIS is where the problem is....
Fourth and final firing happens again on this line: "dateControl.SelectedDate = ..."


Even though it's running so many times, the end result is technically correct. So I might normally just ignore the issue. BUT, there are other things (such as database updates) that need to happen immediately when that date changes, and it is mandatory that they happen exactly once. Which means I have no choice, I have to solve this somehow.

If it was only firing twice, I'd be able to deal with it. But, I can't figure out any way to distinguish between the third firing, and the first.
There is a boolean value called "isDropDownOpen", I can check if the calendar is open or closed. But if I put in a condition that checks for that, then the update never runs for a manually typed date. Which is also not okay...

Any ideas?
Edited by wedge - 6/20/13 at 8:21pm
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #2 of 6
Thread Starter 
Okie... this took many hours longer than it should have. But I think I got it.

This seems to satisfy all of my needs
Code:
private void dateControl_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!dateControl.SelectedDate.Value.DayOfWeek.Equals(Date) && dateControl.SelectedDate.Value.DayOfWeek != dateControl.FirstDayOfWeek)
            {
                if (!Date.Equals(dateControl.SelectedDate.Value.AddDays(dateControl.FirstDayOfWeek - dateControl.SelectedDate.Value.DayOfWeek)))
                {
                    Date = dateControl.SelectedDate.Value.AddDays(dateControl.FirstDayOfWeek - dateControl.SelectedDate.Value.DayOfWeek);
                }

                dateControl.SelectedDate = Date;
            }
        }

I just put my db update logic inside the second if statement. That runs just once even though the whole event still fires four times.

I'm still open to suggestions if anyone knows a better resolution. This still seems messy to me. But at least it works.

Edit: This solution does have a side benefit though. This also prevent the code from running twice if the user selects a different date within the currently selected week. So maybe this is the best solution.
Edited by wedge - 6/20/13 at 9:45pm
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
post #3 of 6
sounds to me like you've subscribed to multiple events with the same callback might want to take a look at that area in your code, if not then maybe there's another event that will fire when you want it to?
Edited by lin2dev - 6/21/13 at 1:12am
SolusIgnis
(21 items)
 
2 Watts
(6 items)
 
Huh?!
(13 items)
 
CPUCPUCPUMotherboard
i7-3820 Xeon Phi 31S1P Copressor (WIP) Xeon Phi 31S1P Copressor (WIP) ASRock X79 Extreme6 
GraphicsRAMHard DriveCooling
XFX Reference 6950 Samsung Magic RAM Crucial M4 Cosair H80 
OSMonitorMonitorMonitor
Windows 8.1 Pro HannSpree HF205 1600x900 ASUS VH239H 1920x1080 ASUS VH239H 1920x1080 
KeyboardCaseMouseMouse Pad
CMStorm Quick Fire Pro, Cherry MX Blue Thermaltake Oversoar RX-I MadCatz RAT 9 Saitek Cyborg V.5 
OtherOtherOther
Thrustmaster HOTAS Warthog TrackIR 5 Saitek Pro Combat Pedals 
CPUMotherboardRAMHard Drive
Marvell PXA510 D2Plug DDR3 
Optical DriveCooling
Pssh! Who needs optical drives Passive 
CPUOS
i386 MS-DOS 
  hide details  
Reply
SolusIgnis
(21 items)
 
2 Watts
(6 items)
 
Huh?!
(13 items)
 
CPUCPUCPUMotherboard
i7-3820 Xeon Phi 31S1P Copressor (WIP) Xeon Phi 31S1P Copressor (WIP) ASRock X79 Extreme6 
GraphicsRAMHard DriveCooling
XFX Reference 6950 Samsung Magic RAM Crucial M4 Cosair H80 
OSMonitorMonitorMonitor
Windows 8.1 Pro HannSpree HF205 1600x900 ASUS VH239H 1920x1080 ASUS VH239H 1920x1080 
KeyboardCaseMouseMouse Pad
CMStorm Quick Fire Pro, Cherry MX Blue Thermaltake Oversoar RX-I MadCatz RAT 9 Saitek Cyborg V.5 
OtherOtherOther
Thrustmaster HOTAS Warthog TrackIR 5 Saitek Pro Combat Pedals 
CPUMotherboardRAMHard Drive
Marvell PXA510 D2Plug DDR3 
Optical DriveCooling
Pssh! Who needs optical drives Passive 
CPUOS
i386 MS-DOS 
  hide details  
Reply
post #4 of 6
Usually multiple firing issues can be solved with an if stmt including !Page.PostBack() or just Page.PostBack() depending on the behavior you want.
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
post #5 of 6
Just do it when the DatePicker's lost focus:
Code:
private void dateControl_LostFocus(object sender, RoutedEventArgs e)
{
    dateControl.SelectedDate = dateControl.SelectedDate.Value.AddDays(dateControl.FirstDayOfWeek - dateControl.SelectedDate.Value.DayOfWeek);
}
(of course, add this to the dateControl's LostFocus event).
post #6 of 6
Thread Starter 
Quote:
Originally Posted by 3930K View Post

Just do it when the DatePicker's lost focus:
Code:
private void dateControl_LostFocus(object sender, RoutedEventArgs e)
{
    dateControl.SelectedDate = dateControl.SelectedDate.Value.AddDays(dateControl.FirstDayOfWeek - dateControl.SelectedDate.Value.DayOfWeek);
}
(of course, add this to the dateControl's LostFocus event).

I thought of that too, it would totally workaround the issue. But it would introduce its own issues, and wouldn't provide the best user experience.
In any case, the solution I came up with is working very nicely. No problems that I can see with it.
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
Death Star
(21 items)
 
Darksaber
(11 items)
 
 
CPUMotherboardGraphicsRAM
Athlon II x2 245 Asus M3A78 Radeon HD6570 1GB Mushkin Silverline 2GB DDR2  
Hard DriveOptical DriveOSMonitor
OCZ Vertex 2 120GB Samsung Blu-Ray Windows 7 Samsung 46" DLP 
PowerCaseOther
Silverstone Strider Essentials 400W Silverstone Milo ML03B Hauppage WinTV 1250 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C# - Problem with an event double firing