// SumApplication.cs Copyright (c) Kari Laitinen // http://www.naturalprogramming.com // 2008-02-25 File created. // 2022-12-20 Tested in Developer Command Prompt for VS 2022. /* With this program you can calculate the sum of two integers. This program demonstrates the following things: - the use of Panel, TextBox, and Label objects In Windows terminology, the various GUI components (e.g. TextBox, Label, Panel) are called controls. More notes at the end of this file. */ using System ; using System.Windows.Forms ; using System.Drawing ; class SumForm : Form { const int WINDOW_WIDTH = 600 ; const int WINDOW_HEIGHT = 435 ; TextBox first_text_box, second_text_box ; Label result_label ; public SumForm() { Text = "CALCULATE THE SUM OF TWO INTEGERS" ; Size = new Size( WINDOW_WIDTH, WINDOW_HEIGHT ) ; // First we specify a Panel into which we will later attach // Label and TextBox objects. Panel calculation_panel = new Panel() ; // The following statement specifies that the position // of the panel is ( 100, 50 ), and its width is 300 pixels // and its height is 150 pixels. calculation_panel.Bounds = new Rectangle( 100, 50, 300, 150 ) ; // Next, we'll create Label and TextBox objects. // The Bounds property of these objects will be set so // that they fit nicely into the "calculation panel" // that was created earlier. Label first_integer_label = new Label() ; first_integer_label.Text = "First integer:" ; first_integer_label.Bounds = new Rectangle( 0, 0, 150, 50 ) ; Label second_integer_label = new Label() ; second_integer_label.Text = "Second integer:" ; second_integer_label.Bounds = new Rectangle( 0, 50, 150, 50 ) ; Label calculated_sum_label = new Label() ; calculated_sum_label.Text = "Calculated sum:" ; calculated_sum_label.Bounds = new Rectangle( 0, 100, 150, 50 ) ; Font font_for_numbers = new Font( FontFamily.GenericMonospace, 14, FontStyle.Bold ) ; first_text_box = new TextBox() ; first_text_box.TextAlign = HorizontalAlignment.Right ; first_text_box.Font = font_for_numbers ; first_text_box.Bounds = new Rectangle( 150, 0, 150, 50 ) ; first_text_box.TextChanged += new EventHandler( text_box_text_changed ) ; second_text_box = new TextBox() ; second_text_box.TextAlign = HorizontalAlignment.Right ; second_text_box.Font = font_for_numbers ; second_text_box.Bounds = new Rectangle( 150, 50, 150, 50 ) ; second_text_box.TextChanged += new EventHandler( text_box_text_changed ) ; result_label = new Label() ; result_label.TextAlign = ContentAlignment.TopRight ; result_label.Font = font_for_numbers ; result_label.Bounds = new Rectangle( 150, 100, 150, 50 ) ; result_label.Text = "0" ; // Next, we'll add the GUI components (or controls) to the panel calculation_panel.Controls.Add( first_integer_label ) ; calculation_panel.Controls.Add( second_integer_label ) ; calculation_panel.Controls.Add( calculated_sum_label ) ; calculation_panel.Controls.Add( first_text_box ) ; calculation_panel.Controls.Add( second_text_box ) ; calculation_panel.Controls.Add( result_label ) ; Controls.Add( calculation_panel ) ; // Add the panel to the form } private void text_box_text_changed( object sender, EventArgs event_data ) { String first_text = first_text_box.Text ; String second_text = second_text_box.Text ; // The following two if constructs ensure that empty text boxes // are treated as if they contained a zero. if ( first_text.Length == 0 ) { first_text = "0" ; } if ( second_text.Length == 0 ) { second_text = "0" ; } try { int first_integer = Convert.ToInt32( first_text ) ; int second_integer = Convert.ToInt32( second_text ) ; int sum_of_two_integers = first_integer + second_integer ; result_label.Text = "" + sum_of_two_integers ; } catch ( Exception ) { result_label.Text = "INPUT ERROR!" ; } } } public class SumApplication { static void Main() { Application.Run( new SumForm() ) ; } } /* POSSIBLE EXERCISE: Modify the program so that you can write negative numbers to the text boxes. If you now enter a minus sign, the program says "INPUT ERROR!". (You can, though, currently enter negative numbers if you first type a positive number, and then add the minus sign.) */