
Добавил:
eipimru
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз:
Предмет:
Файл:Лаба 1 (Калькулятор) / MyForm
.h#pragma once
namespace Pr1 {
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>
/// Сводка для MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: добавьте код конструктора
//
}
protected:
/// <summary>
/// Освободить все используемые ресурсы.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ AddButton;
private: System::Windows::Forms::Button^ subtract;
private: System::Windows::Forms::Button^ multiply;
private: System::Windows::Forms::Button^ divide;
private: System::Windows::Forms::Button^ reset;
private: System::Windows::Forms::Button^ close;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBoxResult;
private: System::Windows::Forms::Label^ operationLabel;
private: System::Windows::Forms::Label^ ErrorLabel1;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ label5;
private: System::Windows::Forms::Label^ label6;
protected:
private:
/// <summary>
/// Обязательная переменная конструктора.
/// </summary>
System::ComponentModel::Container^ components;
#pragma region Windows Form Designer generated code
/// <summary>
/// Требуемый метод для поддержки конструктора — не изменяйте
/// содержимое этого метода с помощью редактора кода.
/// </summary>
void InitializeComponent(void)
{
this->AddButton = (gcnew System::Windows::Forms::Button());
this->subtract = (gcnew System::Windows::Forms::Button());
this->multiply = (gcnew System::Windows::Forms::Button());
this->divide = (gcnew System::Windows::Forms::Button());
this->reset = (gcnew System::Windows::Forms::Button());
this->close = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBoxResult = (gcnew System::Windows::Forms::TextBox());
this->operationLabel = (gcnew System::Windows::Forms::Label());
this->ErrorLabel1 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->label5 = (gcnew System::Windows::Forms::Label());
this->label6 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// AddButton
//
this->AddButton->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->AddButton->Location = System::Drawing::Point(393, 8);
this->AddButton->Name = L"AddButton";
this->AddButton->Size = System::Drawing::Size(98, 27);
this->AddButton->TabIndex = 0;
this->AddButton->Text = L"Сложение";
this->AddButton->UseVisualStyleBackColor = true;
this->AddButton->Click += gcnew System::EventHandler(this, &MyForm::AddButton_Click);
//
// subtract
//
this->subtract->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->subtract->Location = System::Drawing::Point(393, 40);
this->subtract->Name = L"subtract";
this->subtract->Size = System::Drawing::Size(98, 27);
this->subtract->TabIndex = 1;
this->subtract->Text = L"Вычитание";
this->subtract->UseVisualStyleBackColor = true;
this->subtract->Click += gcnew System::EventHandler(this, &MyForm::subtract_Click);
//
// multiply
//
this->multiply->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 7, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->multiply->Location = System::Drawing::Point(393, 74);
this->multiply->Name = L"multiply";
this->multiply->Size = System::Drawing::Size(98, 27);
this->multiply->TabIndex = 2;
this->multiply->Text = L"Умножение";
this->multiply->UseVisualStyleBackColor = true;
this->multiply->Click += gcnew System::EventHandler(this, &MyForm::multiply_Click);
//
// divide
//
this->divide->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->divide->Location = System::Drawing::Point(393, 107);
this->divide->Name = L"divide";
this->divide->Size = System::Drawing::Size(98, 27);
this->divide->TabIndex = 3;
this->divide->Text = L"Деление";
this->divide->UseVisualStyleBackColor = true;
this->divide->Click += gcnew System::EventHandler(this, &MyForm::divide_Click);
//
// reset
//
this->reset->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->reset->Location = System::Drawing::Point(393, 140);
this->reset->Name = L"reset";
this->reset->Size = System::Drawing::Size(98, 27);
this->reset->TabIndex = 4;
this->reset->Text = L"Сброс";
this->reset->UseVisualStyleBackColor = true;
this->reset->Click += gcnew System::EventHandler(this, &MyForm::reset_Click);
//
// close
//
this->close->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 8, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->close->Location = System::Drawing::Point(393, 173);
this->close->Name = L"close";
this->close->Size = System::Drawing::Size(98, 27);
this->close->TabIndex = 5;
this->close->Text = L"Закрыть";
this->close->UseVisualStyleBackColor = true;
this->close->Click += gcnew System::EventHandler(this, &MyForm::close_Click);
//
// textBox1
//
this->textBox1->CharacterCasing = System::Windows::Forms::CharacterCasing::Lower;
this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->textBox1->Location = System::Drawing::Point(117, 41);
this->textBox1->MaxLength = 100;
this->textBox1->Name = L"textBox1";
this->textBox1->ShortcutsEnabled = false;
this->textBox1->Size = System::Drawing::Size(167, 26);
this->textBox1->TabIndex = 6;
this->textBox1->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
this->textBox1->WordWrap = false;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox1_TextChanged);
this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox1_KeyPress);
//
// textBox2
//
this->textBox2->CharacterCasing = System::Windows::Forms::CharacterCasing::Lower;
this->textBox2->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->textBox2->Location = System::Drawing::Point(117, 73);
this->textBox2->MaxLength = 100;
this->textBox2->Name = L"textBox2";
this->textBox2->ShortcutsEnabled = false;
this->textBox2->Size = System::Drawing::Size(167, 26);
this->textBox2->TabIndex = 7;
this->textBox2->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
this->textBox2->WordWrap = false;
this->textBox2->TextChanged += gcnew System::EventHandler(this, &MyForm::textBox2_TextChanged);
this->textBox2->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &MyForm::textBox2_KeyPress);
//
// textBoxResult
//
this->textBoxResult->CharacterCasing = System::Windows::Forms::CharacterCasing::Lower;
this->textBoxResult->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->textBoxResult->Location = System::Drawing::Point(117, 102);
this->textBoxResult->MaxLength = 100;
this->textBoxResult->Name = L"textBoxResult";
this->textBoxResult->ReadOnly = true;
this->textBoxResult->ShortcutsEnabled = false;
this->textBoxResult->Size = System::Drawing::Size(167, 26);
this->textBoxResult->TabIndex = 8;
this->textBoxResult->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
this->textBoxResult->WordWrap = false;
//
// operationLabel
//
this->operationLabel->AutoSize = true;
this->operationLabel->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 15, System::Drawing::FontStyle::Bold, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->operationLabel->Location = System::Drawing::Point(12, 8);
this->operationLabel->Name = L"operationLabel";
this->operationLabel->Size = System::Drawing::Size(0, 29);
this->operationLabel->TabIndex = 9;
//
// ErrorLabel1
//
this->ErrorLabel1->AutoEllipsis = true;
this->ErrorLabel1->AutoSize = true;
this->ErrorLabel1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(204)));
this->ErrorLabel1->ForeColor = System::Drawing::Color::FromArgb(static_cast<System::Int32>(static_cast<System::Byte>(192)), static_cast<System::Int32>(static_cast<System::Byte>(0)),
static_cast<System::Int32>(static_cast<System::Byte>(0)));
this->ErrorLabel1->Location = System::Drawing::Point(11, 131);
this->ErrorLabel1->Name = L"ErrorLabel1";
this->ErrorLabel1->Size = System::Drawing::Size(353, 40);
this->ErrorLabel1->TabIndex = 10;
this->ErrorLabel1->Text = L"Делить на 0 нельзя,\nвторое число должно быть отлично от 0";
this->ErrorLabel1->Visible = false;
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(12, 47);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(99, 16);
this->label4->TabIndex = 12;
this->label4->Text = L"Первое число";
//
// label5
//
this->label5->AutoSize = true;
this->label5->Location = System::Drawing::Point(12, 79);
this->label5->Name = L"label5";
this->label5->Size = System::Drawing::Size(97, 16);
this->label5->TabIndex = 13;
this->label5->Text = L"Второе число";
//
// label6
//
this->label6->AutoSize = true;
this->label6->Location = System::Drawing::Point(12, 108);
this->label6->Name = L"label6";
this->label6->Size = System::Drawing::Size(77, 16);
this->label6->TabIndex = 14;
this->label6->Text = L"Результат";
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(503, 229);
this->Controls->Add(this->label6);
this->Controls->Add(this->label5);
this->Controls->Add(this->label4);
this->Controls->Add(this->ErrorLabel1);
this->Controls->Add(this->operationLabel);
this->Controls->Add(this->textBoxResult);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->close);
this->Controls->Add(this->reset);
this->Controls->Add(this->divide);
this->Controls->Add(this->multiply);
this->Controls->Add(this->subtract);
this->Controls->Add(this->AddButton);
this->Name = L"MyForm";
this->Text = L"Калькулятор";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: Void textBox_KeyPress(System::Windows::Forms::KeyPressEventArgs^ e, System::Windows::Forms::TextBox^ textbox)
{
textBoxResult->Text = "";
if ((e->KeyChar >= '0') && (e->KeyChar <= '9') // ввод цифр
|| e->KeyChar == 127 // delete
|| e->KeyChar == 8 // backspace
|| ((e->KeyChar == '.' || e->KeyChar == ',') && // ввод . или ,
(textbox->Text->IndexOf(L",") == -1 && textbox->Text->IndexOf(L".") == -1)) // если в строке уже нет одной запятой или точки
|| ((e->KeyChar == '-' && textbox->Text->IndexOf(L"-") == -1))) // не дает поставить 2 минуса
return;
e->Handled = true; // иначе не вводить
}
private: System::Void textBox1_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
textBox_KeyPress(e, textBox1);
}
private: System::Void textBox2_KeyPress(System::Object^ sender, System::Windows::Forms::KeyPressEventArgs^ e)
{
textBox_KeyPress(e, textBox2);
}
private: System::Void textBox_TextChanged(System::Windows::Forms::TextBox^ textbox) // Срабатывает при любом изменении текста
{
operationLabel->Text = "";
textBoxResult->Text = "";
ErrorLabel1->Visible = false; // убирает результат, название операции и сообщение об ошибке при изменении текстбокса
int selectionStart = textbox->SelectionStart;
textbox->Text = textbox->Text->Replace(L".", L",");
System::String^ text = textbox->Text;
if (text->IndexOf("-") > 0)
textbox->Text = "-" + textbox->Text->Replace("-", ""); // переносит - вперёд
textbox->SelectionStart = selectionStart;
}
private: double getNumber(System::Windows::Forms::TextBox^ textbox) // возвращает число из текстбокса, изменяет текстбокс, если в нём перед точкой нет чисел
{
ErrorLabel1->Visible = false;
operationLabel->Text = "";
textBoxResult->Text = ""; // вставил сюда очистку полей, чтобы они вызывались при каждом нажатии операции
if (textbox->Text == "")
textbox->Text = "0";
if (textbox->Text->IndexOf(",") == 0 || textbox->Text->IndexOf("-,") != -1)
textbox->Text = textbox->Text->Replace(",", "0,");
return Convert::ToDouble(textbox->Text);
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
textBox_TextChanged(textBox1);
}
private: System::Void textBox2_TextChanged(System::Object^ sender, System::EventArgs^ e)
{
textBox_TextChanged(textBox2);
}
private: System::Void AddButton_Click(System::Object^ sender, System::EventArgs^ e)
{
double num1 = getNumber(textBox1);
double num2 = getNumber(textBox2);
textBoxResult->Text = Convert::ToString(num1 + num2);
operationLabel->Text = "Сложение";
}
private: System::Void subtract_Click(System::Object^ sender, System::EventArgs^ e)
{
double num1 = getNumber(textBox1);
double num2 = getNumber(textBox2);
textBoxResult->Text = Convert::ToString(num1 - num2);
operationLabel->Text = "Вычитание";
}
private: System::Void multiply_Click(System::Object^ sender, System::EventArgs^ e)
{
double num1 = getNumber(textBox1);
double num2 = getNumber(textBox2);
textBoxResult->Text = Convert::ToString(num1 * num2);
operationLabel->Text = "Умножение";
}
private: System::Void divide_Click(System::Object^ sender, System::EventArgs^ e)
{
double num1 = getNumber(textBox1);
double num2 = getNumber(textBox2);
if (num2 == 0)
{
ErrorLabel1->Visible = true;
}
else
{
textBoxResult->Text = Convert::ToString(num1 / num2);
operationLabel->Text = "Деление";
}
}
private: System::Void reset_Click(System::Object^ sender, System::EventArgs^ e)
{
textBox1->Text = "";
textBox2->Text = "";
}
private: System::Void close_Click(System::Object^ sender, System::EventArgs^ e)
{
this->Close();
}
};
}
Соседние файлы в папке Лаба 1 (Калькулятор)