2014-03-31 38 views
0

當我的代碼是一個獨立的表單時,它的工作原理非常完美。我將它作爲子表單插入到一個mdi容器中,它編譯並運行良好,但是當我關閉表單時,它顯示錯誤「輸入字符串格式不正確」。在下面的代碼:代碼不能用作子表單

int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text))/1024; 
    int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text))/1024; 

以下是帶寬監視器,它應該是孩子的代碼。

public partial class FrmBMon : Form 
      { 


    private const double timerUpdate = 1000; 
    private NetworkInterface[] nicArr; 
    private Timer timer; 

    public FrmBMon() 
    { 
     InitializeComponent(); 
     InitializeNetworkInterface(); 
     InitializeTimer(); 
    } 

    private void InitializeNetworkInterface() 
    { 
     // Grab all local interfaces to this computer 
     nicArr = NetworkInterface.GetAllNetworkInterfaces(); 

     // Add each interface name to the combo box 
     for (int i = 0; i < nicArr.Length; i++) 
      cmbInterface.Items.Add(nicArr[i].Name); 

     // Change the initial selection to the first interface 
     cmbInterface.SelectedIndex = 0; 
    } 

    private void InitializeTimer() 
    { 
     timer = new Timer(); 
     timer.Interval = (int)timerUpdate; 
     timer.Tick += new EventHandler(timer_Tick); 
     timer.Start(); 
    } 


    private void UpdateNetworkInterface() 
    { 
     // Grab NetworkInterface object that describes the current interface 
     NetworkInterface nic = nicArr[cmbInterface.SelectedIndex]; 

     // Grab the stats for that interface 
     IPv4InterfaceStatistics interfaceStats = nic.GetIPv4Statistics(); 

     // Calculate the speed of bytes going in and out 

     int bytesSentSpeed = (int)(interfaceStats.BytesSent - double.Parse(lblBytesSent.Text))/1024; 
     int bytesReceivedSpeed = (int)(interfaceStats.BytesReceived - double.Parse(lblBytesReceived.Text))/1024; 

     // Update the labels 
     lblSpeed.Text = nic.Speed.ToString(); 
     lblInterfaceType.Text = nic.NetworkInterfaceType.ToString(); 
     lblSpeed.Text = nic.Speed.ToString(); 
     lblBytesReceived.Text = interfaceStats.BytesReceived.ToString(); 
     lblBytesSent.Text = interfaceStats.BytesSent.ToString(); 
     lblUpload.Text = bytesSentSpeed.ToString() + " KB/s"; 
     lblDownload.Text = bytesReceivedSpeed.ToString() + " KB/s"; 

    } 

    /// <summary> 
    /// The Timer event for each Tick (second) to update the UI 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    void timer_Tick(object sender, EventArgs e) 
    { 
     UpdateNetworkInterface(); 
    } 
    private void btn_Click(object sender, EventArgs e) 
    { 
     this.Close(); 


    } 

    private void btnCal_Click(object sender, EventArgs e) 
    { 
     string url = @"http://www.microsoft.com/downloads/info.aspx?na=41&srcfamilyid=92ced922-d505-457a-8c9c-84036160639f&srcdisplaylang=en&u=http%3a%2f%2fdownload.microsoft.com%2fdownload%2f2%2f9%2f6%2f296AAFA4-669A-46FE-9509-93753F7B0F46%2fVS-KB-Brochure-CSharp-Letter-HiRez.pdf"; 
     System.Net.WebClient client = new System.Net.WebClient(); 
client.DownloadFileAsync(new Uri(url), Path.GetTempFileName()); 
    } 


} 

這裏是我插入主要父​​窗體中的代碼。

private void bandwidthMonitorToolStripMenuItem_Click(object sender, EventArgs e) 
    { 
     FrmBMon fbm = new FrmBMon(); 
     fbm.MdiParent = this; 
     fbm.Show(); 
    } 
+4

顯示周邊代碼。這是在事件處理程序? –

+0

我已更新我的問題。 – user3382196

回答

0

您應該在關閉表單之前停止計時器,這將確保在事後沒有勾號。也就是說,你不應該解析標籤的文本。您應該首先使用您用來填充標籤的任何數據。使用雙變量來存儲該值,然後將其顯示在標籤中。

+0

謝謝,它工作。 – user3382196