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.
-
- this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.ClientSize = new System.Drawing.Size(681, 741);
- this.Name = "Form1";
- this.Text = "Multiplication Table In C# Window Application";
- this.Load += new System.EventHandler(this.Form1_Load);
- this.ResumeLayout(false);
- this.PerformLayout();
The output looks something as shown below.
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
-
-
- this.listBox1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
- this.listBox1.Cursor = System.Windows.Forms.Cursors.SizeAll;
- this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.listBox1.ForeColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(192)))), ((int)(((byte)(0)))));
- this.listBox1.FormattingEnabled = true;
- this.listBox1.ItemHeight = 25;
- this.listBox1.Location = new System.Drawing.Point(123, 103);
- this.listBox1.Name = "listBox1";
- this.listBox1.Size = new System.Drawing.Size(236, 604);
- this.listBox1.TabIndex = 5;
- 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.
-
-
- this.textBox2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(192)))), ((int)(((byte)(255)))));
- this.textBox2.Font = new System.Drawing.Font("Modern No. 20", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.textBox2.Location = new System.Drawing.Point(123, 54);
- this.textBox2.Multiline = true;
- this.textBox2.Name = "textBox2";
- this.textBox2.Size = new System.Drawing.Size(236, 30);
- this.textBox2.TabIndex = 1;
- this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
- 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.
-
- this.button1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.button1.Location = new System.Drawing.Point(442, 103);
- this.button1.Name = "button1";
- this.button1.Size = new System.Drawing.Size(170, 41);
- this.button1.TabIndex = 2;
- this.button1.Text = "Multiplication";
- this.button1.UseVisualStyleBackColor = true;
- this.button1.Click += new System.EventHandler(this.button1_Click);
-
-
- this.button2.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.button2.Location = new System.Drawing.Point(442, 200);
- this.button2.Name = "button2";
- this.button2.Size = new System.Drawing.Size(170, 41);
- this.button2.TabIndex = 3;
- this.button2.Text = "Reset";
- this.button2.UseVisualStyleBackColor = true;
- this.button2.Click += new System.EventHandler(this.button2_Click);
-
- this.button3.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.35F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.button3.Location = new System.Drawing.Point(442, 308);
- this.button3.Name = "button3";
- this.button3.Size = new System.Drawing.Size(170, 41);
- this.button3.TabIndex = 4;
- this.button3.Text = "Close";
- this.button3.UseVisualStyleBackColor = true;
- 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.
- this.button1.Click += new System.EventHandler(this.button1_Click);
- this.button2.Click += new System.EventHandler(this.button2_Click);
- 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.
- private void button1_Click(object sender, EventArgs e)
- {
- int num=0;
- num = Convert.ToInt32(textBox2.Text);
- for(int a=1;a<=10;a++)
- {
- listBox1.Items.Add(num + " * " + a + "\n = " + num * a);
- }
-
- }
- private void button2_Click(object sender, EventArgs e)
- {
- textBox2.Text = "";
- textBox2.Focus();
- listBox1.Items.Clear();
- this.Hide();
- Form2 f = new Form2();
- f.Show();
- }
- private void button3_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
The final Window given below forms design layout output, as shown below.
Figure 2
Step 5
Complete source code
The final complete source code looks, as shown below.
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
-
- namespace MultiplicationTableApps
- {
- public partial class Form1 : Form
- {
-
-
- public Form1()
- {
- InitializeComponent();
- }
-
- private void button1_Click(object sender, EventArgs e)
- {
- int num=0;
- num = Convert.ToInt32(textBox2.Text);
- for(int a=1;a<=10;a++)
- {
- listBox1.Items.Add(num + " * " + a + "\n = " + num * a);
-
- }
-
- }
-
- private void Form1_Load(object sender, EventArgs e)
- {
- textBox2.Focus();
-
- }
-
- private void textBox1_TextChanged(object sender, EventArgs e)
- {
-
- }
-
- private void button2_Click(object sender, EventArgs e)
- {
- textBox2.Text = "";
- textBox2.Focus();
- listBox1.Items.Clear();
-
-
-
- }
-
- private void button3_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
-
- private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
- {
-
- }
-
- private void textBox2_TextChanged(object sender, EventArgs e)
- {
-
- }
-
-
- }
- }
Step 6
Output
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#.