Accepting Decimal or Floating Point Inputs(Windows Forms Applications-Microsoft Visual C++ 2008 Express Edition)

In general, every data inputted in a textbox are treated as string. For instance if you enter 5, Visual C++ 2008 will treat it as “5” which is a problem if you intend to use the input in calculations. If you have read my post about Accepting Numeric Data, we have used int::Parse function to treat the input as a numeric data. Unfortunately int::Parse works only on whole number inputs and returns a build errors on floating point input. To prepare your variable or control in accepting floating point input use the float::Parse function.

Syntax:
Destination=float::Parse(Source);

Wherein;
1. Source can be a control or variable that where you will be getting your floating point data.
2. Destination is a control or variable will you will be displaying or storing your floating point data.

The following steps demonstrate float::Parse at work:

1. Start MSVC++ Express 2008 by clicking on the Start button, All Programs then clicking on the Microsoft Visual C++ Express Edition Start menu option.

2. Click File>New>Project. The new project dialog box will then come into view.

3. Select Windows Forms Application from the Visual Studio Installed templates, type the project name>Click Ok.
After clicking the Ok button, an IDE suited for windows forms application will then appear containing a form, a toolbox window, and a properties window.

4. Create a user interface similar to the one below:



Note: Those texts that appear beside each control are the suggested names for our form controls in this example. If you noticed I am using .Net naming convention in naming controls because it is simpler to use than Leszynski naming convention. To change the value of the name property of each control, just click each individual control then locate the name property in the properties window then assign those descriptive names in our illustration correspondingly.

5. Double-click your form then locate the following line:

#pragma endregion

6. Declare the following variables below it:

float fltcurrpricex,fltcurrpricey, fltdiscountx,fltdiscounty, fltnewpricex,fltnewpricey;

Declaring these variables below the #pragma endregion line allows our variables to be available publicly and to be used by other procedures.

Your code should now appear just like this:

#pragma endregion  
float fltcurrpricex,fltcurrpricey, fltdiscountx,fltdiscounty, fltnewpricex,fltnewpricey;  

7. Double-click the control named ComputeButton,type the following:

fltcurrpricex=float::Parse(this->XPricetextBox->Text);
fltcurrpricey=float::Parse(this->YPricetextBox->Text);
fltdiscountx=float::Parse(this->DiscountXtextBox->Text);
fltdiscounty=float::Parse(this->DiscountYtextBox->Text);
fltnewpricex= fltcurrpricex - (fltcurrpricex*fltdiscountx);
fltnewpricey= fltcurrpricey - (fltcurrpricey*fltdiscounty);
this->XPricelabel->Text=Convert::ToString(fltnewpricex);
this->YPricelabel->Text=Convert::ToString(fltnewpricey);

8. Your could willnow look like this:

private: System::Void ComputeButton_Click(System::Object^  sender, System::EventArgs^  e) 
{
fltcurrpricex=float::Parse(this->XPricetextBox->Text);
fltcurrpricey=float::Parse(this->YPricetextBox->Text);
fltdiscountx=float::Parse(this->DiscountXtextBox->Text);
fltdiscounty=float::Parse(this->DiscountYtextBox->Text);
fltnewpricex= fltcurrpricex - (fltcurrpricex*fltdiscountx);
fltnewpricey= fltcurrpricey - (fltcurrpricey*fltdiscounty);
this->XPricelabel->Text=Convert::ToString(fltnewpricex);
this->YPricelabel->Text=Convert::ToString(fltnewpricey);
}

9.Press F5 to test your appliaction.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.