开发者

How to convert lengths in C# [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I'm trying to design a length conversion calculator with a combo box, so when the user selects the conversion to be performed from the combo box list and the application modifies the labels that indicate the “from” and “to” lengths accordingly. When the user enters the “from” length and clicks the Calculate button, the program should perform the conversion and display the result in a textbox.

The application should handle the following conversions:

Miles to Kilometers 1 mile = 1.6093 kilometers
Kilometers Miles 1 kilometer = 0.6214 miles
Feet Meters 1 foot = 0.3048 meters
Meters Feet 1 meter = 3.2808 feet
Inches Centimeters 1 inch = 2.54 centimeters
Centimeters Inches 1 centimeter = 0.3937 inches

The application should check the length entered by the user to make sure it is a valid decimal value.

I think I'm suppose开发者_如何学JAVAd to use a rectangular array to store the conversion information. But I have several questions.

How do I get labels to change based on the user selection from the combo box?

What should I set as the text property for the combo box?

What value type would I use for the array? Are the conversion equations members of the array?

Do I enter this under the calculate button or the combo box?

I'm very new to programming (obviously). Any help would be wonderful!

Thanks!

Kali


I suggest that you will define a Hashtable . Key will be type of conversion for example "MK" miles to kilometer and value will be the conversion rate. According to selected value you will know how to calculate it.


  1. How do I get labels to change based on the user selection from the combo box? Answer: Set the "SelectedIndexChanged" event in Designer properties.

  2. What should I set as the text property for the combo box? Answer: Set DropDownStyle to DropDown in Designer properties, then Text will be set automatically.

  3. What value type would I use for the array? Are the conversion equations members of the array? Answer: Double as value type. Set the value to number of meters. E.g. Kilometers would be 1000 and Miles would be 1609.3 The the equation would be:

    labelOutput.Text = (rates[comboBoxFrom.SelectedIndex] * Double.Parse(textBoxValue.Text) / rates[comboBoxTo.SelectedIndex]).ToString(); // E.g. 1000 * 1 / 1609.3 = 1.6093

  4. Do I enter this under the calculate button or the combo box? Answer: You can do it on SelectedIndexChanged of both comboBoxes or when Click event of the button.

Hope that this will help you.


What value type would I use for the array? Are the conversion equations members of the array?

You can create your own primitive Length type similar to Quantity pattern. It can encapsulate conversion in the Length class itself or using separate Converter:

It's useful to provide a simple interface to allow conversion on a quantity directly, although the conversion work is usually best left using a convertor object. If you only have a few units, however, it's easier to embed them directly in quantity classes.


Please have a look at my csunits project at Github. This C# library provides unit conversion for a large range of quantities, including length, and there arer also some example GUI applications included.

Good luck with the conversion!
Anders @ Cureos

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜