我的下载/上传速度计算器有问题。 我把数字写到“inputBox”,我认为它无法解析它。 这是源代码:
!UPDATED!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net.NetworkInformation;
namespace Tomco_DownloadTime {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
timeLabel.Text = DateTime.Now.ToString("HH:mm tt");
}
IPv4InterfaceStatistics stat = NetworkInterface.GetAllNetworkInterfaces() [0].GetIPv4Statistics();
private double getKBDownloadSpeed() {
return (stat.BytesReceived)/1024;
}
private double getMBDownloadSpeed() {
return ((stat.BytesReceived)/1024)/1024;
}
private double getGBDownloadSpeed() {
return (((stat.BytesReceived)/1024)/1024)/1024;
}
private double getKBUploadSpeed() {
return (stat.BytesSent)/1024;
}
private double getMBUploadSpeed() {
return ((stat.BytesSent)/1024)/1024;
}
private double getGBUploadSpeed() {
return (((stat.BytesSent)/1024)/1024)/1024;
}
private void startButton_Click(object sender,EventArgs e) {
this.Width = 335;
this.Height = 154;
double size = double.Parse(inputBox.Text);
if(roundingCheckBox.Checked == true) {
if(downloadCheckBox.Checked == true) {
// download
if(kbCheckBox.Checked) {
sizeTypeLabel.Text = "Size (KB):";
double kbSpeed = size / getKBDownloadSpeed();
outputLabel.Text = kbSpeed.ToString();
}
if(mbCheckBox.Checked) {
sizeTypeLabel.Text = "Size (MB):";
}
if(gbCheckBox.Checked) {
sizeTypeLabel.Text = "Size (GB):";
}
}
if(uploadCheckBox.Checked) {
//upload
if(kbCheckBox.Checked) {
sizeTypeLabel.Text = "Size (KB):";
}
if(mbCheckBox.Checked) {
sizeTypeLabel.Text = "Size (MB):";
}
if(gbCheckBox.Checked) {
sizeTypeLabel.Text = "Size (GB):";
}
}
} else {
}
}
private void optionsButton_Click(object sender,EventArgs e) {
this.Width = 335;
this.Height = 241;
}
}
}
为什么我认为解析存在问题?因为如果我将数字添加到inputBox,然后按“计算”按钮,我的MessageBox就会出现,并显示错误。
答案 0 :(得分:1)
这似乎是由无法识别的小数点分隔符引起的。我想这些代码不是在en-US文化下运行的。
您可以尝试:
double size =
double.Parse(inputBox.Text, System.Globalization.CultureInfo.InvariantCulture)