06 實現判斷小數點輸入位置的四則運算器

來源:互聯網
上載者:User

標籤:result   輸入   blog   drop   控制項   ext   code   change   實現   

 1 private void txtNumber1_KeyPress(object sender, KeyPressEventArgs e) 2         { 3             //MessageBox.Show(((int)(e.KeyChar)).ToString()); //ASCii查看 4             //由於本事件可能有多個文字框的觸發,所以要通過sender來得到觸發該事件的控制項 5             TextBox currentTextBox = sender as TextBox; 6  7             if (e.KeyChar < ‘0‘ || e.KeyChar > ‘9‘) 8             { 9                 e.Handled = true;10             }11             if (e.KeyChar == 46)//讓使用者可以輸入小數點12             {13                 //檢測這個number1文字框中是否已經包含小數點14                 if (currentTextBox.Text.IndexOf(".") == -1)15                 {16                     //游標不能在第一位,也就是說小數點不能出現在第一位17                     if (currentTextBox.SelectionStart > 0)18                     {19                         e.Handled = false;20                     }21                 }22             }23             if (e.KeyChar == 8)  //讓使用者可以輸入退格鍵24             {25                 e.Handled = false;26             }27             28         }29 30         private void btnResult_Click(object sender, EventArgs e)31         {32             try33             {34                 double number1 = Convert.ToDouble(txtNumber1.Text);35                 double number2 = Convert.ToDouble(txtNumber2.Text);36                  37                 switch (comboBox1.Text)38                 {39                     case "加":40                         lbResult.Text = (number1 + number2).ToString();41                         break;42                     case "減":43                         lbResult.Text = (number1 - number2).ToString();44                         break;45                     case "乘":46                         lbResult.Text = (number1 * number2).ToString();47                         break;48                     case "除":49                         lbResult.Text = (number1 / number2).ToString();50                         break;51                 }52             }53             catch(Exception ex)54             {55                 MessageBox.Show("你的輸入有問題"+ex.Message);56             }57         }58 59         private void txtNumber1_TextChanged(object sender, EventArgs e)60         {61             lbResult.Text = " "; 62         }63 64         private void frmFourOperations_Load(object sender, EventArgs e)65         {66             comboBox1.SelectedIndex = 0; //當combobox的dropdownstyle屬性設定為list時 將無法在屬性寫入初始值 需要通過該語句實現67         }

 

06 實現判斷小數點輸入位置的四則運算器

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.