Android Question help! Convert VB.net to b4a

rkxo

Active Member
Licensed User
Longtime User
Hi,

Can someone help me translate this code to b4a .?

VB.net:
 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       Dim numcount As Integer
    Dim Starttime As DateTime
        Try

            numcount = numcount + 1
                If RadioButton1.Checked = True Then
                    Starttime = New DateTime(Now.Year, Now.Month, Now.Day, 0, 0, 0)
                Else
                Starttime = New DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, dateTimePicker2.Value.Day, dateTimePicker2.Value.Hour, dateTimePicker2.Value.Minute, dateTimePicker2.Value.Second)
            End If
                Starttime = Starttime.AddSeconds(NumericUpDown2.Value)
                Dim ref As New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)
                ref = ref.AddSeconds(NumericUpDown3.Value)
                Dim final As TimeSpan = ref - Starttime

                Dim Resto As Double = final.TotalSeconds Mod numericUpDown1.Value
                label1.Text = Resto
                Label2.Text = ref.Second

        Catch ex As Exception

        End Try




    End Sub

i tried but nope it just worked


B4X:
Private Sub Timer1_Tick
    Try

        numcount = numcount + 1

    Starttime =  DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), 0, 0, 0)

   
   
        Dim ref As Long =DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), DateTime.GetHour(DateTime.Now), DateTime.GetMinute(DateTime.Now), DateTime.GetSecond(DateTime.Now))
        ref = ref + MaskedEditText2.text.Replace("_","")*1000
        Dim final As Long = ref -  Starttime

        Dim Resto As Long = final Mod (MaskedEditText3.text.Replace("_","")*1000)
        Label7.Text = Resto
        Label2.Text =DateTime.GetSecond(ref)

    Catch  As Exception

    End Try

End Sub

thanks for advance
 
Last edited:

rkxo

Active Member
Licensed User
Longtime User
Hi,

Can someone help me translate this code to b4a .?

VB.net:
 Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
       Dim numcount As Integer
    Dim Starttime As DateTime
        Try

            numcount = numcount + 1
                If RadioButton1.Checked = True Then
                    Starttime = New DateTime(Now.Year, Now.Month, Now.Day, 0, 0, 0)
                Else
                Starttime = New DateTime(dateTimePicker2.Value.Year, dateTimePicker2.Value.Month, dateTimePicker2.Value.Day, dateTimePicker2.Value.Hour, dateTimePicker2.Value.Minute, dateTimePicker2.Value.Second)
            End If
                Starttime = Starttime.AddSeconds(NumericUpDown2.Value)
                Dim ref As New DateTime(Now.Year, Now.Month, Now.Day, Now.Hour, Now.Minute, Now.Second)
                ref = ref.AddSeconds(NumericUpDown3.Value)
                Dim final As TimeSpan = ref - Starttime

                Dim Resto As Double = final.TotalSeconds Mod numericUpDown1.Value
                label1.Text = Resto
                Label2.Text = ref.Second

        Catch ex As Exception

        End Try




    End Sub

i tried but nope it just worked


B4X:
Private Sub Timer1_Tick
    Try

        numcount = numcount + 1

    Starttime =  DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), 0, 0, 0)

  
  
        Dim ref As Long =DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), DateTime.GetHour(DateTime.Now), DateTime.GetMinute(DateTime.Now), DateTime.GetSecond(DateTime.Now))
        ref = ref + MaskedEditText2.text.Replace("_","")*1000
        Dim final As Long = ref -  Starttime

        Dim Resto As Long = final Mod (MaskedEditText3.text.Replace("_","")*1000)
        Label7.Text = Resto
        Label2.Text =DateTime.GetSecond(ref)

    Catch  As Exception

    End Try

End Sub

thanks for advance
now work ok
B4X:
Private Sub Timer1_Tick
    Try

        numcount = numcount + 1

    Starttime =  DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), 0, 0, 0)

    
        Starttime=Starttime+MaskedEditText2.text.Replace("_","")*1000
        Dim ref As Long =DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), DateTime.GetHour(DateTime.Now), DateTime.GetMinute(DateTime.Now), DateTime.GetSecond(DateTime.Now))
        ref = ref + UpDown1.value*1000
        Dim final As Long = ref -  Starttime

        Dim Resto As Long = final Mod (MaskedEditText3.text.Replace("_","")*1000)
        Dim ty As Int=Resto/1000
        Label7.Text = ty
        Label2.Text =DateTime.GetSecond(ref)

    Catch  As Exception

    End Try

End Sub
 
Upvote 0

emexes

Expert
Licensed User
B4X:
Starttime =  DateUtils.SetDateAndTime(DateTime.GetYear(DateTime.Now), DateTime.GetMonth(DateTime.Now), DateTime.GetDayOfMonth(DateTime.Now), 0, 0, 0)

A random observation is that if this code was executed across midnight across months, then it is possible that the three separate DateTime.Now calls might not all return the same date, and that Starttime might end up being the last day of the next month or the first day of the previous month.

Eg, say that the date is rolling from 31-Jul to 01-Aug, then you could conceivably end up with 31-Aug (wrong by 30 days) or 01-Jul (also wrong by 30 days, and in the wrong direction too, ie time going backwards). It gets even worse across the December-to-January midnight.

I agree that the probability of this happening is small. :rolleyes: But it is also non-zero. šŸ¤” The fix is simple:

B4X:
Dim TempNow As Long = DateTime.Now    'latch the current date and time so that it doesn't change in the middle of the next calculation
StartTime =  DateUtils.SetDateAndTime(DateTime.GetYear(TempNow), DateTime.GetMonth(TempNow), DateTime.GetDayOfMonth(TempNow), 0, 0, 0)
 
Last edited:
Upvote 0

rkxo

Active Member
Licensed User
Longtime User
A random observation is that if this code was executed across midnight across months, then it is possible that the three separate DateTime.Now calls might not all return the same date, and that Starttime might end up being the last day of the next month or the first day of the previous month.

Eg, say that the date is rolling from 31-Jul to 01-Aug, then you could conceivably end up with 31-Aug (wrong by 30 days) or 01-Jul (also wrong by 30 days, and in the wrong direction too, ie time going backwards). It gets even worse across the December-to-January midnight.

I agree that the probability of this happening is small. :rolleyes: But it is also non-zero. šŸ¤” The fix is simple:

B4X:
Dim TempNow As Long = DateTime.Now    'latch the current date and time so that it doesn't change in the middle of the next calculation
StartTime =  DateUtils.SetDateAndTime(DateTime.GetYear(TempNow), DateTime.GetMonth(TempNow), DateTime.GetDayOfMonth(TempNow), 0, 0, 0)
ok , thnaks
 
Upvote 0
Top