/*__________________________________________
* O V E R L O A D E D M E T H O D S
* C++ 2012
* Ron Kessler
* Created 1/28/2014
* __________________________________________
*/
#pragma once
namespace My5OverloadedMethods {
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;
}
}
internal: System::Windows::Forms::RadioButton^ optConcat;
protected:
internal: System::Windows::Forms::GroupBox^ GroupBox1;
internal: System::Windows::Forms::RadioButton^ optAdd;
internal: System::Windows::Forms::Button^ btnEnd;
internal: System::Windows::Forms::Button^ btnClear;
internal: System::Windows::Forms::Button^ btnCompute;
internal: System::Windows::Forms::TextBox^ txtAnswer;
internal: System::Windows::Forms::TextBox^ txtSecondValue;
internal: System::Windows::Forms::TextBox^ txtFirstValue;
internal: System::Windows::Forms::Label^ Label3;
internal: System::Windows::Forms::Label^ Label2;
internal: System::Windows::Forms::Label^ Label1;
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->optConcat = (gcnew System::Windows::Forms::RadioButton());
this->GroupBox1 = (gcnew System::Windows::Forms::GroupBox());
this->optAdd = (gcnew System::Windows::Forms::RadioButton());
this->btnEnd = (gcnew System::Windows::Forms::Button());
this->btnClear = (gcnew System::Windows::Forms::Button());
this->btnCompute = (gcnew System::Windows::Forms::Button());
this->txtAnswer = (gcnew System::Windows::Forms::TextBox());
this->txtSecondValue = (gcnew System::Windows::Forms::TextBox());
this->txtFirstValue = (gcnew System::Windows::Forms::TextBox());
this->Label3 = (gcnew System::Windows::Forms::Label());
this->Label2 = (gcnew System::Windows::Forms::Label());
this->Label1 = (gcnew System::Windows::Forms::Label());
this->GroupBox1->SuspendLayout();
this->SuspendLayout();
//
// optConcat
//
this->optConcat->AutoSize = true;
this->optConcat->Location = System::Drawing::Point(7, 43);
this->optConcat->Name = L"optConcat";
this->optConcat->Size = System::Drawing::Size(86, 17);
this->optConcat->TabIndex = 1;
this->optConcat->TabStop = true;
this->optConcat->Text = L"Concatenate";
this->optConcat->UseVisualStyleBackColor = true;
//
// GroupBox1
//
this->GroupBox1->Controls->Add(this->optConcat);
this->GroupBox1->Controls->Add(this->optAdd);
this->GroupBox1->ForeColor = System::Drawing::Color::FromArgb(static_cast(static_cast(0)), static_cast(static_cast(0)),
static_cast(static_cast(192)));
this->GroupBox1->Location = System::Drawing::Point(343, 73);
this->GroupBox1->Name = L"GroupBox1";
this->GroupBox1->Size = System::Drawing::Size(98, 80);
this->GroupBox1->TabIndex = 40;
this->GroupBox1->TabStop = false;
this->GroupBox1->Text = L"Procedure";
//
// optAdd
//
this->optAdd->AutoSize = true;
this->optAdd->Location = System::Drawing::Point(6, 19);
this->optAdd->Name = L"optAdd";
this->optAdd->Size = System::Drawing::Size(44, 17);
this->optAdd->TabIndex = 0;
this->optAdd->TabStop = true;
this->optAdd->Text = L"Add";
this->optAdd->UseVisualStyleBackColor = true;
//
// btnEnd
//
this->btnEnd->Location = System::Drawing::Point(467, 129);
this->btnEnd->Name = L"btnEnd";
this->btnEnd->Size = System::Drawing::Size(75, 23);
this->btnEnd->TabIndex = 39;
this->btnEnd->Text = L"End";
this->btnEnd->UseVisualStyleBackColor = true;
this->btnEnd->Click += gcnew System::EventHandler(this, &Form1::btnEnd_Click);
//
// btnClear
//
this->btnClear->Location = System::Drawing::Point(467, 101);
this->btnClear->Name = L"btnClear";
this->btnClear->Size = System::Drawing::Size(75, 23);
this->btnClear->TabIndex = 38;
this->btnClear->Text = L"Clear";
this->btnClear->UseVisualStyleBackColor = true;
this->btnClear->Click += gcnew System::EventHandler(this, &Form1::btnClear_Click);
//
// btnCompute
//
this->btnCompute->Location = System::Drawing::Point(467, 73);
this->btnCompute->Name = L"btnCompute";
this->btnCompute->Size = System::Drawing::Size(75, 23);
this->btnCompute->TabIndex = 37;
this->btnCompute->Text = L"OK";
this->btnCompute->UseVisualStyleBackColor = true;
this->btnCompute->Click += gcnew System::EventHandler(this, &Form1::btnCompute_Click);
//
// txtAnswer
//
this->txtAnswer->Location = System::Drawing::Point(220, 146);
this->txtAnswer->Name = L"txtAnswer";
this->txtAnswer->ReadOnly = true;
this->txtAnswer->Size = System::Drawing::Size(100, 20);
this->txtAnswer->TabIndex = 36;
this->txtAnswer->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
//
// txtSecondValue
//
this->txtSecondValue->Location = System::Drawing::Point(220, 110);
this->txtSecondValue->Name = L"txtSecondValue";
this->txtSecondValue->Size = System::Drawing::Size(100, 20);
this->txtSecondValue->TabIndex = 35;
this->txtSecondValue->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
//
// txtFirstValue
//
this->txtFirstValue->Location = System::Drawing::Point(220, 73);
this->txtFirstValue->Name = L"txtFirstValue";
this->txtFirstValue->Size = System::Drawing::Size(100, 20);
this->txtFirstValue->TabIndex = 34;
this->txtFirstValue->TextAlign = System::Windows::Forms::HorizontalAlignment::Right;
//
// Label3
//
this->Label3->Location = System::Drawing::Point(109, 146);
this->Label3->Name = L"Label3";
this->Label3->Size = System::Drawing::Size(74, 19);
this->Label3->TabIndex = 33;
this->Label3->Text = L"Answer";
//
// Label2
//
this->Label2->Location = System::Drawing::Point(109, 110);
this->Label2->Name = L"Label2";
this->Label2->Size = System::Drawing::Size(74, 19);
this->Label2->TabIndex = 32;
this->Label2->Text = L"Second Item";
//
// Label1
//
this->Label1->Location = System::Drawing::Point(109, 73);
this->Label1->Name = L"Label1";
this->Label1->Size = System::Drawing::Size(74, 19);
this->Label1->TabIndex = 31;
this->Label1->Text = L"First Item";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(651, 239);
this->Controls->Add(this->GroupBox1);
this->Controls->Add(this->btnEnd);
this->Controls->Add(this->btnClear);
this->Controls->Add(this->btnCompute);
this->Controls->Add(this->txtAnswer);
this->Controls->Add(this->txtSecondValue);
this->Controls->Add(this->txtFirstValue);
this->Controls->Add(this->Label3);
this->Controls->Add(this->Label2);
this->Controls->Add(this->Label1);
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::CenterScreen;
this->Text = L"Using Overloaded Methods";
this->GroupBox1->ResumeLayout(false);
this->GroupBox1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
//*******START OF MY CODE**********
static String^ MSG_TITLE = "My Great Demo";
private: System::Void btnCompute_Click(System::Object^ sender, System::EventArgs^ e)
{
if (optAdd->Checked)
WhichFunction('A');
else if (optConcat->Checked)
WhichFunction('C');
else
WhichFunction('N');
}
private: Void WhichFunction(char operation)
{
String^ firstTextboxValue = txtFirstValue->Text->Trim();
String^ secondTextboxValue = txtSecondValue->Text->Trim();
switch (operation)
{
case 'A':
if (IsNumeric(firstTextboxValue) && IsNumeric(secondTextboxValue))
{
int result = getResult(Convert::ToInt32(firstTextboxValue),Convert::ToInt32(secondTextboxValue));
txtAnswer->Text = result.ToString();
}
else
{
MessageBox::Show("Be sure to enter numbers", MSG_TITLE);
}
break;
case 'C':
if (IsAlphaChar(firstTextboxValue) && IsAlphaChar(secondTextboxValue))
{
String^ myResult = getResult(firstTextboxValue, secondTextboxValue);
txtAnswer->Text = myResult;
}
else
MessageBox::Show("Be sure to enter letters for text.",MSG_TITLE);
break;
case 'N':
MessageBox::Show("Be sure to select Add or Concatenate.", MSG_TITLE);
break;
}
}
#pragma region "Helper Methods"
private: bool IsAlphaChar(String^ myString)
{
for each (char ch in myString)
{
if ((ch >= 'A' && ch <= 'Z') || (ch >='a' && ch <='z')) //::Is ch a Letter?
return true;
else
{
ClearForm();
return false;
}
}
}
private: bool IsNumeric(String^ myNumber)
{
int result;
if (int::TryParse(myNumber, result))
return true;
else
{
ClearForm();
return false;
}
}
#pragma endregion
#pragma region "Methods to find result"
private: int getResult(int first, int second)
{
return first + second;
}
private: String^ getResult(String^ First, String^ Last)
{
return First + " " + Last;
}
#pragma endregion
#pragma region "Clear Form"
private: System::Void btnClear_Click(System::Object^ sender, System::EventArgs^ e)
{
ClearForm();
}
private: Void ClearForm()
{
for each (Control^ myControl in this->Controls) //cycle through all controls. Radio buttons are inside a group box
{
if (myControl->GetType() == TextBox::typeid)
myControl->Text = "";
else if (myControl->HasChildren) //this will find the controls inside the group box
{
for each (Control^ nestedControl in myControl->Controls)
if (nestedControl->GetType() ==System::Windows::Forms::RadioButton::typeid)
{
RadioButton^ myRadioButton = safe_cast (nestedControl);
myRadioButton->Checked = false;
}
}
txtFirstValue->Focus();
}
}
private: System::Void btnEnd_Click(System::Object^ sender, System::EventArgs^ e)
{
this->Close();
}
};
}