1

我的下载/上传速度计算器有问题。我将数字写到“inputBox”中,我认为它无法解析它。这是源代码:

!更新!

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 就会出现,并显示错误。

4

1 回答 1

1

这似乎是由无法识别的小数分隔符引起的。我猜代码不是在美国文化下运行的。

你可以试试:

double size = 
  double.Parse(inputBox.Text, System.Globalization.CultureInfo.InvariantCulture)
于 2013-07-08T19:41:51.853 回答