I have a form with listboxes used for unit conversions. I'm using the UnitsNet package.
Here are my questions:
- Is this the proper approach?
- If any changes must be made to this code, what are they?
private void listBoxControl1_SelectedIndexChanged(object sender, EventArgs e)
{
UnitTypes enumValue = (UnitTypes)Enum.Parse(typeof(UnitTypes), listBoxControl1.SelectedItem.ToString());
listBoxControl2.Items.Clear();
listBoxControl3.Items.Clear();
switch (enumValue)
{
case UnitTypes.Energy:
listBoxControl2.Items.AddRange(typeof(EnergyUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(EnergyUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Flow:
listBoxControl2.Items.AddRange(typeof(FlowUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(FlowUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Force:
listBoxControl2.Items.AddRange(typeof(ForceUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(ForceUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Frequency:
listBoxControl2.Items.AddRange(typeof(FrequencyUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(FrequencyUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Length:
listBoxControl2.Items.AddRange(typeof(LengthUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(LengthUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Number:
listBoxControl2.Items.AddRange(typeof(NumberUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(NumberUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Power:
listBoxControl2.Items.AddRange(typeof(PowerUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(PowerUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Pressure:
listBoxControl2.Items.AddRange(typeof(PressureUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(PressureUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Speed:
listBoxControl2.Items.AddRange(typeof(SpeedUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(SpeedUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Temperature:
listBoxControl2.Items.AddRange(typeof(TemperatureUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(TemperatureUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Time:
listBoxControl2.Items.AddRange(typeof(TimeUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(TimeUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Timing:
listBoxControl2.Items.AddRange(typeof(TimingUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(TimingUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Torque:
listBoxControl2.Items.AddRange(typeof(TorqueUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(TorqueUnits).GetEnumValues().Cast<object>().ToArray());
break;
case UnitTypes.Volume:
listBoxControl2.Items.AddRange(typeof(VolumeUnits).GetEnumValues().Cast<object>().ToArray());
listBoxControl3.Items.AddRange(typeof(VolumeUnits).GetEnumValues().Cast<object>().ToArray());
break;
}
}
//This is hit when selection changes in listboxes or when value changes in 'Input Value' textbox
//This handles only Length unit as of now.
private void updateValues(object sender, EventArgs e)
{
UnitTypes unitType = (UnitTypes)Enum.Parse(typeof(UnitTypes), listBoxControl1.SelectedItem.ToString());
switch (unitType)
{
case UnitTypes.Energy:
convertEnrgyUnit();
break;
case UnitTypes.Flow:
ConvertFlowUnit();
break;
case UnitTypes.Force:
ConvertForceUnit();
break;
case UnitTypes.Frequency:
ConvertFrequencyUnit();
break;
case UnitTypes.Length:
convertLengthUnit();
break;
case UnitTypes.Number:
break;
case UnitTypes.Power:
break;
case UnitTypes.Pressure:
break;
case UnitTypes.Speed:
break;
case UnitTypes.Temperature:
break;
case UnitTypes.Time:
break;
case UnitTypes.Timing:
break;
case UnitTypes.Torque:
break;
case UnitTypes.Volume:
break;
}
}
private void convertLengthUnit()
{
LengthUnits energyInputVal = LengthUnits.Feet;
LengthUnits energyOutputVal = LengthUnits.Feet;
if (listBoxControl2.SelectedItem != null)
{
energyInputVal = (LengthUnits)listBoxControl2.SelectedItem;
}
if (listBoxControl3.SelectedItem != null)
{
energyOutputVal = (LengthUnits)listBoxControl3.SelectedItem;
}
if (listBoxControl2.SelectedItem != null && listBoxControl3.SelectedItem != null && !String.IsNullOrWhiteSpace(textEdit1.Text))
{
Length inputVal = Length.FromFeet(1);
decimal outputVal = 0;
switch (energyInputVal)
{
case LengthUnits.Feet:
inputVal = Length.FromFeet(Convert.ToDouble(textEdit1.Text));
break;
case LengthUnits.Inches:
inputVal = Length.FromInches(Convert.ToDouble(textEdit1.Text));
break;
case LengthUnits.Kilometers:
inputVal = Length.FromKilometers(Convert.ToDouble(textEdit1.Text));
break;
case LengthUnits.Meters:
inputVal = Length.FromMeters(Convert.ToDouble(textEdit1.Text));
break;
case LengthUnits.Miles:
inputVal = Length.FromMiles(Convert.ToDouble(textEdit1.Text));
break;
}
switch (energyOutputVal)
{
case LengthUnits.Feet:
outputVal = Convert.ToDecimal(inputVal.Feet);
break;
case LengthUnits.Inches:
outputVal = Convert.ToDecimal(inputVal.Inches);
break;
case LengthUnits.Kilometers:
outputVal = Convert.ToDecimal(inputVal.Kilometers);
break;
case LengthUnits.Meters:
outputVal = Convert.ToDecimal(inputVal.Meters);
break;
case LengthUnits.Miles:
outputVal = Convert.ToDecimal(inputVal.Miles);
break;
}
textEdit2.Text = outputVal.ToString();
}
}
private void Form1_Load(object sender, EventArgs e)
{
listBoxControl1.Items.AddRange(typeof(UnitTypes).GetEnumValues().Cast<object>().ToArray());
}