时间跨度不能正常工作

时间:2014-03-24 11:35:45

标签: vb.net

我正在开发一个下载ETA计算器。所以我使用Timespan代码告诉ETA .. Visual Basic中的时间跨度代码不能正常工作...因为当我输入文件大小时和速度即1GB和1 Mb / s,Label1.Text的时间跨度是5.17:00:00。 这是我的代码

Public Class Form1

Private Property z As Object

Private Sub LinkLabel1_LinkClicked(sender As Object, e As LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
    Process.Start("www.speedtest.net")
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim x, y, z, a, b, c As Single

    x = TextBox1.Text
    y = TextBox2.Text

    If RadioButton1.Checked = True And RadioButton3.Checked = True Then
        Label4.Text = "Minutes"
        z = x * 1024
        c = y / 8
        a = z / c
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If

    If RadioButton1.Checked = True And RadioButton4.Checked = True Then
        Label4.Text = "Minutes"
        z = x * 1024
        c = 1024 / 8 / y
        a = z / c
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If
    If RadioButton2.Checked = True And RadioButton3.Checked = True Then
        Label4.Text = "Hours"
        z = x * 1048576
        c = y / 8
        a = z / c
        b = a / 60
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If
    If RadioButton2.Checked = True And RadioButton4.Checked = True Then
        Label4.Text = "Hours"
        z = x * 1048576
        c = 1024 / 8 * y
        a = z / c
        b = a / 60
        Label1.Text = New TimeSpan(a / 60, 0, 0).ToString()
    End If
End Sub

Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged

End Sub

Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
    MsgBox("Made by SldCvr Enterprises", MsgBoxStyle.OkOnly, "About")

End Sub

结束班

这是设计师

http://i.stack.imgur.com/f3pFm.jpg

1 个答案:

答案 0 :(得分:4)

Timespan工作正常,你的实现是错误的。

第一个错误:a / 60将返回一个整数而不是一个double。 第二个错误:新的TimeSpan(h,m,s,ms)需要整数

你的代码编译的原因是因为如果你将a / 60更改为/ 60d(d为十进制),编译器会对a / 60进行舍入,你会获得正确的除法,但也会出现编译器错误。由于整数限制。

要解决此错误,请将您的除法设为总秒数并执行(新的Timespan())。addSeconds(您的数字);