Accepting and Displaying Numeric Data (Windows Forms Applications-Microsoft Visual C++ 2008 Express Edition)

Oftentimes, we create programs that needs to ask numbers from the user.
To accept a numeric data in Visual C++ Express Edition, use the following syntax:

Expression= int::Parse (this-> SourceObjectName->Property);

Where;

1. Expression can either be an ObjectName or a variable.
2. SourceObjectName is the name of the control where the value will come from.

int::Parse is a method that converts text or string into an integer. The int::Parse method is required in accepting numeric inputs. If you omit the int::Parse method, the numeric input will be interpreted as string.

To display a numeric data, use the following syntax:

this-> DestinationObjectName->Property=Convert::ToString(Expression);

Where;

1. Expression can either be an ObjectName or variable.
2. SourceObjectName is the name of the control where the value will come from.

Convert::ToString is a method that converts a value inputted in a control to text or string and is required in displaying numeric output to avoid parameter conversion build errors.

To see int::Parse and Convert::ToString methods in action, follow these steps:

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 shown below:



Note: Those texts that appear beside each control are the suggested names for our form controls in this example. 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.

6. Double-Click the control named OkButton, the following event procedure should then come into sight:

7. Double-click the control named OkButton then type the following between the open and close curly brackets:
//Converts the number inputed by the user into a numeric data
//and assign it to an integer variable named intusernum
int intusernum=int::Parse(this->StringtextBox->Text);
//Converts the numeric data into a string data and display it in a
//control named EnteredLabel
this->EnteredLabel->Text=Convert::ToString(intusernum);

Your code should now look like this:
private: System::Void OkButton_Click(System::Object^  sender, System::EventArgs^  e) {
int intusernum=int::Parse(this->StringtextBox->Text);
this->EnteredLabel->Text=Convert::ToString(intusernum);   
} 
8. Double-click the control named ExitButton then type this->Close();. This will terminate the execution of the current form. Your code will then look like this:
private: System::Void ExitButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->Close();
}   
9. Press F5 to execute your application.

No comments:

Post a Comment

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