Using Radio Buttons

 

This exercise combines labels, panels, radio buttons, and text boxes to create an application. It illustrates how to use radio buttons as regular buttons

Prerequisites:
Panel
Button
Scroll Bars

Using Radio Buttons
  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with Calculate
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	PictureBox  *pctNumbers;
    	PictureBox  *pctOperators;
    	Label		*lblNumber1;
    	TextBox		*txtNumber1;
    	Label		*lblNumber2;
    	TextBox		*txtNumber2;
    	Label		*lblResult;
    	TextBox		*txtResult;
    
    	RadioButton	*rdoAddition;
    	RadioButton	*rdoSubtraction;
    	RadioButton *rdoMultiplication;
    	RadioButton *rdoDivision;
    
    	void AdditionClick(Object *Sender, EventArgs *Args);
    	void SubtractionClick(Object *Sender, EventArgs *Args);
    	void MultiplicationClick(Object *Sender, EventArgs *Args);
    	void DivisionClick(Object *Sender, EventArgs *Args);
    
    	void CreateTheControls();
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = "Using Radio Buttons";
    	this->Size = Drawing::Size(385, 190);
    	this->MinimizeBox = false;
    	this->MaximizeBox = false;
    	this->FormBorderStyle = FormBorderStyle::FixedDialog;
    
    	CreateTheControls();
    }
    
    void SimpleForm::CreateTheControls()
    {
    	pctNumbers = new PictureBox;
    	pctNumbers->BorderStyle = BorderStyle::Fixed3D;
    	pctNumbers->Location = Point(16, 16);
    	pctNumbers->Size = Drawing::Size(200, 125);
    	Controls->Add(pctNumbers);
    
    	lblNumber1 = new Label;
    	lblNumber1->AutoSize = true;
    	lblNumber1->Location = Point(16, 30);
    	lblNumber1->Text = S"Number &1:";
    	this->pctNumbers->Controls->Add(lblNumber1);
    
    	txtNumber1 = new TextBox;
    	txtNumber1->Location = Point(100, 24);
    	txtNumber1->Size = Drawing::Size(84, 16);
    	txtNumber1->Text = "0.00";
    	txtNumber1->TextAlign =  System::Windows::Forms::HorizontalAlignment::Right;
    	this->pctNumbers->Controls->Add(txtNumber1);
    
    	lblNumber2 = new Label;
    	lblNumber2->AutoSize = true;
    	lblNumber2->Location = Point(16, 54);
    	lblNumber2->Text = S"Number &2:";
    	this->pctNumbers->Controls->Add(lblNumber2);
    
    	txtNumber2 = new TextBox;
    	txtNumber2->Location = Point(100, 48);
    	txtNumber2->Size = Drawing::Size(84, 16);
    	txtNumber2->Text = "1.00";
    	txtNumber2->TextAlign =  System::Windows::Forms::HorizontalAlignment::Right;
    	this->pctNumbers->Controls->Add(txtNumber2);
    
    	lblResult = new Label;
    	lblResult->AutoSize = true;
    	lblResult->Location = Point(16, 76);
    	lblResult->Text = S"Result:";
    	this->pctNumbers->Controls->Add(lblResult);
    
    	txtResult = new TextBox;
    	txtResult->Location = Point(100, 72);
    	txtResult->Size = Drawing::Size(84, 16);
    	this->pctNumbers->Controls->Add(txtResult);
    
    	pctOperators = new PictureBox;
    	pctOperators->BorderStyle = BorderStyle::Fixed3D;
    	pctOperators->Location = Point(220, 16);
    	pctOperators->Size = Drawing::Size(140, 125);
    
    	rdoAddition = new RadioButton;
    	rdoAddition->Location = Point(16, 16);
    	rdoAddition->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoAddition->Text = S"&Addition";
    	rdoAddition->add_Click(new EventHandler(this, AdditionClick));
    	pctOperators->Controls->Add(rdoAddition);
    
    	rdoSubtraction = new RadioButton;
    	rdoSubtraction->Location = Point(16, 40);
    	rdoSubtraction->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoSubtraction->Text = S"&Subtraction";
    	rdoSubtraction->add_Click(new EventHandler(this, SubtractionClick));
    	pctOperators->Controls->Add(rdoSubtraction);
    
    	rdoMultiplication = new RadioButton;
    	rdoMultiplication->Location = Point(16, 64);
    	rdoMultiplication->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoMultiplication->Text = S"&Multiplication";
    	rdoMultiplication->add_Click(new EventHandler(this, MultiplicationClick));
    	pctOperators->Controls->Add(rdoMultiplication);
    
    	rdoDivision = new RadioButton;
    	rdoDivision->Location = Point(16, 88);
    	rdoDivision->CheckAlign = Drawing::ContentAlignment::MiddleRight;
    	rdoDivision->Text = S"&Division";
    	rdoDivision->add_Click(new EventHandler(this, DivisionClick));
    	pctOperators->Controls->Add(rdoDivision);
    
    	Controls->Add(pctOperators);
    }
    
    void SimpleForm::AdditionClick(Object *Sender, EventArgs *Args)
    {
    	double Number1, Number2, Result;
    
    	try {
    		Number1 = Convert::ToDouble(txtNumber1->Text);
    		Number2 = Convert::ToDouble(txtNumber2->Text);
    
    		Result = Number1 + Number2;
    		txtResult->Text = Result.ToString();
    	}
    	catch(System::FormatException *fmtE)
    	{
    		MessageBox::Show(fmtE->Message);
    	}
    }
    
    void SimpleForm::SubtractionClick(Object *Sender, EventArgs *Args)
    {
    	double Number1, Number2, Result;
    
    	try {
    		Number1 = Convert::ToDouble(txtNumber1->Text);
    		Number2 = Convert::ToDouble(txtNumber2->Text);
    
    		Result = Number1 - Number2;
    		txtResult->Text = Result.ToString();
    	}
    	catch(System::FormatException *fmtE)
    	{
    		MessageBox::Show(fmtE->Message);
    	}
    }
    
    void SimpleForm::MultiplicationClick(Object *Sender, EventArgs *Args)
    {
    	double Number1, Number2, Result;
    
    	try {
    		Number1 = Convert::ToDouble(txtNumber1->Text);
    		Number2 = Convert::ToDouble(txtNumber2->Text);
    
    		Result = Number1 * Number2;
    		txtResult->Text = Result.ToString();
    	}
    	catch(System::FormatException *fmtE)
    	{
    		MessageBox::Show(fmtE->Message);
    	}
    }
    
    void SimpleForm::DivisionClick(Object *Sender, EventArgs *Args)
    {
    	double Number1, Number2, Result;
    
    	try {
    		Number1 = Convert::ToDouble(txtNumber1->Text);
    		Number2 = Convert::ToDouble(txtNumber2->Text);
    
    		Result = Number1 / Number2;
    		txtResult->Text = Result.ToString();
    	}
    	catch(System::FormatException *fmtE)
    	{
    		MessageBox::Show(fmtE->Message);
    	}
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm * FM = new SimpleForm();
    	Application::Run(FM);
    
    	return 0;
    }
  12. Test the application.

 

 

Home Copyright © 2004-2010 FunctionX, Inc.