30 days of. Net [Windows Mobile applications]-day 04: mileage tracker (mileage fuel-consuming computing program)

Source: Internet
Author: User
ArticleDirectory
    • Input parameters
    • Output
    • Interface Processing

For the original article, see day 04: mileage tracker.

Requirement

Chris Reeder, a good friend of the author, hopes to use SQL ce database for fuel-efficient mileage computing.Program.

Implementation

As the author was very busy that day, he simplified the requirements, removed the sqlce storage, the support of international organizations, the status chart, and the multi-time mileage comparison. As the requirement is simplified, the function becomes simple. You only need to enter several parameters and then calculate the result.

Input parameters

1. distance: the author uses miles as the unit. I remember my colleague asked me if I used an international unit in China. I said that China had unified the Unit during Qin Dynasty, the international organization was used at the time of liberation. From the author's article, many people prefer non-international organizations, such as Miles and gallon.
2. fuel volume. The author uses gallon ).
3. Price.
4. cylinder volume.

Output

1. miles per gallon: How many miles can a Galen walk, that is, the Distance divided by the amount of oil. Since the program does not perform unit conversion, we can assume that the input distance is kilometers, and the input amount of oil is increased, the result is "every kilometer.
2. gallons per 100 miles, how much galun oil is consumed per 100 miles.
3. Cost per 100 miles, how much is spent every 100 miles.
4. Maximum range: How long can an entire cylinder of oil go.

During implementation, the author encapsulates all parameters and computing into a class called mileage for processing.

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;
}

The calculation method is a simple arithmetic operation. Note that all numeric variables use decimal instead of float and double. People who have worked on accounting software will know that float and double often encounter errors when performing multiplication and division. In reality, use. tostring ("0.00") for formatting, and use stringbuilder to merge strings to improve efficiency.

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 ();

 

Interface Processing

Since Compact framework does not directly support transparent controls, the author uses the transparent label controls of Alex yakhnin-transparent labels.
To use this transparent label control, you need to use a picturebox to save a graphic object. The label is placed in this picturebox and set to invisible, which is displayed by the drawlabel method, the call of the drawlabel method is triggered by the paint event of picturebox.

The digital input control. The author uses the MS nuerictextbox. For details, refer to how to: create a numeric text box. This article describes how to generate Custom User Controls by inheriting textbox.

// 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 = system. globalization. cultureinfo. currentculture. numberformat;
string decimalseparator = numberformatinfo. numberdecimalseparator;
string groupseparator = numberformatinfo. numbergroupseparator;
string negativesign = numberformatinfo. negativesign;

StringKeyinput=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 ();
}
}

The key to this class is to overload the onkeypress event and control the input to be numerical only.

Installer:Mileagetracker. Cab

Source code:Milesagetracker.zip
 

. NET Compact framework, WinCE, Windows Mobile Development Series

Jake's blog in blog Park --Simplified development and wireless life

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.