How To Create Multiplication Table In C# Window Application

Introduction

In this step by step tutorial, I will demonstrate how to create and use a multiplication table, using Windows Form and C#. This tutorial covers the topics given below.

  • How to create Windows Form.
  • How to add textbox and list box.
  • How to format a button.
  • How to add a button click event handler.
  • Complete source code.
  • Final output.

Step 1

Create a Windows Form

The Windows Form element represents a C# control. 

  1. // Form 1  
  2.      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);  
  3.             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;  
  4.             this.ClientSize = new System.Drawing.Size(681, 741);  
  5. this.Name = "Form1";  
  6.             this.Text = "Multiplication Table In C# Window Application";  
  7.             this.Load += new System.EventHandler(this.Form1_Load);  
  8.             this.ResumeLayout(false);  
  9.             this.PerformLayout();   

The output looks something as shown below.

C#
Figure 1

The Width and Height attributes of the Windows form element represent the client size of a form. The Content property of the Windows form element sets the text of a Windows form and this.Name attribute represents the name of the control, which is a unique identifier of a control. You may also use the Name property to set the name of a control.

Step 2

Add textbox and list box

Add textbox and list box to expand the tool box and simply drag and drop these tools. You can also write the code for textbox and list box 

  1. // listBox1  
  2.               
  3.             this.listBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));  
  4.             this.listBox1.Cursor = System.Windows.Forms.Cursors.SizeAll;  
  5.             this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  6.             this.listBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));  
  7.             this.listBox1.FormattingEnabled = true;  
  8.             this.listBox1.ItemHeight = 25;  
  9.             this.listBox1.Location = new System.Drawing.Point(123, 103);  
  10.             this.listBox1.Name = "listBox1";  
  11.             this.listBox1.Size = new System.Drawing.Size(236, 604);  
  12.             this.listBox1.TabIndex = 5;  
  13.             this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);   

The content property of list box name attributes this.listbox1.Name, font attributes set this.listBox1.Font and tab index set this.listBox1.TabIndex. Event handling of the list box is this.listBox1.SelectedIndexChanged. 

  1. // textBox2  
  2.               
  3.             this.textBox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));  
  4.             this.textBox2.Font = new System.Drawing.Font("Modern No. 20", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  5.             this.textBox2.Location = new System.Drawing.Point(123, 54);  
  6.             this.textBox2.Multiline = true;  
  7.             this.textBox2.Name = "textBox2";  
  8.             this.textBox2.Size = new System.Drawing.Size(236, 30);  
  9.             this.textBox2.TabIndex = 1;  
  10.             this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;  
  11.             this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);   

The content property of textbox name attributes this.textBox2.Name, font attributes set this.textBox2.Font and tab index sets this.textBox2.TabIndex. If you want use textbox as a multiline set text property, use this.textBox2.Multiline (true) Event handling of the list box this.textBox2.TextChanged

Step 3

Formatting a button

The code snippet sets the button control in the Window form. 

  1. // Multiplication  
  2.             this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  3.             this.button1.Location = new System.Drawing.Point(442, 103);  
  4.             this.button1.Name = "button1";  
  5.             this.button1.Size = new System.Drawing.Size(170, 41);  
  6.             this.button1.TabIndex = 2;  
  7.             this.button1.Text = "Multiplication";  
  8.             this.button1.UseVisualStyleBackColor = true;  
  9.             this.button1.Click += new System.EventHandler(this.button1_Click);  
  10. // Reset  
  11.              
  12.             this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  13.             this.button2.Location = new System.Drawing.Point(442, 200);  
  14.             this.button2.Name = "button2";  
  15.             this.button2.Size = new System.Drawing.Size(170, 41);  
  16.             this.button2.TabIndex = 3;  
  17.             this.button2.Text = "Reset";  
  18.             this.button2.UseVisualStyleBackColor = true;  
  19.             this.button2.Click += new System.EventHandler(this.button2_Click);    
  20. // Close  
  21.             this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));  
  22.             this.button3.Location = new System.Drawing.Point(442, 308);  
  23.             this.button3.Name = "button3";  
  24.             this.button3.Size = new System.Drawing.Size(170, 41);  
  25.             this.button3.TabIndex = 4;  
  26.             this.button3.Text = "Close";  
  27.             this.button3.UseVisualStyleBackColor = true;  
  28.             this.button3.Click += new System.EventHandler(this.button3_Click);   

Not, let’s be a little creative. How about creating a button control with a font formatting, size, font style and tab Index of the button?

Step 4

Adding a Button Click Event Handler

The Click attribute of the button element adds the click event handler. The code given below adds the click event handler for a button. 

  1. this.button1.Click += new System.EventHandler(this.button1_Click);            
  2. this.button2.Click += new System.EventHandler(this.button2_Click);            
  3. this.button3.Click += new System.EventHandler(this.button3_Click);            

Now, whatever code, you write in the click event handler will be executed on the Button click. The code listed in Listing 3 creates a circle on the Button click event handler. 

  1. private void button1_Click(object sender, EventArgs e)  
  2.         {  
  3.             int num=0;  
  4.             num = Convert.ToInt32(textBox2.Text);  
  5.             for(int a=1;a<=10;a++)  
  6.             {  
  7.                 listBox1.Items.Add(num + " * " + a + "\n = " + num * a);  
  8.             }  
  9.   
  10.         }  
  11. private void button2_Click(object sender, EventArgs e)  
  12.         {  
  13.             textBox2.Text = "";  
  14.             textBox2.Focus();  
  15.             listBox1.Items.Clear();  
  16.             this.Hide();  
  17.             Form2 f = new Form2();  
  18.             f.Show();  
  19.         }  
  20. private void button3_Click(object sender, EventArgs e)  
  21.         {  
  22.             Application.Exit();  
  23.         }   

The final Window given below forms design layout output, as shown below.

C#
Figure 2

Step 5

Complete source code

The final complete source code looks, as shown below. 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10.   
  11. namespace MultiplicationTableApps  
  12. {  
  13.     public partial class Form1 : Form  
  14.     {  
  15.           
  16.   
  17.         public Form1()  
  18.         {  
  19.             InitializeComponent();  
  20.         }  
  21.   
  22.         private void button1_Click(object sender, EventArgs e)  
  23.         {  
  24.             int num=0;  
  25.             num = Convert.ToInt32(textBox2.Text);  
  26.             for(int a=1;a<=10;a++)  
  27.             {  
  28.                 listBox1.Items.Add(num + " * " + a + "\n = " + num * a);  
  29.                    //textBox1.Text+= num + " * " + a + "\n = " + num * a;  
  30.             }  
  31.   
  32.         }  
  33.   
  34.         private void Form1_Load(object sender, EventArgs e)  
  35.         {  
  36.             textBox2.Focus();  
  37.               
  38.         }  
  39.   
  40.         private void textBox1_TextChanged(object sender, EventArgs e)  
  41.         {  
  42.   
  43.         }  
  44.   
  45.         private void button2_Click(object sender, EventArgs e)  
  46.         {  
  47.             textBox2.Text = "";  
  48.             textBox2.Focus();  
  49.             listBox1.Items.Clear();  
  50.             //this.Hide();  
  51.             //Form2 f = new Form2();  
  52.             //f.Show();  
  53.         }  
  54.   
  55.         private void button3_Click(object sender, EventArgs e)  
  56.         {  
  57.             Application.Exit();  
  58.         }  
  59.   
  60.         private void listBox1_SelectedIndexChanged(object sender, EventArgs e)  
  61.         {  
  62.   
  63.         }  
  64.   
  65.         private void textBox2_TextChanged(object sender, EventArgs e)  
  66.         {  
  67.   
  68.         }  
  69.   
  70.          
  71.     }  
  72. }   

Step 6

Output

C#
Figure 3

Summary

In this article, I discussed how can we create multiplication table, using Windows form and C#. We also saw how can we format a button by setting its size font style properties. Afterwards, we saw how to use list box and text box. In the end of this article, we saw how to print any number of multiplication table in Windows Form Application using C#. In my next article, I will talk about how to create speech recognition application Window apps, using C#. 

Up Next
    Ebook Download
    View all
    Learn
    View all