2015-12-10 4 views
0

Создание калькулятора, чтобы попытаться привыкнуть к кодированию на C# с помощью Visual studio. Я почти там, но получаю сообщение об ошибке, показанном в заголовке, когда я пытаюсь нажать два символа (т. Е. **). Как я могу остановить это и остановить пользователя, чтобы он мог ввести два символа? Я пробовал через booleans сказать false или true, но стал немного застрял!Необработанное исключение типа «System.FormatException» произошло в mscorlib.dll, stuck

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

     Boolean Decimalcick = true; 
     Boolean Plusclick = true; 
     Boolean Multiplyclick = true; 
     Boolean Devisionclick = true; 
     Boolean Subtractclick = true; 

     float num, ans; 
     int count; 

     private void Button_click(object sender, EventArgs e) 
     { 
      Button button = (Button)sender;   //adds the numbers into the screen when clicked 
      Screen.Text = Screen.Text + button.Text; 
      Screen.ForeColor = Color.Red; //text that entered appears red 

      Decimalcick = true; 
      Plusclick = true; 
      Multiplyclick = true; 
      Devisionclick = true; 
      Subtractclick = true; 
     } 

     private void operatorclick(object sender, EventArgs e) 
     { 
      Button button = (Button)sender;   //adds the symbols into the screen when clicked 
      Screen.Text = Screen.Text + button.Text; //all symbols are under button_click so i do not have to repeat the code over/ 
     } 

     private void Clearclick(object sender, EventArgs e) 
     { 
      Screen.Clear(); //when clicked clears the screen 
      Result.Clear(); //when clicked clears the result 

      Decimalcick = true; 
      Plusclick = true; 
      Multiplyclick = true; 
      Devisionclick = true; 
      Subtractclick = true; 
     } 

     private void Decimalclick(object sender, EventArgs e) 
     { 
      Screen.Text = Screen.Text + "."; //adds decimal point to screen when/if clicked 
      Screen.ForeColor = Color.Red; //decimal point appears red 

      Decimalcick = false; 
      Plusclick = false; 
      Multiplyclick = false; 
      Devisionclick = false; 
      Subtractclick = false; 
     } 

     private void Closebtn_Click(object sender, EventArgs e) 
     { 
      this.Close(); // closes the application down 
     } 

     private void plusclick(object sender, EventArgs e) //addition 
     { 
      num = float.Parse(Screen.Text); 
      Screen.Clear(); //clears the screen of everything 
      Screen.Focus(); //textbox is focused upon when the screen is cleared 
      count = 1; //this counts the store case 
      Result.Text = num.ToString() + "+"; //this puts the text onto the top text box 

      Decimalcick = false; 
      Plusclick = false; 
      Multiplyclick = false; 
      Devisionclick = false; 
      Subtractclick = false; 
     } 

     private void multiplyclick(object sender, EventArgs e) //multiply 
     { 
      num = float.Parse(Screen.Text); 
      Screen.Clear(); //clears the screen of everything 
      Screen.Focus(); //textbox is focused upon when the screen is cleared 
      count = 3; //this counts the store case 
      Result.Text = num.ToString() + "*"; //this puts the text onto the top textbox 

      Decimalcick = false; 
      Plusclick = false; 
      Multiplyclick = false; 
      Devisionclick = false; 
      Subtractclick = false; 
     } 

     private void divideclick(object sender, EventArgs e) //divide 
     { 
      num = float.Parse(Screen.Text); 
      Screen.Clear(); //clears the screen of everything 
      Screen.Focus(); //textbox is focused upon when the screen is cleared 
      count = 4; //this counts the store case 
      Result.Text = num.ToString() + "/"; //this puts the text onto the lab 

      Decimalcick = false; 
      Plusclick = false; 
      Multiplyclick = false; 
      Devisionclick = false; 
      Subtractclick = false; 
     } 

     private void subtractclick(object sender, EventArgs e) //subtract 
     { 
      num = float.Parse(Screen.Text); 
      Screen.Clear(); //clears the screen of everything 
      Screen.Focus(); //textbox is focused upon when the screen is cleared 
      count = 2; //this counts the store case 
      Result.Text = num.ToString() + "-"; //this puts the text onto the label 

      Decimalcick = false; 
      Plusclick = false; 
      Multiplyclick = false; 
      Devisionclick = false; 
      Subtractclick = false; 
     } 

     private void equalsclick(object sender, EventArgs e) 
     { 
      switch (count) //initalising switch statement 
      { 
       case 1: 
        ans = num + float.Parse(Screen.Text);//Adding numbers 
        Result.Text = ans.ToString();   //this converts my answer from a float to a string 
        break; 
       case 2: 
        ans = num - float.Parse(Screen.Text); //Subtracting numbers 
        Result.Text = ans.ToString();   //float to a string 
        break; 
       case 3: 
        ans = num * float.Parse(Screen.Text); //Multiplying numbers 
        Result.Text = ans.ToString();   //float to a string 
        break; 
       case 4: 
        ans = num/float.Parse(Screen.Text); //Division of numbers 
        Result.Text = ans.ToString();   //float to a string 
        break; 
       default:         //the default figure 
        break; 
      } 
     } 
    } 
} 

ответ

0

Добавьте к каждому обработчику кнопки оператора:

if("+-/*".Contains(Result.Text[Result.Text.Length - 1])) return; 

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

+0

спасибо, но он все еще не устраивает 'num = float.Parse (Screen.Text);' –

+0

Вам необходимо проверить Screen.Text с указанным выше кодом точно так же, как Result.Text в ваших обработчиках. Это происходит в equalsclick (...). –

+0

просто попробовал, и ему все равно не нравится 'num = float.Parse (Screen.Text);' каждый раз вызывает это сообщение :( –

Смежные вопросы