Monday, August 15, 2011

Windows Form:Tip Calculator



Here is all the auto generated code for the window:


#pragma once

namespace TipCalculator {

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 for Form1
///

public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}

protected:
///
/// Clean up any resources being used.
///

~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Panel^ panel1;
protected:
private: System::Windows::Forms::RadioButton^ rdoTwentyPercent;
private: System::Windows::Forms::RadioButton^ rdoFifteenPercent;
private: System::Windows::Forms::RadioButton^ rdoTenPercent;
private: System::Windows::Forms::TextBox^ txtAmount;
private: System::Windows::Forms::Label^ label1;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Label^ label2;
private: System::Windows::Forms::Label^ label3;
private: System::Windows::Forms::Label^ label4;
private: System::Windows::Forms::Label^ lblTax;
private: System::Windows::Forms::Label^ lblTip;
private: System::Windows::Forms::Label^ lblTotal;

private:
///
/// Required designer variable.
///

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///

void InitializeComponent(void)
{
this->panel1 = (gcnew System::Windows::Forms::Panel());
this->rdoTenPercent = (gcnew System::Windows::Forms::RadioButton());
this->rdoFifteenPercent = (gcnew System::Windows::Forms::RadioButton());
this->rdoTwentyPercent = (gcnew System::Windows::Forms::RadioButton());
this->txtAmount = (gcnew System::Windows::Forms::TextBox());
this->label1 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->label4 = (gcnew System::Windows::Forms::Label());
this->lblTax = (gcnew System::Windows::Forms::Label());
this->lblTip = (gcnew System::Windows::Forms::Label());
this->lblTotal = (gcnew System::Windows::Forms::Label());
this->panel1->SuspendLayout();
this->SuspendLayout();
//
// panel1
//
this->panel1->Controls->Add(this->rdoTwentyPercent);
this->panel1->Controls->Add(this->rdoFifteenPercent);
this->panel1->Controls->Add(this->rdoTenPercent);
this->panel1->Location = System::Drawing::Point(32, 56);
this->panel1->Name = L"panel1";
this->panel1->Size = System::Drawing::Size(280, 170);
this->panel1->TabIndex = 0;
//
// rdoTenPercent
//
this->rdoTenPercent->AutoSize = true;
this->rdoTenPercent->Location = System::Drawing::Point(58, 22);
this->rdoTenPercent->Name = L"rdoTenPercent";
this->rdoTenPercent->Size = System::Drawing::Size(77, 17);
this->rdoTenPercent->TabIndex = 0;
this->rdoTenPercent->TabStop = true;
this->rdoTenPercent->Text = L"10 Percent";
this->rdoTenPercent->UseVisualStyleBackColor = true;
//
// rdoFifteenPercent
//
this->rdoFifteenPercent->AutoSize = true;
this->rdoFifteenPercent->Location = System::Drawing::Point(58, 60);
this->rdoFifteenPercent->Name = L"rdoFifteenPercent";
this->rdoFifteenPercent->Size = System::Drawing::Size(77, 17);
this->rdoFifteenPercent->TabIndex = 1;
this->rdoFifteenPercent->TabStop = true;
this->rdoFifteenPercent->Text = L"15 Percent";
this->rdoFifteenPercent->UseVisualStyleBackColor = true;
//
// rdoTwentyPercent
//
this->rdoTwentyPercent->AutoSize = true;
this->rdoTwentyPercent->Location = System::Drawing::Point(58, 99);
this->rdoTwentyPercent->Name = L"rdoTwentyPercent";
this->rdoTwentyPercent->Size = System::Drawing::Size(96, 17);
this->rdoTwentyPercent->TabIndex = 2;
this->rdoTwentyPercent->TabStop = true;
this->rdoTwentyPercent->Text = L"twenty Percent";
this->rdoTwentyPercent->UseVisualStyleBackColor = true;
//
// txtAmount
//
this->txtAmount->Location = System::Drawing::Point(157, 257);
this->txtAmount->Name = L"txtAmount";
this->txtAmount->Size = System::Drawing::Size(100, 20);
this->txtAmount->TabIndex = 1;
//
// label1
//
this->label1->AutoSize = true;
this->label1->Location = System::Drawing::Point(32, 260);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(126, 13);
this->label1->TabIndex = 2;
this->label1->Text = L"Enter Amount Before Tax";
//
// button1
//
this->button1->Location = System::Drawing::Point(35, 305);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 3;
this->button1->Text = L"Calculate";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(334, 263);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(28, 13);
this->label2->TabIndex = 4;
this->label2->Text = L"Tax:";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(337, 305);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(25, 13);
this->label3->TabIndex = 5;
this->label3->Text = L"Tip:";
//
// label4
//
this->label4->AutoSize = true;
this->label4->Location = System::Drawing::Point(337, 338);
this->label4->Name = L"label4";
this->label4->Size = System::Drawing::Size(34, 13);
this->label4->TabIndex = 6;
this->label4->Text = L"Total:";
//
// lblTax
//
this->lblTax->AutoSize = true;
this->lblTax->Location = System::Drawing::Point(437, 263);
this->lblTax->Name = L"lblTax";
this->lblTax->Size = System::Drawing::Size(0, 13);
this->lblTax->TabIndex = 7;
//
// lblTip
//
this->lblTip->AutoSize = true;
this->lblTip->Location = System::Drawing::Point(440, 304);
this->lblTip->Name = L"lblTip";
this->lblTip->Size = System::Drawing::Size(0, 13);
this->lblTip->TabIndex = 8;
//
// lblTotal
//
this->lblTotal->AutoSize = true;
this->lblTotal->Location = System::Drawing::Point(440, 338);
this->lblTotal->Name = L"lblTotal";
this->lblTotal->Size = System::Drawing::Size(0, 13);
this->lblTotal->TabIndex = 9;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::FromArgb(static_cast(static_cast(192)), static_cast(static_cast(192)),
static_cast(static_cast(255)));
this->ClientSize = System::Drawing::Size(612, 446);
this->Controls->Add(this->lblTotal);
this->Controls->Add(this->lblTip);
this->Controls->Add(this->lblTax);
this->Controls->Add(this->label4);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->button1);
this->Controls->Add(this->label1);
this->Controls->Add(this->txtAmount);
this->Controls->Add(this->panel1);
this->Name = L"Form1";
this->Text = L"Tip Calculator";
this->panel1->ResumeLayout(false);
this->panel1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion


And here, much shorter, is the piece of code we wrote in the button event handler:


private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {

double TAX = .095;
double mealAmount =double::Parse(txtAmount ->Text);
double taxAmount=(mealAmount * TAX);
lblTax->Text=taxAmount.ToString("c");
double percent=0;
if (rdoTenPercent->Checked)
{
percent=.1;
}
if (rdoFifteenPercent->Checked)
{
percent=.15;
}
if(rdoTwentyPercent->Checked)
{
percent=.2;
}

double tipAmount=(mealAmount * percent);
lblTip->Text=tipAmount.ToString("c");

lblTotal->Text=(mealAmount + taxAmount + tipAmount).ToString("c");
}

No comments:

Post a Comment