
http://ojc.asia/bbs/board.php?bo_table=LecCsharp&wr_id=432
C# 윈폼 실습교육 동영상, Winform 기반 미니 계산기, C#학원, WPF 학원, 닷넷학원교육
ojc.asia

C# 윈폼 실습
Winform 기반 미니 계산기
C# 윈폼기반 미니 계산기
DataTable에서 Compute 메서드는 필터 조건에 맞는 행을 추출하여 특정컬럼을 집계(SUM, AVG, MAX, MIN, COUNT등)한다.
본 계산기에서는 계산을 위한 수식을 DataTable의 Compute 메서드를 이용하여 처리한 간단한 미니 계산기를 만들어 보자.


-------------------------------------------------------
1. 디자인 코드(Form1.Designer.cs)
-------------------------------------------------------
namespace Calc
{
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.textBox1 = new System.Windows.Forms.TextBox();
this.btnC = new System.Windows.Forms.Button();
this.btnPow = new System.Windows.Forms.Button();
this.btnback = new System.Windows.Forms.Button();
this.btnminus = new System.Windows.Forms.Button();
this.btnplus = new System.Windows.Forms.Button();
this.btnthree = new System.Windows.Forms.Button();
this.btntwo = new System.Windows.Forms.Button();
this.btnone = new System.Windows.Forms.Button();
this.btndivide = new System.Windows.Forms.Button();
this.btnnine = new System.Windows.Forms.Button();
this.btneight = new System.Windows.Forms.Button();
this.btnseven = new System.Windows.Forms.Button();
this.btnmultiply = new System.Windows.Forms.Button();
this.btnsix = new System.Windows.Forms.Button();
this.btfive = new System.Windows.Forms.Button();
this.btnfour = new System.Windows.Forms.Button();
this.btnzero = new System.Windows.Forms.Button();
this.btnequal = new System.Windows.Forms.Button();
this.btnperiod = new System.Windows.Forms.Button();
this.btnroot = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(26, 17);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(286, 21);
this.textBox1.TabIndex = 0;
//
// btnC
//
this.btnC.Location = new System.Drawing.Point(28, 62);
this.btnC.Name = "btnC";
this.btnC.Size = new System.Drawing.Size(57, 37);
this.btnC.TabIndex = 1;
this.btnC.Text = "C";
this.btnC.UseVisualStyleBackColor = true;
this.btnC.Click += new System.EventHandler(this.btnC_Click);
//
// btnPow
//
this.btnPow.Location = new System.Drawing.Point(255, 272);
this.btnPow.Name = "btnPow";
this.btnPow.Size = new System.Drawing.Size(57, 37);
this.btnPow.TabIndex = 2;
this.btnPow.Text = "Pow";
this.btnPow.UseVisualStyleBackColor = true;
this.btnPow.Click += new System.EventHandler(this.btnPow_Click);
//
// btnback
//
this.btnback.Location = new System.Drawing.Point(104, 62);
this.btnback.Name = "btnback";
this.btnback.Size = new System.Drawing.Size(57, 37);
this.btnback.TabIndex = 3;
this.btnback.Text = "<--";
this.btnback.UseVisualStyleBackColor = true;
this.btnback.Click += new System.EventHandler(this.btnback_Click);
//
// btnminus
//
this.btnminus.Location = new System.Drawing.Point(257, 62);
this.btnminus.Name = "btnminus";
this.btnminus.Size = new System.Drawing.Size(57, 37);
this.btnminus.TabIndex = 4;
this.btnminus.Text = "-";
this.btnminus.UseVisualStyleBackColor = true;
this.btnminus.Click += new System.EventHandler(this.btnminus_Click);
//
// btnplus
//
this.btnplus.Location = new System.Drawing.Point(178, 62);
this.btnplus.Name = "btnplus";
this.btnplus.Size = new System.Drawing.Size(57, 37);
this.btnplus.TabIndex = 8;
this.btnplus.Text = "+";
this.btnplus.UseVisualStyleBackColor = true;
this.btnplus.Click += new System.EventHandler(this.btnplus_Click);
//
// btnthree
//
this.btnthree.Location = new System.Drawing.Point(178, 115);
this.btnthree.Name = "btnthree";
this.btnthree.Size = new System.Drawing.Size(57, 37);
this.btnthree.TabIndex = 7;
this.btnthree.Text = "3";
this.btnthree.UseVisualStyleBackColor = true;
this.btnthree.Click += new System.EventHandler(this.btnthree_Click);
//
// btntwo
//
this.btntwo.Location = new System.Drawing.Point(104, 115);
this.btntwo.Name = "btntwo";
this.btntwo.Size = new System.Drawing.Size(57, 37);
this.btntwo.TabIndex = 6;
this.btntwo.Text = "2";
this.btntwo.UseVisualStyleBackColor = true;
this.btntwo.Click += new System.EventHandler(this.bttntwo_Click);
//
// btnone
//
this.btnone.Location = new System.Drawing.Point(28, 115);
this.btnone.Name = "btnone";
this.btnone.Size = new System.Drawing.Size(57, 37);
this.btnone.TabIndex = 5;
this.btnone.Text = "1";
this.btnone.UseVisualStyleBackColor = true;
this.btnone.Click += new System.EventHandler(this.btnone_Click);
//
// btndivide
//
this.btndivide.Location = new System.Drawing.Point(255, 165);
this.btndivide.Name = "btndivide";
this.btndivide.Size = new System.Drawing.Size(57, 37);
this.btndivide.TabIndex = 16;
this.btndivide.Text = "/";
this.btndivide.UseVisualStyleBackColor = true;
this.btndivide.Click += new System.EventHandler(this.btndivide_Click);
//
// btnnine
//
this.btnnine.Location = new System.Drawing.Point(178, 218);
this.btnnine.Name = "btnnine";
this.btnnine.Size = new System.Drawing.Size(57, 37);
this.btnnine.TabIndex = 15;
this.btnnine.Text = "9";
this.btnnine.UseVisualStyleBackColor = true;
this.btnnine.Click += new System.EventHandler(this.btnnine_Click);
//
// btneight
//
this.btneight.Location = new System.Drawing.Point(104, 218);
this.btneight.Name = "btneight";
this.btneight.Size = new System.Drawing.Size(57, 37);
this.btneight.TabIndex = 14;
this.btneight.Text = "8";
this.btneight.UseVisualStyleBackColor = true;
this.btneight.Click += new System.EventHandler(this.btneight_Click);
//
// btnseven
//
this.btnseven.Location = new System.Drawing.Point(28, 218);
this.btnseven.Name = "btnseven";
this.btnseven.Size = new System.Drawing.Size(57, 37);
this.btnseven.TabIndex = 13;
this.btnseven.Text = "7";
this.btnseven.UseVisualStyleBackColor = true;
this.btnseven.Click += new System.EventHandler(this.btnseven_Click);
//
// btnmultiply
//
this.btnmultiply.Location = new System.Drawing.Point(255, 115);
this.btnmultiply.Name = "btnmultiply";
this.btnmultiply.Size = new System.Drawing.Size(57, 37);
this.btnmultiply.TabIndex = 12;
this.btnmultiply.Text = "*";
this.btnmultiply.UseVisualStyleBackColor = true;
this.btnmultiply.Click += new System.EventHandler(this.btnmultiply_Click);
//
// btnsix
//
this.btnsix.Location = new System.Drawing.Point(178, 165);
this.btnsix.Name = "btnsix";
this.btnsix.Size = new System.Drawing.Size(57, 37);
this.btnsix.TabIndex = 11;
this.btnsix.Text = "6";
this.btnsix.UseVisualStyleBackColor = true;
this.btnsix.Click += new System.EventHandler(this.btnsix_Click);
//
// btfive
//
this.btfive.Location = new System.Drawing.Point(104, 165);
this.btfive.Name = "btfive";
this.btfive.Size = new System.Drawing.Size(57, 37);
this.btfive.TabIndex = 10;
this.btfive.Text = "5";
this.btfive.UseVisualStyleBackColor = true;
this.btfive.Click += new System.EventHandler(this.btnfive_Click);
//
// btnfour
//
this.btnfour.Location = new System.Drawing.Point(28, 165);
this.btnfour.Name = "btnfour";
this.btnfour.Size = new System.Drawing.Size(57, 37);
this.btnfour.TabIndex = 9;
this.btnfour.Text = "4";
this.btnfour.UseVisualStyleBackColor = true;
this.btnfour.Click += new System.EventHandler(this.btnfour_Click);
//
// btnzero
//
this.btnzero.Location = new System.Drawing.Point(28, 271);
this.btnzero.Name = "btnzero";
this.btnzero.Size = new System.Drawing.Size(57, 37);
this.btnzero.TabIndex = 17;
this.btnzero.Text = "0";
this.btnzero.UseVisualStyleBackColor = true;
this.btnzero.Click += new System.EventHandler(this.btnzero_Click);
//
// btnequal
//
this.btnequal.Location = new System.Drawing.Point(178, 271);
this.btnequal.Name = "btnequal";
this.btnequal.Size = new System.Drawing.Size(57, 37);
this.btnequal.TabIndex = 19;
this.btnequal.Text = "=";
this.btnequal.UseVisualStyleBackColor = true;
this.btnequal.Click += new System.EventHandler(this.btnequal_Click);
//
// btnperiod
//
this.btnperiod.Location = new System.Drawing.Point(104, 271);
this.btnperiod.Name = "btnperiod";
this.btnperiod.Size = new System.Drawing.Size(57, 37);
this.btnperiod.TabIndex = 18;
this.btnperiod.Text = ".";
this.btnperiod.UseVisualStyleBackColor = true;
this.btnperiod.Click += new System.EventHandler(this.btnperiod_Click);
//
// btnroot
//
this.btnroot.Location = new System.Drawing.Point(255, 218);
this.btnroot.Name = "btnroot";
this.btnroot.Size = new System.Drawing.Size(57, 37);
this.btnroot.TabIndex = 20;
this.btnroot.Text = "√";
this.btnroot.UseVisualStyleBackColor = true;
this.btnroot.Click += new System.EventHandler(this.btnroot_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(333, 335);
this.Controls.Add(this.btnroot);
this.Controls.Add(this.btnequal);
this.Controls.Add(this.btnperiod);
this.Controls.Add(this.btnzero);
this.Controls.Add(this.btndivide);
this.Controls.Add(this.btnnine);
this.Controls.Add(this.btneight);
this.Controls.Add(this.btnseven);
this.Controls.Add(this.btnmultiply);
this.Controls.Add(this.btnsix);
this.Controls.Add(this.btfive);
this.Controls.Add(this.btnfour);
this.Controls.Add(this.btnplus);
this.Controls.Add(this.btnthree);
this.Controls.Add(this.btntwo);
this.Controls.Add(this.btnone);
this.Controls.Add(this.btnminus);
this.Controls.Add(this.btnback);
this.Controls.Add(this.btnPow);
this.Controls.Add(this.btnC);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "미니계산기";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button btnC;
private System.Windows.Forms.Button btnPow;
private System.Windows.Forms.Button btnback;
private System.Windows.Forms.Button btnminus;
private System.Windows.Forms.Button btnplus;
private System.Windows.Forms.Button btnthree;
private System.Windows.Forms.Button btntwo;
private System.Windows.Forms.Button btnone;
private System.Windows.Forms.Button btndivide;
private System.Windows.Forms.Button btnnine;
private System.Windows.Forms.Button btneight;
private System.Windows.Forms.Button btnseven;
private System.Windows.Forms.Button btnmultiply;
private System.Windows.Forms.Button btnsix;
private System.Windows.Forms.Button btfive;
private System.Windows.Forms.Button btnfour;
private System.Windows.Forms.Button btnzero;
private System.Windows.Forms.Button btnequal;
private System.Windows.Forms.Button btnperiod;
private System.Windows.Forms.Button btnroot;
}
}
-------------------------------------------------------
2. C# 소스코드(Form1.cs)
-------------------------------------------------------
using System;
using System.Data;
using System.Windows.Forms;
namespace Calc
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//----------------------------------------------------
// 모든 계산 Clear
//----------------------------------------------------
private void btnC_Click(object sender, EventArgs e)
{
textBox1.Clear();
}
//----------------------------------------------------
// BACK 버튼 클릭(맨 오른쪽 숫자 제거)
//----------------------------------------------------
private void btnback_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 0)
{
string text = textBox1.Text;
textBox1.Text = text.Substring(0, text.Length - 1);
}
}
//----------------------------------------------------
// 마이너스(-) 버튼 클릭
//----------------------------------------------------
private void btnminus_Click(object sender, EventArgs e)
{
textBox1.Text += "-";
}
//----------------------------------------------------
// 1 버튼 클릭
//----------------------------------------------------
private void btnone_Click(object sender, EventArgs e)
{
textBox1.Text += "1";
}
//----------------------------------------------------
// 2 버튼 클릭
//----------------------------------------------------
private void bttntwo_Click(object sender, EventArgs e)
{
textBox1.Text += "2";
}
//----------------------------------------------------
// 3 버튼 클릭
//----------------------------------------------------
private void btnthree_Click(object sender, EventArgs e)
{
textBox1.Text += "3";
}
//----------------------------------------------------
// 더하기(+) 버튼 클릭
//----------------------------------------------------
private void btnplus_Click(object sender, EventArgs e)
{
textBox1.Text += "+";
}
//----------------------------------------------------
// 4 버튼 클릭
//----------------------------------------------------
private void btnfour_Click(object sender, EventArgs e)
{
textBox1.Text += "4";
}
//----------------------------------------------------
// 5 버튼 클릭
//----------------------------------------------------
private void btnfive_Click(object sender, EventArgs e)
{
textBox1.Text += "5";
}
//----------------------------------------------------
// 6 버튼 클릭
//----------------------------------------------------
private void btnsix_Click(object sender, EventArgs e)
{
textBox1.Text += "6";
}
//----------------------------------------------------
// 곱하기 버튼 클릭
//----------------------------------------------------
private void btnmultiply_Click(object sender, EventArgs e)
{
textBox1.Text += "*";
}
//----------------------------------------------------
// 7 버튼 클릭
//----------------------------------------------------
private void btnseven_Click(object sender, EventArgs e)
{
textBox1.Text += "7";
}
//----------------------------------------------------
// 8 버튼 클릭
//----------------------------------------------------
private void btneight_Click(object sender, EventArgs e)
{
textBox1.Text += "8";
}
//----------------------------------------------------
// 9 버튼 클릭
//----------------------------------------------------
private void btnnine_Click(object sender, EventArgs e)
{
textBox1.Text += "9";
}
//----------------------------------------------------
// 나누기 버튼 클릭
//----------------------------------------------------
private void btndivide_Click(object sender, EventArgs e)
{
textBox1.Text += "/";
}
//----------------------------------------------------
// 0 버튼 클릭
//----------------------------------------------------
private void btnzero_Click(object sender, EventArgs e)
{
textBox1.Text += "0";
}
//----------------------------------------------------
// 쩜(.) 버튼 클릭
//----------------------------------------------------
private void btnperiod_Click(object sender, EventArgs e)
{
string str = textBox1.Text.ToString();
int len = str.Length;
if (textBox1.Text[--len] != '.')
{
textBox1.Text += ".";
}
}
//----------------------------------------------------
// 등호(=) 버튼 클릭
//----------------------------------------------------
private void btnequal_Click(object sender, EventArgs e)
{
try
{
//DataTable의 Compute메서드는 지정된 식을 계산한다.
//두번째 인자는 필터조건이며 본 예제에서는 필요없다.
//원래 DataTable에서 Compute는 필터 조건에 맞는 행을 추출하여
//특정컬럼을 집계한다. SUM, AVG, MAX, MIN, COUNT등
DataTable dt = new DataTable();
var a = dt.Compute(textBox1.Text, "");
textBox1.Text = a.ToString();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
//----------------------------------------------------
// 루트 버튼 클릭
//----------------------------------------------------
private void btnroot_Click(object sender, EventArgs e)
{
try
{
double a = Math.Sqrt(Double.Parse(textBox1.Text));
textBox1.Text = a.ToString();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
//----------------------------------------------------
// POW 버튼 클릭
//----------------------------------------------------
private void btnPow_Click(object sender, EventArgs e)
{
try
{
double a = Math.Pow(Double.Parse(textBox1.Text), 2);
textBox1.Text = a.ToString();
}
catch (Exception e1)
{
MessageBox.Show(e1.Message);
}
}
}
}
#윈폼, #윈폼계산기, #Winform, #시샵계산기, #Windform계산기, #닷넷계산기, #시샵동영상, #시샵교육, #닷넷교육, #닷넷동영상,윈폼, 윈폼계산기, Winform, 시샵계산기, Windform계산기, 닷넷계산기, 시샵동영상, 시샵교육, 닷넷교육, 닷넷동영상,