.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

elementary addition

Posted By:papsantony       Posted Date: August 13, 2010    Points: 10    Category: Visual Studio    URL: http://www.dotnetspark.com  

elementary addition
 

Practical Learning: Introducing Text Boxes


  1. Create a Window Forms Application named ElementaryAddition2
  2. Design the form as follows:
     
    Elementary Addition
    Control Text Name TextAlign Font Additional Properties
    Label 00 lblOperand1 Center Name: Tahoma
    Size:   48
    Bold:   True
    AutoSize: True
    ForeColor: Blue
    Label +   Center Name: Arial
    Size:   50
    Bold:   True
    AutoSize: True
    ForeColor: Maroon
    Label 00 lblOperand2 Center Name: Tahoma
    Size:   48
    Bold:   True
    AutoSize: True
    ForeColor: Blue
    Label =   Center Name: Arial
    Size:   50
    Bold:   True
    AutoSize: True
    ForeColor: Green
    TextBox 000 txtResult Center Name: Tahoma
    Size:   48
    Bold:   True
     
    Label New Operation lblNewOperation Center Name: Tahoma, Size: 28 Bold: True AutoSize: True
    BorderStyle: Fixed3D
    ForeColor: White
    BackColor:Maroon 
    Label Check lblCheckAnswer Center Name: Tahoma, Size: 28 Bold: True AutoSize: True
    BorderStyle: Fixed3D
    ForeColor: White
    BackColor:Maroon 
    Label Quit lblQuit Center Name: Tahoma, Size: 28 Bold: True AutoSize: True
    BorderStyle: FixedSingle
  3. Double-click the New Operation label and implement its event as follows:
     
    private void lblNewOperation_Click(object sender, EventArgs e)
    {
    int operand1;
    int operand2;

    Random rnd = new Random();

    operand1 = rnd.Next(99);
    operand2 = rnd.Next(99);
    int result = operand1 + operand2;

    lblOperand1.Text = operand1.ToString();
    lblOperand2.Text = operand2.ToString();
    txtResult.Text = "";
    txtResult.Focus();
    }
  4. Return to the form and double-click the Check label
  5. Implement its event as follows:
     
    private void lblCheckAnswer_Click(object sender, EventArgs e)
    {
    int Operand1 = 0;
    int Operand2 = 0;
    int Result = 0;

    try
    {
    Operand1 = int.Parse(lblOperand1.Text);
    }
    catch (FormatException)
    {
    MessageBox.Show("Invalid Value");
    }

    try
    {
    Operand2 = int.Parse(lblOperand2.Text);
    }
    catch (FormatException)
    {
    MessageBox.Show("Invalid Value");
    }

    try
    {
    Result = int.Parse(txtResult.Text);
    }
    catch (FormatException)
    {
    MessageBox.Show("Invalid Answer");
    }

    if (Result == (Operand1 + Operand2))
    MessageBox.Show("WOW - Good Answer");
    else
    MessageBox.Show("PSSST - Wrong Answer");

    lblNewOperation_Click(sender, e);
    }
  6. Return to the form and double-click Quit
  7. Implement its event as follows:
     
    private void lblQuit_Click(object sender, EventArgs e)
    {
    Close();
    }
  8. Execute the application and test it
  9. Click the Close button to close the form and return to your programming environment


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend