在创建Windows应用程序表单时放置常规函数的位置

时间:2016-01-20 06:07:42

标签: winforms function c++-cli

所以我第一次在Windows Forms Applications中工作,我试图创建一个简单的计算器,但是在创建一个函数来检查边缘情况时,它不允许我调用该函数每当我运行程序。我用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:&#39; System :: Windows :: Forms :: TextBoxBase :: ToString&#39;:函数调用缺少参数列表;使用&#39;&amp; System :: Windows :: Forms :: TextBoxBase :: ToString&#39;创建指向成员的指针c:\ users \ user \ documents \ visual studio 2013 \ projects \ simplecalculator \ simplecalculator \ SimpleCalculator.h 142 1 SimpleCalculator

1 个答案:

答案 0 :(得分:0)

实际上问题不是与WinForms相关,而是相当琐碎的错误。有几个地方缺少ToString个括号(ToString是一个方法)。

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

您应该做的另一件事是制作isValidNumber方法static,因为它没有使用任何州。

static bool isValidNumber(String s){
    // ...
}