天天看點

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

作者的好朋友Chris Reeder希望有一個使用SQL CE database的裡程耗油計算程式。

由于作者那天很忙,是以把需求簡化了,去掉SqlCe的存儲,國際機關的支援,狀态圖,多次裡程比較等等需求。由于需求的簡化,功能變得簡便,隻是需要輸入幾個參數,然後計算出結果。

1.距離,作者使用了英裡(Miles)作為機關,記得以前同僚問我中國是否使用國際機關,我說中國在秦的時候就統一了機關,解放的時候就統一使用國際機關了。從作者的文章看很多人還是喜歡使用非國際機關,例如Miles和Gallon那些。

2.油量,作者使用加倫(Gallon)。

3.價格。

4.油缸量。 

1.Miles Per Gallon,一加倫能走多少英裡,就是距離除以油量,由于程式沒有進行機關轉換,我們可以假設在輸入的距離為公裡,輸入的油量為升,那麼結果就是”公裡每升“了。

2.Gallons Per 100 Miles,每100英裡耗多少加倫的油。

3.Cost Per 100 Miles,每100英裡花多少錢。

4.Maximum Range,一整缸油能走多長路程。

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

在實作的時候作者把所有的參數和計算封裝到一個叫做Mileage的類進行處理。

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

public void Calculate()

{

    MilesPerGallon = 0;

    GallonsPer100Miles = 0;

    if(Fuel != 0)

        MilesPerGallon = Distance/Fuel;

    if (MilesPerGallon != 0)

        GallonsPer100Miles = 100/MilesPerGallon;

    CostPer100Miles = GallonsPer100Miles*Price;

    MaximumRange = Tank*MilesPerGallon;

}

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

計算方法就是簡單的四則運算。需要注意到地方是,所有數字變量使用decimal而不是float和double,做過會計軟體的人會知道float和double做乘除法的時候經常出錯。在現實的時候使用.ToString("0.00")進行格式化,使用StringBuilder進行合并字元串操作,提高效率。

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.AppendFormat("Miles Per Gallon: {0} Mpg", mileage.MilesPerGallon.ToString("0.00")).AppendLine().AppendLine();

stringBuilder.AppendFormat("{0} Gallons Per 100 Miles", mileage.GallonsPer100Miles.ToString("0.00")).AppendLine().AppendLine();

stringBuilder.AppendFormat("${0} Per 100 Miles", mileage.CostPer100Miles.ToString("0.00")).AppendLine().AppendLine();

stringBuilder.AppendFormat("Maximum Range: {0} Miles", mileage.MaximumRange.ToString("0.00"));

textBoxResults.Text = stringBuilder.ToString();

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

使用該透明标簽控件,需要使用一個PictureBox儲存一個圖形對象,Label放在這個PictureBox,并設定為不可見,由DrawLabel方法來顯示,DrawLabel方法的調用是通過PictureBox的Paint事件觸發。

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

// Restricts the entry of characters to digits (including hex), the negative sign,

// the decimal point, and editing keystrokes (backspace).

protected override void OnKeyPress(KeyPressEventArgs e)

    base.OnKeyPress(e);

    NumberFormatInfo numberFormatInfo = System.Globalization.CultureInfo.CurrentCulture.NumberFormat;

    string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;

    string groupSeparator = numberFormatInfo.NumberGroupSeparator;

    string negativeSign = numberFormatInfo.NegativeSign;

    string keyInput = e.KeyChar.ToString();

    if (Char.IsDigit(e.KeyChar))

    {

        // Digits are OK

    }

    else if (keyInput.Equals(decimalSeparator) || keyInput.Equals(groupSeparator) ||

     keyInput.Equals(negativeSign))

        // Decimal separator is OK

    else if (e.KeyChar == '\b')

        // Backspace key is OK

    //    else if ((ModifierKeys & (Keys.Control | Keys.Alt)) != 0)

    //    {

    //     // Let the edit control handle control and alt key combinations

    //    }

    else if (this.allowSpace && e.KeyChar == ' ')

    else

        // Consume this invalid key and beep

        e.Handled = true;

        //    MessageBeep();

30 Days of .NET [Windows Mobile Applications] - Day 04: Mileage Tracker(裡程耗油計算程式)需求實作

這個類的關鍵是重載OnKeyPress事件,控制輸入隻能為數值類型。

<a href="http://www.cnblogs.com/procoder/archive/2009/05/archive/2009/04/13/1434628.html">.NET Compact Framework, WinCE, Windows Mobile開發系列</a>

    本文轉自Jake Lin部落格園部落格,原文連結:http://www.cnblogs.com/procoder/archive/2009/05/26/1490382.html,如需轉載請自行聯系原作者

繼續閱讀