2013-01-14 58 views
1

我正在設計一個使用C#的基本計算器。我輸入小數點時遇到困難。例如,如果我輸入.8,那麼它給我0.8這是正確的,如果顯示屏上沒有任何東西,但在那之後,如果我輸入小數點符號,那麼它也接受它是0.8 .....我想一個數字只能接受一個小數點符號。我的代碼是只允許一個文本框中的一個小數點

private void btn_Decimal_Click(object sender, EventArgs e) 
{ 
    if (txt_Result.Text == "" || LastcharIssymbol==true) 
    { 
     txt_Result.Text = txt_Result.Text + 0 + "."; 
    } 
    else 
     txt_Result.Text = txt_Result.Text + "."; 
} 

在這裏,如果我進入0.9.999那麼它也接受,如果我進入999 .....那麼它也接受。我只想要一個小數符號被接受爲999.999的一個數字。請幫幫我。另外我還添加了兩個可以顯示當前系統日期和時間的附加標籤。我無法顯示日期和時間,但我可以使用VB.Net顯示日期和時間。我不知道我在哪裏得到錯誤。我的整個代碼是

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace CS_Calculator 
{ 
    public partial class Form1 : Form 
    { 
     Boolean LastcharIssymbol {get;set;} 
     string op; 
     double a, memory; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btn_1_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "1"; 
      LastcharIssymbol= false; 
     } 

     private void btn_2_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "2"; 
      LastcharIssymbol = false; 
     } 

     private void btn_3_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "3"; 
      LastcharIssymbol = false; 
     } 

     private void btn_4_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "4"; 
      LastcharIssymbol = false; 
     } 

     private void btn_5_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "5"; 
      LastcharIssymbol = false; 
     } 

     private void btn_6_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "6"; 
      LastcharIssymbol = false; 
     } 

     private void btn_7_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "7"; 
      LastcharIssymbol = false; 
     } 

     private void btn_8_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "8"; 
      LastcharIssymbol = false; 
     } 

     private void btn_9_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "9"; 
      LastcharIssymbol = false; 
     } 

     private void btn_0_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = txt_Result.Text + "0"; 
      LastcharIssymbol = false; 
     } 

     private void btn_Decimal_Click(object sender, EventArgs e) 
     { 
      if (txt_Result.Text == "" || LastcharIssymbol==true) 
      { 
       txt_Result.Text = txt_Result.Text + 0 + "."; 
      } 
      else 
       txt_Result.Text = txt_Result.Text + "."; 
     } 

     private void btn_Plus_Click(object sender, EventArgs e) 
     { 
      if(txt_Result.Text=="" || LastcharIssymbol) 
      { 
       MessageBox.Show("Please Enter first number to perform the addition operation."); 
      } 
      else 
      { 
       op = "+"; 
       txt_Result.Text = txt_Result.Text + op; 
       LastcharIssymbol=true; 
      } 
     } 

     private void btn_Minus_Click(object sender, EventArgs e) 
     { 
      if (txt_Result.Text == "" || LastcharIssymbol) 
      { 
       MessageBox.Show("Please enter first number to erform the substraction operation."); 
      } 
      else 
      { 
       op = "-"; 
       txt_Result.Text = txt_Result.Text + op; 
       LastcharIssymbol = true; 
      } 
     } 

     private void btn_Division_Click(object sender, EventArgs e) 
     { 
      if (txt_Result.Text == "" || LastcharIssymbol) 
      { 
       MessageBox.Show("Please enter first number to perform the division operation."); 
      } 
      else 
      { 
       op = "/"; 
       txt_Result.Text = txt_Result.Text + op; 
       LastcharIssymbol = true; 
      } 
     } 

     private void btn_Mult_Click(object sender, EventArgs e) 
     { 
      if (txt_Result.Text == "" || LastcharIssymbol) 
      { 
       MessageBox.Show("Please enter first number to perform the multiplication operation."); 
      } 
      else 
      { 
       op = "*"; 
       txt_Result.Text = txt_Result.Text + op; 
       LastcharIssymbol = true; 
      } 
     } 

     private void btn_Equal_Click(object sender, EventArgs e) 
     { 

     } 

     private void button4_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = ""; 
     } 

     private void btn_Clear_All_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = ""; 
      op = ""; 
      a = 0; 
      memory = 0; 
     } 

     private void btn_Memory_Click(object sender, EventArgs e) 
     { 
      memory = Convert.ToDouble(txt_Result.Text); 
     } 

     private void btn_Show_Memory_Click(object sender, EventArgs e) 
     { 
      txt_Result.Text = memory.ToString(); 
     } 
    } 
} 
+0

請一次提出一個問題。你的標籤和日期/時間碼在哪裏?請修改代碼。大部分是不必要的。請只顯示給你的問題。另外,這功課呢? –

+0

謝謝保羅。我已經完成日曆顯示日期和時間。我寫了 lbl_Time_Disp.Text = DateTime.Now.ToLongTimeString(); lbl_Date_Disp.Text = DateTime.Now。ToLongDateString(); 在Form_Load事件和Timer_Tick事件下我寫了 lbl_Time_Disp.Text = DateTime.Now.ToLongTimeString(); 我認爲這段代碼是正確的。 – Pritam

+0

建議@Pritam,儘量不要讓你的問題變得混亂,否則人們不會讀到最後,因爲它就像是一本2000頁的書:) –

回答

1
if (!txt_Result.Text.Contains(".")) 
    if(txt_Result.Text == string.Empty) 
     txt_Result.Text = "0."; 
    else 
     txt_Result.Text += "."; 
else 
    MessageBox.Show("more dots are not allowd"); 

爲你的情況的文字像'11 0.9 +這有業務12' 你可以做以下

string formula = "11.9/1.2*99.9+19"; 

string lastPiece = formula.Split(new char[] { '+', '-', '*', '/' })[formula.Split(new char[] { '+', '-', '*', '/' }).Count() - 1]; 
if (!lastPiece.Contains('.')) formula += "."; 
//adds dot 

lastPiece = formula.Split(new char[] { '+', '-', '*', '/' })[formula.Split(new char[] { '+', '-', '*', '/' }).Count() - 1]; 

if (!lastPiece.Contains('.')) formula += "."; 
//does not add dot 

MessageBox.Show(formula); 
//output : 11.9/1.2*99.9+19. 
+0

嗨Mahdi,在這裏,如果我正在執行像11.9 + 99.9的操作,那麼對於99.9的第二個數字,它不接受小數點符號。請幫幫我。我是C#的新手。 – Pritam

+0

@Pritam it'ok pal問你的問題。 99.9已經有一個'.'爲什麼它應該接受另一個呢? –

+0

哦,我認爲剛纔你的觀點,你的意思是你的文本框也包含操作,對吧? –

1

你可以使用decimal.TryParse測試字符串是否是一個有效的十進制數。如果不是,例如您的輸入有兩個小數點,則TryParse調用失敗。

的TryParse是一個不錯的選擇,因爲可以有與除加倍小數點,如...三小數點放錯位置的減號,字母字符等輸入的數字很多問題

嘗試:

private void btn_Decimal_Click(object sender, EventArgs e) 
{ 
    decimal num; 
    if (!Decimal.TryParse(txt_Result.Text, out num)) 
    { 
     MessageBox.Show(txt_Result.Text + " is not a valid number."); 
     return; 
    } 

    if (txt_Result.Text == "" || LastcharIssymbol==true) 
     txt_Result.Text = txt_Result.Text + 0 + "."; 
    else 
     txt_Result.Text = txt_Result.Text + "."; 
} 
0

改變你的最後else到:

else if (!txt_Result.Text.Contains (".")) { 
    txt_Result.Text = txt_Result.Text + "."; 
} 

或者考慮禁用小數點按鈕。

您應該對該值進行一些驗證以確保它是有效的數字。

2

一旦點擊您應該禁用小數,並再次,如果啓用任何操作員或'C'被按下。

1
private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)//textprice key pressed 
    { 
     if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b') && (e.KeyChar != '.')) 
     { 
      e.Handled = true; 
     } 
     else 
     { 
      e.Handled = false; 
     } 
     if (Char.IsControl(e.KeyChar)) 
     { 
      e.Handled = false; 
     } 
     else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.') 
     { 
      TextBox tb = sender as TextBox; 
      int cursorPosLeft = tb.SelectionStart; 
      int cursorPosRight = tb.SelectionStart + tb.SelectionLength; 
      string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight); 
      string[] parts = result.Split('.'); 
      if (parts.Length > 1) 
      { 
       if (parts[1].Length > 2 || parts.Length > 2) 
       { 
        e.Handled = true; 
       } 
      } 
     } 
0

**又如,100%**

private void txtPrice_KeyPress(object sender, KeyPressEventArgs e) 
    { 

     if (txtPrice.Text.Length == 0) 
     { 
      if (e.KeyChar == '.') 
      { 
       e.Handled = true; 
      } 
     } 
     if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46) 
     { 
      e.Handled = true; 
     } 
     if (e.KeyChar == '.' && txtPrice.Text.IndexOf('.') > -1) 
     { 
      e.Handled = true; 
     } 
    } 
-2

將這個代碼textbox_keypress 這裏txtweight是我的文本框的名字,你用你

私人無效txtweight_KeyPress(對象發件人,KeyPressEventArgs e) {

 if (txtweight.Text.Length == 0) 
     { 
      if (e.KeyChar == '.') 
      { 
       e.Handled = true; 
      } 
     } 
     if (!char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 46) 
     { 
      e.Handled = true; 
     } 
     if (e.KeyChar == '.' && txtweight.Text.IndexOf('.') > -1) 
     { 
      e.Handled = true; 
     } 


    } 
相關問題