Coding question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?U2hlbGRvbg==?=

    Coding question

    Hello -

    I found the following code on the Internet which is perfect for my needs
    with datetimepicker controls:

    Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles dtpOther.ValueC hanged
    Dim newTime As DateTime = dtpOther.Value
    Dim hr As Integer = newTime.Hour
    Dim min As Integer = newTime.Minute
    Dim sec As Integer = newTime.Second

    If (oldTime.Hour = 23 AndAlso newTime.Hour = 0) OrElse (oldTime.Hour
    = 0 AndAlso newTime.Hour = 23) Then
    hr = oldTime.Hour
    End If
    If (oldTime.Minute = 59 AndAlso newTime.Minute = 0) OrElse
    (oldTime.Minute = 0 AndAlso newTime.Minute = 59) Then
    min = oldTime.Minute
    End If
    If (oldTime.Second = 59 AndAlso newTime.Second = 0) OrElse
    (oldTime.Second = 0 AndAlso newTime.Second = 59) Then
    sec = oldTime.Second
    End If
    If hr <newTime.Hour OrElse min <newTime.Minu te OrElse sec <>
    newTime.Second Then
    dtpOther.Value = DateTime.Now.Da te.Add(New TimeSpan(hr, min, sec))
    End If
    oldTime = dtpOther.Value
    End Sub

    I have nine timepicker controls which all need to do the same thing as the
    above. How can I consolidate it in code instead of doing one each of the Sub
    for each of the controls?

    I know it's an elementary question for a lot of you, but I'm just learning
    this stuff.

    Any help will be appreciated!
    --
    Sheldon
  • Lloyd Sheen

    #2
    Re: Coding question


    "Sheldon" <Sheldon@discus sions.microsoft .comwrote in message
    news:7D5C42B0-5B72-4232-B10F-4DE548DB2E56@mi crosoft.com...
    Hello -
    >
    I found the following code on the Internet which is perfect for my needs
    with datetimepicker controls:
    >
    Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles dtpOther.ValueC hanged
    Dim newTime As DateTime = dtpOther.Value
    Dim hr As Integer = newTime.Hour
    Dim min As Integer = newTime.Minute
    Dim sec As Integer = newTime.Second
    >
    If (oldTime.Hour = 23 AndAlso newTime.Hour = 0) OrElse
    (oldTime.Hour
    = 0 AndAlso newTime.Hour = 23) Then
    hr = oldTime.Hour
    End If
    If (oldTime.Minute = 59 AndAlso newTime.Minute = 0) OrElse
    (oldTime.Minute = 0 AndAlso newTime.Minute = 59) Then
    min = oldTime.Minute
    End If
    If (oldTime.Second = 59 AndAlso newTime.Second = 0) OrElse
    (oldTime.Second = 0 AndAlso newTime.Second = 59) Then
    sec = oldTime.Second
    End If
    If hr <newTime.Hour OrElse min <newTime.Minu te OrElse sec <>
    newTime.Second Then
    dtpOther.Value = DateTime.Now.Da te.Add(New TimeSpan(hr, min,
    sec))
    End If
    oldTime = dtpOther.Value
    End Sub
    >
    I have nine timepicker controls which all need to do the same thing as the
    above. How can I consolidate it in code instead of doing one each of the
    Sub
    for each of the controls?
    >
    I know it's an elementary question for a lot of you, but I'm just learning
    this stuff.
    >
    Any help will be appreciated!
    --
    Sheldon
    There are two different ways to do it.

    First is using AddHandler. This could be put in the load of the form as
    follows:

    AddHandler ControlName.Val ueChanged , AddressOf dtpOther_ValueC hanged

    The second is to add each Control.ValueCh anged in the Handles clause as
    follows:

    Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
    System.EventArg s) Handles dtpOther.ValueC hanged , OtherControl.Va lueChanged
    , AnotherControl. ValueChanged ....etc

    Hope this helps
    LS

    Comment

    • Just_a_fan@home.net

      #3
      Re: Re: Coding question

      Well, you got half the answer here. The rest of the answer is that you
      have to determine which control was hit to cause the routine to run.

      It gets messy! Losing control arrays is a real pain!

      Mike

      Sub AlarmChanged(By Val sender As System.Object, ByVal e As
      System.EventArg s) _
      Handles Alarm1.ValueCha nged, Alarm2.ValueCha nged, Alarm3.ValueCha nged,
      Alarm4.ValueCha nged, Alarm5.ValueCha nged, _
      Alarm6.ValueCha nged, Alarm7.ValueCha nged, Alarm8.ValueCha nged,
      Alarm9.ValueCha nged, Alarm10.ValueCh anged, _
      Alarm11.ValueCh anged, Alarm12.ValueCh anged, Alarm13.ValueCh anged,
      Alarm14.ValueCh anged, Alarm15.ValueCh anged, _
      Alarm16.ValueCh anged, Alarm17.ValueCh anged, Alarm18.ValueCh anged,
      Alarm19.ValueCh anged, Alarm20.ValueCh anged, _
      Alarm21.ValueCh anged, Alarm22.ValueCh anged, Alarm23.ValueCh anged,
      Alarm24.ValueCh anged, Alarm25.ValueCh anged, _
      Alarm26.ValueCh anged, Alarm27.ValueCh anged, Alarm28.ValueCh anged,
      Alarm29.ValueCh anged, Alarm30.ValueCh anged, _
      Alarm31.ValueCh anged, Alarm32.ValueCh anged, Alarm33.ValueCh anged,
      Alarm34.ValueCh anged, Alarm35.ValueCh anged, _
      Alarm36.ValueCh anged, Alarm37.ValueCh anged, Alarm38.ValueCh anged,
      Alarm39.ValueCh anged, Alarm30.ValueCh anged, _
      Alarm41.ValueCh anged, Alarm42.ValueCh anged, _
      Alarm1.Click, Alarm2.Click, Alarm3.Click, Alarm4.Click, Alarm5.Click,
      _
      Alarm6.Click, Alarm7.Click, Alarm8.Click, Alarm9.Click, Alarm10.Click,
      _
      Alarm11.Click, Alarm12.Click, Alarm13.Click, Alarm14.Click,
      Alarm15.Click, _
      Alarm16.Click, Alarm17.Click, Alarm18.Click, Alarm19.Click,
      Alarm20.Click, _
      Alarm21.Click, Alarm22.Click, Alarm23.Click, Alarm24.Click,
      Alarm25.Click, _
      Alarm26.Click, Alarm27.Click, Alarm28.Click, Alarm29.Click,
      Alarm30.Click, _
      Alarm31.Click, Alarm32.Click, Alarm33.Click, Alarm34.Click,
      Alarm35.Click, _
      Alarm36.Click, Alarm37.Click, Alarm38.Click, Alarm39.Click,
      Alarm30.Click, _
      Alarm41.Click, Alarm42.Click

      If sender.Equals(A larm1) Then : CheckAlarmVsWar n(Warn1, Alarm1) : Exit
      Sub
      ElseIf sender.Equals(A larm2) Then : CheckAlarmVsWar n(Warn2, Alarm2)
      : Exit Sub
      ElseIf sender.Equals(A larm3) Then : CheckAlarmVsWar n(Warn3, Alarm3)
      : Exit Sub
      ElseIf sender.Equals(A larm4) Then : CheckAlarmVsWar n(Warn4, Alarm4)
      : Exit Sub
      ElseIf sender.Equals(A larm5) Then : CheckAlarmVsWar n(Warn5, Alarm5)
      : Exit Sub
      ElseIf sender.Equals(A larm6) Then : CheckAlarmVsWar n(Warn6, Alarm6)
      : Exit Sub
      ElseIf sender.Equals(A larm7) Then : CheckAlarmVsWar n(Warn7, Alarm7)
      : Exit Sub
      ElseIf sender.Equals(A larm8) Then : CheckAlarmVsWar n(Warn8, Alarm8)
      : Exit Sub
      ElseIf sender.Equals(A larm9) Then : CheckAlarmVsWar n(Warn9, Alarm9)
      : Exit Sub
      ElseIf sender.Equals(A larm10) Then : CheckAlarmVsWar n(Warn10,
      Alarm10) : Exit Sub
      ElseIf sender.Equals(A larm11) Then : CheckAlarmVsWar n(Warn11,
      Alarm11) : Exit Sub


      .....repeat until sick of the whole thing! This is NUTZ!!!!!


      On Sat, 19 Jul 2008 18:18:04 -0400, in
      microsoft.publi c.dotnet.langua ges.vb "Lloyd Sheen" <a@b.cwrote:
      >
      >"Sheldon" <Sheldon@discus sions.microsoft .comwrote in message
      >news:7D5C42B 0-5B72-4232-B10F-4DE548DB2E56@mi crosoft.com...
      >Hello -
      >>
      >I found the following code on the Internet which is perfect for my needs
      >with datetimepicker controls:
      >>
      >Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
      >System.EventAr gs) Handles dtpOther.ValueC hanged
      > Dim newTime As DateTime = dtpOther.Value
      > Dim hr As Integer = newTime.Hour
      > Dim min As Integer = newTime.Minute
      > Dim sec As Integer = newTime.Second
      >>
      > If (oldTime.Hour = 23 AndAlso newTime.Hour = 0) OrElse
      >(oldTime.Hou r
      >= 0 AndAlso newTime.Hour = 23) Then
      > hr = oldTime.Hour
      > End If
      > If (oldTime.Minute = 59 AndAlso newTime.Minute = 0) OrElse
      >(oldTime.Minut e = 0 AndAlso newTime.Minute = 59) Then
      > min = oldTime.Minute
      > End If
      > If (oldTime.Second = 59 AndAlso newTime.Second = 0) OrElse
      >(oldTime.Secon d = 0 AndAlso newTime.Second = 59) Then
      > sec = oldTime.Second
      > End If
      > If hr <newTime.Hour OrElse min <newTime.Minu te OrElse sec <>
      >newTime.Seco nd Then
      > dtpOther.Value = DateTime.Now.Da te.Add(New TimeSpan(hr, min,
      >sec))
      > End If
      > oldTime = dtpOther.Value
      > End Sub
      >>
      >I have nine timepicker controls which all need to do the same thing as the
      >above. How can I consolidate it in code instead of doing one each of the
      >Sub
      >for each of the controls?
      >>
      >I know it's an elementary question for a lot of you, but I'm just learning
      >this stuff.
      >>
      >Any help will be appreciated!
      >--
      >Sheldon
      >
      >There are two different ways to do it.
      >
      >First is using AddHandler. This could be put in the load of the form as
      >follows:
      >
      >AddHandler ControlName.Val ueChanged , AddressOf dtpOther_ValueC hanged
      >
      >The second is to add each Control.ValueCh anged in the Handles clause as
      >follows:
      >
      >Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
      System.EventArg s) Handles dtpOther.ValueC hanged , OtherControl.Va lueChanged
      >, AnotherControl. ValueChanged ....etc
      >
      >Hope this helps
      >LS

      Comment

      • Jack Jackson

        #4
        Re: Coding question

        On Sat, 19 Jul 2008 18:18:04 -0400, "Lloyd Sheen" <a@b.cwrote:
        >
        >"Sheldon" <Sheldon@discus sions.microsoft .comwrote in message
        >news:7D5C42B 0-5B72-4232-B10F-4DE548DB2E56@mi crosoft.com...
        >Hello -
        >>
        >I found the following code on the Internet which is perfect for my needs
        >with datetimepicker controls:
        >>
        >Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
        >System.EventAr gs) Handles dtpOther.ValueC hanged
        > Dim newTime As DateTime = dtpOther.Value
        > Dim hr As Integer = newTime.Hour
        > Dim min As Integer = newTime.Minute
        > Dim sec As Integer = newTime.Second
        >>
        > If (oldTime.Hour = 23 AndAlso newTime.Hour = 0) OrElse
        >(oldTime.Hou r
        >= 0 AndAlso newTime.Hour = 23) Then
        > hr = oldTime.Hour
        > End If
        > If (oldTime.Minute = 59 AndAlso newTime.Minute = 0) OrElse
        >(oldTime.Minut e = 0 AndAlso newTime.Minute = 59) Then
        > min = oldTime.Minute
        > End If
        > If (oldTime.Second = 59 AndAlso newTime.Second = 0) OrElse
        >(oldTime.Secon d = 0 AndAlso newTime.Second = 59) Then
        > sec = oldTime.Second
        > End If
        > If hr <newTime.Hour OrElse min <newTime.Minu te OrElse sec <>
        >newTime.Seco nd Then
        > dtpOther.Value = DateTime.Now.Da te.Add(New TimeSpan(hr, min,
        >sec))
        > End If
        > oldTime = dtpOther.Value
        > End Sub
        >>
        >I have nine timepicker controls which all need to do the same thing as the
        >above. How can I consolidate it in code instead of doing one each of the
        >Sub
        >for each of the controls?
        >>
        >I know it's an elementary question for a lot of you, but I'm just learning
        >this stuff.
        >>
        >Any help will be appreciated!
        >--
        >Sheldon
        >
        >There are two different ways to do it.
        >
        >First is using AddHandler. This could be put in the load of the form as
        >follows:
        >
        >AddHandler ControlName.Val ueChanged , AddressOf dtpOther_ValueC hanged
        >
        >The second is to add each Control.ValueCh anged in the Handles clause as
        >follows:
        >
        >Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
        System.EventArg s) Handles dtpOther.ValueC hanged , OtherControl.Va lueChanged
        >, AnotherControl. ValueChanged ....etc
        >
        >Hope this helps
        >LS
        And don't forget to change the method to use the sender parameter as
        the object to modify.

        Add:

        Dim dtp As DateTimePicker = DirectCast(send er, DateTimePicker)

        and change all references to dtpOther to dtp.

        Comment

        • Steve Gerrard

          #5
          Re: Re: Coding question

          Just_a_fan@home .net wrote:
          Well, you got half the answer here. The rest of the answer is that
          you have to determine which control was hit to cause the routine to
          run.
          sender *is* the control that was hit.

          So
          Dim TheAlarm As Alarm = DirectCast(send er, Alarm)

          CheckAlarm(TheA larm)...


          Comment

          • Steve Gerrard

            #6
            Re: Coding question

            Sheldon wrote:
            >
            I have nine timepicker controls which all need to do the same thing
            as the above. How can I consolidate it in code instead of doing one
            each of the Sub for each of the controls?
            >
            In addition to using a single handler for all instances, as noted in other
            posts, if you wanted to get fancy, you could create a new control class which
            inherits timepicker, and adds the code above. Then use that in all your forms.
            It is not difficult to do.


            Comment

            • Cor Ligthert[MVP]

              #7
              Re: Coding question

              Sheldon-

              Almost the same answer as steve Gerard, however then with the extention::
              "Use an usercontrol."



              -Cor

              "Sheldon" <Sheldon@discus sions.microsoft .comschreef in bericht
              news:7D5C42B0-5B72-4232-B10F-4DE548DB2E56@mi crosoft.com...
              Hello -
              >
              I found the following code on the Internet which is perfect for my needs
              with datetimepicker controls:
              >
              Private Sub dtpOther_ValueC hanged(ByVal sender As Object, ByVal e As
              System.EventArg s) Handles dtpOther.ValueC hanged
              Dim newTime As DateTime = dtpOther.Value
              Dim hr As Integer = newTime.Hour
              Dim min As Integer = newTime.Minute
              Dim sec As Integer = newTime.Second
              >
              If (oldTime.Hour = 23 AndAlso newTime.Hour = 0) OrElse
              (oldTime.Hour
              = 0 AndAlso newTime.Hour = 23) Then
              hr = oldTime.Hour
              End If
              If (oldTime.Minute = 59 AndAlso newTime.Minute = 0) OrElse
              (oldTime.Minute = 0 AndAlso newTime.Minute = 59) Then
              min = oldTime.Minute
              End If
              If (oldTime.Second = 59 AndAlso newTime.Second = 0) OrElse
              (oldTime.Second = 0 AndAlso newTime.Second = 59) Then
              sec = oldTime.Second
              End If
              If hr <newTime.Hour OrElse min <newTime.Minu te OrElse sec <>
              newTime.Second Then
              dtpOther.Value = DateTime.Now.Da te.Add(New TimeSpan(hr, min,
              sec))
              End If
              oldTime = dtpOther.Value
              End Sub
              >
              I have nine timepicker controls which all need to do the same thing as the
              above. How can I consolidate it in code instead of doing one each of the
              Sub
              for each of the controls?
              >
              I know it's an elementary question for a lot of you, but I'm just learning
              this stuff.
              >
              Any help will be appreciated!
              --
              Sheldon

              Comment

              Working...