2016-01-20 82 views
-1

所以我第一次在Windows窗体应用程序,我试图创建一个简单的计算器,但是当创建一个函数来检查它的边缘情况不允许我在运行程序时调用函数。我正在使用C++创建winform。这是我的代码。 isValidNumber(String s)是我试图调用的函数,我收到一个错误。还附上我的错误日志创建窗口应用程序窗体时的何处放置一般功能

#pragma once 
namespace SimpleCalculator { 

using namespace System; 
using namespace System::ComponentModel; 
using namespace System::Collections; 
using namespace System::Windows::Forms; 
using namespace System::Data; 
using namespace System::Drawing; 

/// <summary> 
/// Summary for SimpleCalculator 
/// </summary> 
public ref class SimpleCalculator : public System::Windows::Forms::Form 
{ 
public: 
    SimpleCalculator(void) 
    { 
     InitializeComponent(); 
     // 
     //TODO: Add the constructor code here 
     // 
    } 

protected: 
    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    ~SimpleCalculator() 
    { 
     if (components) 
     { 
      delete components; 
     } 
    } 
private: System::Windows::Forms::TextBox^ numberTB1; 
private: System::Windows::Forms::TextBox^ numberTB2; 
private: System::Windows::Forms::ComboBox^ operationCB; 
private: System::Windows::Forms::Label^ lblTotal; 
private: System::Windows::Forms::Button^ btnCalculate; 
protected: 





protected: 

private: 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    System::ComponentModel::Container ^components; 
    bool isValidNumber(String s); 

#pragma region Windows Form Designer generated code 
    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    void InitializeComponent(void) 
    { 
     this->numberTB1 = (gcnew System::Windows::Forms::TextBox()); 
     this->numberTB2 = (gcnew System::Windows::Forms::TextBox()); 
     this->operationCB = (gcnew System::Windows::Forms::ComboBox()); 
     this->lblTotal = (gcnew System::Windows::Forms::Label()); 
     this->btnCalculate = (gcnew System::Windows::Forms::Button()); 
     this->SuspendLayout(); 
     // 
     // numberTB1 
     // 
     this->numberTB1->Location = System::Drawing::Point(23, 38); 
     this->numberTB1->Name = L"numberTB1"; 
     this->numberTB1->Size = System::Drawing::Size(112, 20); 
     this->numberTB1->TabIndex = 0; 
     // 
     // numberTB2 
     // 
     this->numberTB2->Location = System::Drawing::Point(23, 64); 
     this->numberTB2->Name = L"numberTB2"; 
     this->numberTB2->Size = System::Drawing::Size(111, 20); 
     this->numberTB2->TabIndex = 1; 
     // 
     // operationCB 
     // 
     this->operationCB->AccessibleDescription = L""; 
     this->operationCB->DropDownStyle = System::Windows::Forms::ComboBoxStyle::DropDownList; 
     this->operationCB->FormattingEnabled = true; 
     this->operationCB->Items->AddRange(gcnew cli::array<System::Object^>(4) { L"Add", L"Subtract", L"Multiply", L"Divide" }); 
     this->operationCB->Location = System::Drawing::Point(141, 49); 
     this->operationCB->Name = L"operationCB"; 
     this->operationCB->Size = System::Drawing::Size(112, 21); 
     this->operationCB->TabIndex = 2; 
     // 
     // lblTotal 
     // 
     this->lblTotal->BackColor = System::Drawing::Color::White; 
     this->lblTotal->BorderStyle = System::Windows::Forms::BorderStyle::Fixed3D; 
     this->lblTotal->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
      static_cast<System::Byte>(0))); 
     this->lblTotal->Location = System::Drawing::Point(23, 105); 
     this->lblTotal->Name = L"lblTotal"; 
     this->lblTotal->Size = System::Drawing::Size(229, 36); 
     this->lblTotal->TabIndex = 3; 
     this->lblTotal->Text = L"0"; 
     // 
     // btnCalculate 
     // 
     this->btnCalculate->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 20.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, 
      static_cast<System::Byte>(0))); 
     this->btnCalculate->Location = System::Drawing::Point(23, 172); 
     this->btnCalculate->Name = L"btnCalculate"; 
     this->btnCalculate->Size = System::Drawing::Size(229, 42); 
     this->btnCalculate->TabIndex = 4; 
     this->btnCalculate->Text = L"Calculate"; 
     this->btnCalculate->UseVisualStyleBackColor = true; 
     this->btnCalculate->Click += gcnew System::EventHandler(this, &SimpleCalculator::btnCalculate_Click); 
     // 
     // SimpleCalculator 
     // 
     this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); 
     this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; 
     this->ClientSize = System::Drawing::Size(284, 261); 
     this->Controls->Add(this->btnCalculate); 
     this->Controls->Add(this->lblTotal); 
     this->Controls->Add(this->operationCB); 
     this->Controls->Add(this->numberTB2); 
     this->Controls->Add(this->numberTB1); 
     this->Name = L"SimpleCalculator"; 
     this->Text = L"SimpleCalculator"; 
     this->ResumeLayout(false); 
     this->PerformLayout(); 

    } 
#pragma endregion 
    double firstNum; 
    double secondNum; 
    double total; 

private: System::Void btnCalculate_Click(System::Object^ sender, System::EventArgs^ e) { 
    if (SimpleCalculator::isValidNumber(numberTB1->ToString) == false || SimpleCalculator::isValidNumber(numberTB2->ToString) == false){ 
     MessageBox::Show("Input valid Number in TextBox", "Invalid Number Input", MessageBoxButtons::OK, MessageBoxIcon::Error); 
    } 
    else if (operationCB->ToString == ""){ 
     MessageBox::Show("Input valid Operation in Dropdown", "Invalid Operation Input", MessageBoxButtons::OK, MessageBoxIcon::Error); 
    } 
    else if (operationCB->ToString == "Divide" && Convert::ToDouble(numberTB2->Text) == 0){ 
     MessageBox::Show("Invalid Division Operation. Cannot Divide by 0", "0 Division Operation", MessageBoxButtons::OK, MessageBoxIcon::Error); 
    } 
    else{ 
     firstNum = Convert::ToDouble(numberTB1->Text); 
     secondNum = Convert::ToDouble(numberTB2->Text); 

     if ((this->operationCB)->Text == "Add"){ 
      total = firstNum + secondNum; 
      lblTotal->Text = Convert::ToString(total); 
     } 
     else if ((this->operationCB)->Text == "Subtract"){ 
      total = firstNum - secondNum; 
      lblTotal->Text = Convert::ToString(total); 
     } 
     else if ((this->operationCB)->Text == "Multiply"){ 
      total = firstNum * secondNum; 
      lblTotal->Text = Convert::ToString(total); 
     } 
     else { 
      total = firstNum/secondNum; 
      lblTotal->Text = Convert::ToString(total); 
     } 
    } 
} 
}; 

bool isValidNumber(String s){ 
bool validDecimal = true; 

if ((s.Length == 1 && s[0] == '.') || s.Length == 0){ 
    return false; 
} 

int i = 0; 
while (i < s.Length){ 
    if (s[i].IsDigit || (s[i] == '.' && validDecimal == true)){ 
     if (s[i] == '.') 
      validDecimal = false; 
    } 
    else{ 
     return false; 
    } 
} 
return true; 
} 

} 

错误2错误C3867: '系统:视窗:形式:TextBoxBase ::的ToString':函数调用缺少参数列表;使用'& System :: Windows :: Forms :: TextBoxBase :: ToString'来创建指向成员c:\ users \ user \ documents \ visual studio 2013 \ projects \ simplecalculator \ simplecalculator \ SimpleCalculator.h的指针142 1 SimpleCalculator

+0

ToString是*方法*,不是属性。您必须使用ToString() –

回答

0

其实这个问题不是WinForms相关的,而是相当微不足道的错误。有几个地方缺少ToString括号(ToString是一种方法)。

你可以很容易地通过添加括号来修复它,它会编译,但最有可能你会得到一个错误的结果。您真正需要做的是将->ToString(主要用于调试目的)替换为相应的属性访问器->Text

你应该做的另一件事是使isValidNumber方法static,因为它不使用任何状态。

static bool isValidNumber(String s){ 
    // ... 
} 
相关问题