C# - Simple Conversion Application
Submitted by razormist on Sunday, April 8, 2018 - 22:41.
In this tutorial we will create a Simple Conversion Application using C#. This application is consist of different functionalities that can be used at some point. C# is a general-purpose, object-oriented programming language. C# automatically manages inaccessible object memory using a garbage collector, which eliminates developer concerns and memory leaks. It has a designed for improving productivity in the development of Web applications. It has a friendly environment for all new developers. So let's do the coding.
or also you create the layout by dragging the designating tools to the forms.
Try to run the application and see if it works.
There you go we successfully create a Simple Conversion Application using C#. I hope that this tutorial help you understand on how to develop an application using C#. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!
Getting Started
First you will have to download & install the Visual Studio. Visual Studios is an open source development feel free to create any application that you want. Here's the link for the Visual Studio https://www.visualstudio.com/. Application Design We will now create the design for the application, first locate the designer file called form1.Designer.cs, this is the default name when you create a new windows form. Then write these codes inside your designer file.- namespace Simple_Conversion_Application
- {
- partial class Form1
- {
- /// <summary>
- /// Required designer variable.
- /// </summary>
- private System.ComponentModel.IContainer components = null;
- /// <summary>
- /// Clean up any resources being used.
- /// </summary>
- /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
- protected override void Dispose(bool disposing)
- {
- if (disposing && (components != null))
- {
- components.Dispose();
- }
- base.Dispose(disposing);
- }
- #region Windows Form Designer generated code
- /// <summary>
- /// Required method for Designer support - do not modify
- /// the contents of this method with the code editor.
- /// </summary>
- private void InitializeComponent()
- {
- this.groupBox1.SuspendLayout();
- this.groupBox2.SuspendLayout();
- this.groupBox3.SuspendLayout();
- this.SuspendLayout();
- //
- // groupBox1
- //
- this.groupBox1.Controls.Add(this.btn_nclear);
- this.groupBox1.Controls.Add(this.btn_nconvert);
- this.groupBox1.Controls.Add(this.txt_octal);
- this.groupBox1.Controls.Add(this.txt_hexadecimal);
- this.groupBox1.Controls.Add(this.txt_binary);
- this.groupBox1.Controls.Add(this.label3);
- this.groupBox1.Controls.Add(this.label4);
- this.groupBox1.Controls.Add(this.label2);
- this.groupBox1.Controls.Add(this.txt_number);
- this.groupBox1.Controls.Add(this.label1);
- this.groupBox1.Font = new System.Drawing.Font("Arial Narrow", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.groupBox1.Name = "groupBox1";
- this.groupBox1.TabIndex = 0;
- this.groupBox1.TabStop = false;
- this.groupBox1.Text = "Number Conversion";
- //
- // btn_nclear
- //
- this.btn_nclear.Name = "btn_nclear";
- this.btn_nclear.TabIndex = 10;
- this.btn_nclear.Text = "CLEAR";
- this.btn_nclear.UseVisualStyleBackColor = true;
- //
- // btn_nconvert
- //
- this.btn_nconvert.Name = "btn_nconvert";
- this.btn_nconvert.TabIndex = 9;
- this.btn_nconvert.Text = "CONVERT";
- this.btn_nconvert.UseVisualStyleBackColor = true;
- //
- // txt_octal
- //
- this.txt_octal.Enabled = false;
- this.txt_octal.Multiline = true;
- this.txt_octal.Name = "txt_octal";
- this.txt_octal.TabIndex = 8;
- //
- // txt_hexadecimal
- //
- this.txt_hexadecimal.Enabled = false;
- this.txt_hexadecimal.Multiline = true;
- this.txt_hexadecimal.Name = "txt_hexadecimal";
- this.txt_hexadecimal.TabIndex = 7;
- this.txt_hexadecimal.Text = " ";
- //
- // txt_binary
- //
- this.txt_binary.Enabled = false;
- this.txt_binary.Multiline = true;
- this.txt_binary.Name = "txt_binary";
- this.txt_binary.TabIndex = 6;
- //
- // label3
- //
- this.label3.AutoSize = true;
- this.label3.Name = "label3";
- this.label3.TabIndex = 5;
- this.label3.Text = "Octal";
- //
- // label4
- //
- this.label4.AutoSize = true;
- this.label4.Name = "label4";
- this.label4.TabIndex = 4;
- this.label4.Text = "Hexadecimal";
- //
- // label2
- //
- this.label2.AutoSize = true;
- this.label2.Name = "label2";
- this.label2.TabIndex = 2;
- this.label2.Text = "Binary";
- //
- // txt_number
- //
- this.txt_number.Name = "txt_number";
- this.txt_number.TabIndex = 1;
- //
- // label1
- //
- this.label1.AutoSize = true;
- this.label1.Name = "label1";
- this.label1.TabIndex = 0;
- this.label1.Text = "Enter a number";
- //
- // button1
- //
- this.button1.Font = new System.Drawing.Font("Arial Narrow", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.button1.Name = "button1";
- this.button1.TabIndex = 1;
- this.button1.Text = "Exit Application";
- this.button1.UseVisualStyleBackColor = true;
- //
- // groupBox2
- //
- this.groupBox2.Controls.Add(this.txt_currency);
- this.groupBox2.Controls.Add(this.btn_cclear);
- this.groupBox2.Controls.Add(this.btn_cconvert);
- this.groupBox2.Controls.Add(this.label6);
- this.groupBox2.Controls.Add(this.comboBox1);
- this.groupBox2.Controls.Add(this.txt_mon);
- this.groupBox2.Controls.Add(this.label5);
- this.groupBox2.Font = new System.Drawing.Font("Arial Narrow", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.groupBox2.Name = "groupBox2";
- this.groupBox2.TabIndex = 2;
- this.groupBox2.TabStop = false;
- this.groupBox2.Text = "Currency Conversion";
- //
- // txt_currency
- //
- this.txt_currency.Name = "txt_currency";
- this.txt_currency.ReadOnly = true;
- this.txt_currency.TabIndex = 6;
- //
- // btn_cclear
- //
- this.btn_cclear.Name = "btn_cclear";
- this.btn_cclear.TabIndex = 5;
- this.btn_cclear.Text = "CLEAR";
- this.btn_cclear.UseVisualStyleBackColor = true;
- //
- // btn_cconvert
- //
- this.btn_cconvert.Name = "btn_cconvert";
- this.btn_cconvert.TabIndex = 4;
- this.btn_cconvert.Text = "CONVERT";
- this.btn_cconvert.UseVisualStyleBackColor = true;
- //
- // label6
- //
- this.label6.AutoSize = true;
- this.label6.Name = "label6";
- this.label6.TabIndex = 3;
- this.label6.Text = "Currency";
- //
- // comboBox1
- //
- this.comboBox1.FormattingEnabled = true;
- "Dollar",
- "Rupee",
- "Euro",
- "Yen",
- "Won"});
- this.comboBox1.Name = "comboBox1";
- this.comboBox1.TabIndex = 2;
- //
- // txt_mon
- //
- this.txt_mon.Name = "txt_mon";
- this.txt_mon.TabIndex = 1;
- //
- // label5
- //
- this.label5.AutoSize = true;
- this.label5.Name = "label5";
- this.label5.TabIndex = 0;
- this.label5.Text = "Enter a number";
- //
- // groupBox3
- //
- this.groupBox3.Controls.Add(this.btn_tclear);
- this.groupBox3.Controls.Add(this.btn_tconvert);
- this.groupBox3.Controls.Add(this.txt_conversion);
- this.groupBox3.Controls.Add(this.label9);
- this.groupBox3.Controls.Add(this.txt_totemp);
- this.groupBox3.Controls.Add(this.label8);
- this.groupBox3.Controls.Add(this.comboBox2);
- this.groupBox3.Controls.Add(this.txt_tnumber);
- this.groupBox3.Controls.Add(this.label7);
- this.groupBox3.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
- this.groupBox3.Name = "groupBox3";
- this.groupBox3.TabIndex = 3;
- this.groupBox3.TabStop = false;
- this.groupBox3.Text = "Tempature Conversion";
- //
- // btn_tclear
- //
- this.btn_tclear.Name = "btn_tclear";
- this.btn_tclear.TabIndex = 8;
- this.btn_tclear.Text = "CLEAR";
- this.btn_tclear.UseVisualStyleBackColor = true;
- //
- // btn_tconvert
- //
- this.btn_tconvert.Name = "btn_tconvert";
- this.btn_tconvert.TabIndex = 7;
- this.btn_tconvert.Text = "CONVERT";
- this.btn_tconvert.UseVisualStyleBackColor = true;
- //
- // txt_conversion
- //
- this.txt_conversion.Name = "txt_conversion";
- this.txt_conversion.ReadOnly = true;
- this.txt_conversion.TabIndex = 6;
- //
- // label9
- //
- this.label9.AutoSize = true;
- this.label9.Name = "label9";
- this.label9.TabIndex = 5;
- this.label9.Text = "To";
- //
- // txt_totemp
- //
- this.txt_totemp.Name = "txt_totemp";
- this.txt_totemp.ReadOnly = true;
- this.txt_totemp.TabIndex = 4;
- //
- // label8
- //
- this.label8.AutoSize = true;
- this.label8.Name = "label8";
- this.label8.TabIndex = 3;
- this.label8.Text = "Conversion";
- //
- // comboBox2
- //
- this.comboBox2.FormattingEnabled = true;
- "Fahrenheit",
- "Celsius"});
- this.comboBox2.Name = "comboBox2";
- this.comboBox2.TabIndex = 2;
- //
- // txt_tnumber
- //
- this.txt_tnumber.Name = "txt_tnumber";
- this.txt_tnumber.TabIndex = 1;
- //
- // label7
- //
- this.label7.AutoSize = true;
- this.label7.Name = "label7";
- this.label7.TabIndex = 0;
- this.label7.Text = "Enter a number";
- //
- // Form1
- //
- this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
- this.Controls.Add(this.groupBox3);
- this.Controls.Add(this.groupBox2);
- this.Controls.Add(this.button1);
- this.Controls.Add(this.groupBox1);
- this.Name = "Form1";
- this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
- this.Text = "Simple Conversion Application";
- this.groupBox1.ResumeLayout(false);
- this.groupBox1.PerformLayout();
- this.groupBox2.ResumeLayout(false);
- this.groupBox2.PerformLayout();
- this.groupBox3.ResumeLayout(false);
- this.groupBox3.PerformLayout();
- this.ResumeLayout(false);
- }
- #endregion
- private System.Windows.Forms.GroupBox groupBox1;
- private System.Windows.Forms.Button button1;
- private System.Windows.Forms.TextBox txt_octal;
- private System.Windows.Forms.TextBox txt_hexadecimal;
- private System.Windows.Forms.TextBox txt_binary;
- private System.Windows.Forms.Label label3;
- private System.Windows.Forms.Label label4;
- private System.Windows.Forms.Label label2;
- private System.Windows.Forms.TextBox txt_number;
- private System.Windows.Forms.Label label1;
- private System.Windows.Forms.GroupBox groupBox2;
- private System.Windows.Forms.GroupBox groupBox3;
- private System.Windows.Forms.Button btn_nclear;
- private System.Windows.Forms.Button btn_nconvert;
- private System.Windows.Forms.ComboBox comboBox1;
- private System.Windows.Forms.TextBox txt_mon;
- private System.Windows.Forms.Label label5;
- private System.Windows.Forms.Button btn_cclear;
- private System.Windows.Forms.Button btn_cconvert;
- private System.Windows.Forms.Label label6;
- private System.Windows.Forms.TextBox txt_currency;
- private System.Windows.Forms.Button btn_tclear;
- private System.Windows.Forms.Button btn_tconvert;
- private System.Windows.Forms.TextBox txt_conversion;
- private System.Windows.Forms.Label label9;
- private System.Windows.Forms.TextBox txt_totemp;
- private System.Windows.Forms.Label label8;
- private System.Windows.Forms.ComboBox comboBox2;
- private System.Windows.Forms.TextBox txt_tnumber;
- private System.Windows.Forms.Label label7;
- }
- }
Creating the Script
This code contains the function of the application. This code contains the three main functionnalities of the application. The three are the Number Conversion, Currency Converter, and Temperature conversion. To do that locate the csharp script, In my case the default script will be called Form1.cs. Then write these block of codes inside the Class of the form called Form1.- private void NClear(object sender, EventArgs e) {
- txt_binary.Text = "";
- txt_hexadecimal.Text = "";
- txt_octal.Text = "";
- txt_number.Text = "";
- }
- private void ExitApp(object sender, EventArgs e) {
- Application.Exit();
- }
- private void ConvertNumber(object sender, EventArgs e) {
- if (txt_number.Text != "")
- {
- int num = int.Parse(txt_number.Text);
- txt_binary.Text = Convert.ToString(num, 2);
- txt_hexadecimal.Text = Convert.ToString(num, 16);
- txt_octal.Text = Convert.ToString(num, 8);
- }
- }
- private void CClear(object sender, EventArgs e) {
- txt_mon.Text = "";
- txt_currency.Text = "";
- comboBox1.Text = "";
- }
- private void ConvertCurrency(object sender, EventArgs e)
- {
- if (txt_mon.Text != "" && comboBox1.Text != "")
- {
- Double num = Double.Parse(txt_mon.Text);
- switch (comboBox1.Text)
- {
- case "Dollar":
- txt_currency.Text = Convert.ToString(num * 50);
- break;
- case "Rupee":
- txt_currency.Text = Convert.ToString(num * 1.25);
- break;
- case "Euro":
- txt_currency.Text = Convert.ToString(num * 0.016);
- break;
- case "Yen":
- txt_currency.Text = Convert.ToString(num * 2.06);
- break;
- case "Won":
- txt_currency.Text = Convert.ToString(num * 58.56);
- break;
- }
- }
- }
- private void TClear(object sender, EventArgs e) {
- txt_totemp.Text = "";
- comboBox2.Text = "";
- txt_tnumber.Text = "";
- txt_conversion.Text = "";
- }
- private void ChangeSelection(object sender, EventArgs e)
- {
- if (comboBox2.Text == "Fahrenheit") {
- txt_totemp.Text = "Celsius";
- } else if (comboBox2.Text == "Celsius") {
- txt_totemp.Text = "Fahrenheit";
- }
- }
- private void ConverTemperature(object sender, EventArgs e) {
- Double num = Double.Parse(txt_tnumber.Text);
- if (txt_tnumber.Text != "" && comboBox2.Text != "") {
- if (comboBox2.Text == "Fahrenheit")
- {
- txt_conversion.Text = Convert.ToString(((num - 32) / 9) * 5);
- } else if (comboBox2.Text == "Celsius") {
- txt_conversion.Text = Convert.ToString(((num * 9) / 5) + 32);
- }
- }
- }
Add new comment
- 198 views