Мой код работает, но моя программа не работает. Как я могу это исправить? - PullRequest
0 голосов
/ 21 октября 2018

В переменных есть одна часть, которая не читается:

decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);

Теперь я полагаю, что я понимаю, что причиной вывода текстового поля является число, вставленное пользователем, а не результат, которыйдолжен быть предоставлен.

Я пробовал несколько способов исправить это, но не получаю, есть ли кто-нибудь, кто может помочь мне с моей формой кодирования?

/*Distance Converter.
* In the English measurement system, 1 yard equals 3 feet and 1 foot equals 12 inches.
* Use this information to create an application that let's the user convert distances to and from inches, feet, and yards.
* The user enters the distance to be converted into a TextBox.
* A Listbox allows the user to select the units being converted from,
* and another ListBox allows the user to select the units being converted to.
* Note: Be sure to handle the situation where the user picks the asme units from both list boxes.
* The converted calue will be the same as the value entered. */

using System;
using System.Windows.Forms;

namespace _26DistanceConverter
{

   public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void btExit_Click(object sender, EventArgs e)
      {
         Application.Exit();
      }

      private void btConvert_Click(object sender, EventArgs e)
      {
         //1ft = 12" - ft to inch 1"                        1/12ft - inch to ft
         //1 yard - 3 feet - yard to ft  1ft                1/3 yards - ft to yard
         //1 yard = 3 (ft) x 12" - yard to inch             1" = 1/ (3x12) - inch to yard

         const decimal inchToFoot = 1m / 12m;
         const decimal inchToYard = 1m / (3m * 12m);
         const decimal footToInch = 12m;
         const decimal footToYard = 1m/ 3m;
         const decimal yardToInch = 3m * 12m;
         const decimal yardToFoot = 3m;
         //tb Distance Covered equals txtInput
         decimal distanceToConvert = Convert.ToDecimal(tbDistanceCovered.Text);
         //tb Output = txtOutput

         ***decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);***

         string from = LstFrom.SelectedItem.ToString().ToUpper();
         string to = LstTo.SelectedItem.ToString().ToUpper();
         tbDistanceCovered.Text = Convert.ToString("n2" + tbOutput.Text);

         if (from == "Inches" && to == "Feet")
         {
            convertedDistance = distanceToConvert * inchToFoot;
         }
         else if (from == "Inches" && to == "Yards")
         {
            convertedDistance = distanceToConvert * inchToYard;
         }
         else if (from == "Feet" && to == "Inches")
         {
            convertedDistance = distanceToConvert * footToInch;
         }
         else if (from == "Feet" && to == "Yards")
         {
            convertedDistance = distanceToConvert * footToYard;
         }
         else if (from == "Yards" && to == "Inches")
         {
            convertedDistance = distanceToConvert * yardToInch;
         }
         else if (from == "Yards" && to == "Feet")
         {
            convertedDistance = distanceToConvert * yardToFoot;
         }
         else if (from == "Yards" && to == "Yards")
         {
            convertedDistance = distanceToConvert;
         }
         else if (from == "Inches" && to == "Inches")
         {
            convertedDistance = distanceToConvert;
         }
         else if (from == "Feet" && to == "Feet")
         {
            convertedDistance = distanceToConvert; //when using same units
         }
         else
            MessageBox.Show("Please enter a valid number", "Invalid Input");
      }

      private void btClear_Click(object sender, EventArgs e)
      {
         LstFrom.ClearSelected();
         LstTo.ClearSelected();
         tbOutput.Clear();
         tbDistanceCovered.Clear();
      }
   }
}

1 Ответ

0 голосов
/ 21 октября 2018

попробуйте это:

private void btConvert_Click(object sender, EventArgs e)
{
    // put this in the begining
    decimal convertedDistance = 0;

    // rest of your code will provide a value for convertedDistance 


    // and at the end put the value in output textbox with desired formatting
    tbOutput.Text = distanceToConvert.ToString("#.##");
}

также не забудьте удалить эти строки:

decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);

и

tbDistanceCovered.Text = Convert.ToString("n2" + tbOutput.Text);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...