How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox?
How do I resolve this paradox? How do I resolve this paradox?
How do I resolve this paradox?
Go Back  Xtreme Visual Basic Talk > > > How do I resolve this paradox?


Reply
 
Thread Tools Display Modes
  #1  
Old 11-22-2010, 01:19 PM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Smile How do I resolve this paradox?


(sorry for the title, it's really hard to give it one as you might see)
I have a load sub with a few declarations in it as so:
Code:
Private Sub CustomizeGraph_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TimeTo_dtp.CustomFormat = "HH:mm"
        TimeFrom_dtp.CustomFormat = "HH:mm"
        TimeTo_dtp.MaxDate = DateTime.Now
        TimeFrom_dtp.MaxDate = DateTime.Now.AddMinutes(-TimeInterval)
        TimeTo_dtp.Value = DateTime.Now
        TimeFrom_dtp.Value = DateTime.Now.AddMinutes(-TimeInterval)
        ReadingFrequency(0) = 1
        ReadingFrequency(1) = 5
        ReadingFrequency(2) = 10
        ReadingFrequency(3) = 15
        ReadingFrequency(4) = 60
        ReadingFrequency(5) = 1440
        ReadingFrequency_cmb.SelectedIndex = 3
    End Sub
When I change the TimeFrom_dtp.Value and TimeTo_dtp.Value it automatically raises the TimeFrom_dtp.ValueChanged and TimeTo_dtp.ValueChanged subs. Here is the code for those:
Code:
Private Sub TimeFrom_dtp_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimeFrom_dtp.ValueChanged
        TimeFrom_dtp.MaxDate = TimeTo_dtp.Value.AddMinutes(-TimeInterval)
    End Sub

    Private Sub TimeTo_dtp_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TimeTo_dtp.ValueChanged
        TimeTo_dtp.MinDate = TimeFrom_dtp.Value.AddMinutes(TimeInterval)
        TimeTo_dtp.MaxDate = DateTime.Now
    End Sub
But when the TimeFrom_dtp.ValueChanged code is executed, the TimeFrom_dtp.Value isn't correct as it hasn't been set yet. If I change it round so the TimeTo_dtp.Value is set first then that doesn't work because the TimeFrom_dtp hasn't been set yet?
How can I get it so that the ValueChanged code isn't triggered until I've set all the values of my datetimepickers?
Reply With Quote
  #2  
Old 11-22-2010, 01:28 PM
AtmaWeapon's Avatar
AtmaWeaponHow do I resolve this paradox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It's a common problem with a pretty simple solution.

You can't turn off the events, but you *can* make your code do nothing when they're raised. Here's a pattern that works:
Code:
Class Whatever
    Private _isLoading As Boolean

    Sub Whatever_Load(...)
        _isLoading = True
        ' do whatever
        _isLoading = Flase
    End Sub

    Sub SomeControl_ValueChanged(...)
        If _isLoading Then
            Return
        End If
    End Sub
End Class
There's other approaches, but that's the general idea.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 11-22-2010, 01:50 PM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

That seems like a good simple way. I just think it's a shame the programmers of the visual basic 2010 programme didn't make it so nothing runs until everything in the load event has been completed. or at least give the user the choice. Thanks.
Reply With Quote
  #4  
Old 11-22-2010, 01:56 PM
AtmaWeapon's Avatar
AtmaWeaponHow do I resolve this paradox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I usually make this post kind of long, but I think I'll try a shorter version.

The events aren't raised by the language; they're raised by code. This is more or less what the code for DateTimePicker.Value looks like:
Code:
Public Property Value As DateTime
    Get
        Return _value
    End Get
    Set(ByVal value As DateTime)
        If value <> _value Then
            _value = value
            RaiseEvent ValueChanged(Me, EventArgs.Empty)
        End If
    End Set
End Property
Now for the part that would take a long time but I'll sidestep by asking you to do some homework: design a mechanism that allows you to suspend raising this event temporarily with no input from the user to indicate when the update should be suspended. You should find this difficult, but the reasons why will be enlightening.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #5  
Old 11-22-2010, 02:07 PM
Michael___ Michael___ is offline
Centurion
 
Join Date: Sep 2009
Posts: 111
Default

What I was thinking was: everything in the load event happens then, after everything has happen (like variables being set, datetimepicker time being set ect...) then any thing (like the TimeTo_dtp.ValueChanged sub for example) is raised, but only after the whole load sub is finished.
Reply With Quote
  #6  
Old 11-22-2010, 02:51 PM
AtmaWeapon's Avatar
AtmaWeaponHow do I resolve this paradox? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

It was a trap; "find the things you didn't think about" is a game programmers *love* to play

What you are proposing is that event raises are deferred until the Load event has happened. How do you believe this should be implemented? (This is going to get long.)

I've written at length about events in the past, but the short story is when an event is raised what happens is every registered event handler is called serially. So for your implementation to exist, there'd have to be some special code in RaiseEvent that would check if the object raising the event has finished loading yet. If not, the event should be deferred. This has many problems:
  • Anything can raise an event, so any contract you define has to be implemented by every object that has an event, not just controls.
    • You might answer this with "everything with an event should have whatever mechanism is specified." This cannot be enforced without the help of the compiler. Suppose you get the compiler to specify it. Now the 99% of classes that don't have this problem need to implement some interface/method they don't need. And the 1% that *do* need it have to implement something that looks like what's been suggested. It's a loss for everyone.
    • You might answer this with "the compiler can look for the mechanism and use them if they are present". This is a bad solution because such mechanisms rely on reflection and are quite slow. Again, the 99% classes that don't need this functionality have to pay the price of the 1% that do.
  • What happens to events that have been raised but deferred when an exception happens? Do you raise them even though Load failed and the form is certainly in an invalid state?
  • What if there are some events you want to happen while the Load event is running? If that's not supported, what's the workaround?
There's probably more concerns, but that's enough to chew on for now
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
Reply

Tags
changed, confusing, datetimepicker, michael jackson, paradox, vague tags, valuechanged


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox? How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox? How do I resolve this paradox?
How do I resolve this paradox?
How do I resolve this paradox?
 
How do I resolve this paradox?
How do I resolve this paradox?
 
-->