2021년 11월 6일 토요일

계산기, 닷넷 C# 윈폼기반의 계산기를 작성하세요

 계산기, 닷넷 C# 윈폼기반의 계산기를 작성하세요


-------------------------------------------------------
1. 디자인 코드
-------------------------------------------------------
 
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.btnCE = 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_1);
            //
            // btnCE
            //
            this.btnCE.Location = new System.Drawing.Point(104, 62);
            this.btnCE.Name = "btnCE";
            this.btnCE.Size = new System.Drawing.Size(57, 37);
            this.btnCE.TabIndex = 2;
            this.btnCE.Text = "CE";
            this.btnCE.UseVisualStyleBackColor = true;
            this.btnCE.Click += new System.EventHandler(this.btnCE_Click);
            //
            // btnback
            //
            this.btnback.Location = new System.Drawing.Point(178, 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(257, 115);
            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(257, 218);
            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(257, 165);
            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, 271);
            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.btnCE);
            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 btnCE;
        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# 소스코드
-------------------------------------------------------
 
 
 
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Calc
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        float num1, ans;
        int count;
        private void btnC_Click_1(object sender, EventArgs e)
        {
            textBox1.Clear();
            count = 0;
        }
        private void btnCE_Click(object sender, EventArgs e)
        {
            if (num1 == 0 && textBox1.TextLength > 0)
            {
                num1 = 0; textBox1.Clear();
            }
            else if (num1 > 0 && textBox1.TextLength > 0)
            {
                textBox1.Clear();
            }
        }
        private void btnback_Click(object sender, EventArgs e)
        {
            int lenght = textBox1.TextLength - 1;
            string text = textBox1.Text;
            textBox1.Clear();
            for (int i = 0; i < lenght; i++)
                textBox1.Text = textBox1.Text + text[i];
        }
        private void btnminus_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                num1 = float.Parse(textBox1.Text);
                textBox1.Clear();
                textBox1.Focus();
                count = 1;
            }
        }
        private void btnone_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 1;
        }
        private void bttntwo_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 2;
        }
        private void btnthree_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 3;
        }
        private void btnplus_Click(object sender, EventArgs e)
        {
            num1 = float.Parse(textBox1.Text);
            textBox1.Clear();
            textBox1.Focus();
            count = 2;
        }
        private void btnfour_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 4;
        }
        private void btnfive_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 5;
        }
        private void btnsix_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 6;
        }
        private void btnmultiply_Click(object sender, EventArgs e)
        {
            num1 = float.Parse(textBox1.Text);
            textBox1.Clear();
            textBox1.Focus();
            count = 3;
        }
        private void btnseven_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 7;
        }
        private void btneight_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 8;
        }
        private void btnnine_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 9;
        }
        private void btndivide_Click(object sender, EventArgs e)
        {
            num1 = float.Parse(textBox1.Text);
            textBox1.Clear();
            textBox1.Focus();
            count = 4;
        }
        private void btnzero_Click(object sender, EventArgs e)
        {
            textBox1.Text = textBox1.Text + 0;
        }
        private void btnperiod_Click(object sender, EventArgs e)
        {
            int c = textBox1.TextLength;
            int flag = 0;
            string text = textBox1.Text;
            for (int i = 0; i < c; i++)
            {
                if (text[i].ToString() == ".")
                {
                    flag = 1; break;
                }
                else
                {
                    flag = 0;
                }
            }
            if (flag == 0)
            {
                textBox1.Text = textBox1.Text + ".";
            }
        }
        private void btnequal_Click(object sender, EventArgs e)
        {
            compute(count);
        }
        private void btnroot_Click(object sender, EventArgs e)
        {
            num1 = float.Parse(textBox1.Text);
            textBox1.Clear();
            textBox1.Focus();
            count = 5;
        }
        public void compute(int count)
        {
            switch (count)
            {
                case 1:
                    ans = num1 - float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                case 2:
                    ans = num1 + float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                case 3:
                    ans = num1 * float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                case 4:
                    ans = num1 / float.Parse(textBox1.Text);
                    textBox1.Text = ans.ToString();
                    break;
                case 5:
                    ans = (float)Math.Sqrt(num1);
                    textBox1.Text = ans.ToString();
                    break;
                default:
                    break;
            }
        }
    }
}
 
 
[실행화면]


C# 윈폼(TreeView, ListView)

 C# 윈폼(TreeView, ListView)


1. Form의 속성(F4)중 Text속성을 “TreeView&ListView TEST”, Name속성을 “FrmMain”으로…


2. 아래와 같이 버튼 컨트롤을 배치하자.(툴 박스가 안 보인다면 View > Toolbox를 클릭)


    - GroupBox(Text: TreeView & ListView)

    - TreeView(Name : trv1), ListView(Name : lstv1, View : Details), 

    - Button(Name : addRoot, Text : 루트추가), Button(Name : addChild, Text : 자식추가)


auK4qPVbPpAD4gCemzcBTKYStjB_5u0w6_0q6Cl8



3. Form1.cs 파일을 코드 편집기로 열어 아래 내용을 추가하자.

    public partial class FrmMain : Form

    {

        Random r = new Random(37);

        public FrmMain()

        {

            InitializeComponent();

        }

    }


4. FrmMain의 생성자에 아래 코드를 추가하자.


        public FrmMain()

        {

            InitializeComponent();

            lstv1.Columns.Add("Name");

            lstv1.Columns.Add("Depth");

        }


5. Form1.cs 파일을 코드 편집기로 열어 아래 내용을 추가하자.


        void TreeToList()

        {

            lstv1.Items.Clear();

            foreach (TreeNode node in trv1.Nodes)

            {

                TreeToList(node);

            }

        }


        void TreeToList(TreeNode node)

        {

            //TreeNode 형식의 FullPath 속성은 루트노드에서 현재노드까지의 경로를 나타내며

            //각 경로는 \로 구분한다.

            lstv1.Items.Add(

                new ListViewItem(

                     new string[] { node.Text,

                                    node.FullPath.Count(f => f == '\\').ToString() }));

            foreach (TreeNode n in node.Nodes)

            {

                TreeToList(n);

            }

        }


6. 이번에는 버튼 클릭 이벤트 처리용 메소드를 만들자.


        private void addRoot_Click(object sender, EventArgs e)

        {

            trv1.Nodes.Add(r.Next().ToString());

            TreeToList();

        }

        private void addChild_Click(object sender, EventArgs e)

        {

            if (trv1.SelectedNode == null)

            {

                MessageBox.Show("선태된 노드 없음.", "트리뷰테스트",

                                MessageBoxButtons.OK, MessageBoxIcon.Error);

                return;

            }

            trv1.SelectedNode.Nodes.Add(r.Next().ToString());

            trv1.SelectedNode.Expand();

            TreeToList();

        }


-JLeNI-9qCzOBLZtCYh6fnUL-KAFxSbPGHPBjhaR

 

 

C# 윈폼(Button, Form, Dialog)

 C# 윈폼(Button, Form, Dialog)


윈도우 프로그램은 두 가지 모드로 자식 윈도우를 띄울 수 있다. Modal 형태로 띄우면 새로 뜬 윈도우가 창을 닫을 때 까지 부모 윈도우를 절대 사용할 수 없는 경우이며 심각한 정보를 표시하거나 중요한 결정 사항을 꼭 입력 받은 후 다음으로 진행하는 경우에 주로 사용한다.

Modaless인 경우 새창이 뜨더라도 이전 창의 UI에 접근이 가능한 형태가 되는데, 웹브라우저의 파일 다운로드 창이 좋은 예이다.


1. Form의 속성(F4)중 Name속성을 “frmMain”, Text 속성을 “모달테스트”로 하자.

2. 아래와 같이 버튼 컨트롤을 배치하자.(툴 박스가 안 보인다면 메뉴 > View > Toolbox를 클릭)


    - Name :btnModal(Text:Modal), btnModaless(Text:Modaless), 

               btnMessage(Text:MessageBox)


rS4LWZ9revp5kIP2yKIJJ_L-vW3F2tGv5JkyIrYR



3. btnModal, btnModaless, btnMessage의 Click 이벤트에 각각 이벤트 처리용 메소드를 만들어 등록하자.


        private void btnModal_Click(object sender, EventArgs e)

        {

            Form f = new Form();

            f.Text = "Model Windows";

            f.Width = 200;

            f.Height = 100;

            f.BackColor = Color.Blue;


            //모달창으로 윈도우를 연다.

            f.ShowDialog();  

        }

        private void btnModaless_Click(object sender, EventArgs e)

        {

            Form f = new Form();

            f.Text = "Modeless Windows";

            f.Width = 200;

            f.Height = 100;

            f.BackColor = Color.Black;


            //모달창이 아닌 윈도우를 연다.

            f.Show();  

        }

        private void btnMessage_Click(object sender, EventArgs e)

        {

            MessageBox.Show("오라클자바커뮤니티", "메시지박스", MessageBoxButtons.OK, 

                             MessageBoxIcon.Exclamation);  //경고창

        }

#윈폼, #Button, #Form, #Dialog 

C# 윈폼(TrackBar, ProgressBar)

 C# 윈폼(TrackBar, ProgressBar)


실습을 위해 프로젝트를 하나 생성하자. (Windows Forms Application)


1. Form의 속성(F4)중 Text속성을 “TrackBar, ProgressBar 컨트롤 테스트”로 하자.


2. 아래와 같이 컨트롤을 배치하자.(툴 박스가 안 보인다면 View > Toolbox를 클릭)


WRkg75_Md6uPKp99HWyM0oNpBsY4Yg3saL23PQ1B



3. 컨트롤을 위한 이벤트 처리용 메소드를 만들자. 


public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        //TrackBar 컨트롤의 속성(F4)에서 번개그림을 눌러 해당 이벤트를 더블클릭

        private void trb1_Scroll(object sender, EventArgs e)

        {

    trb1.Maximum = 100;

            prb1.Maximum = 100;

            prb1.Value = trb1.Value;

        }


        //버튼(btnStart) 컨트롤의 속성(F4)에서 번개그림을 눌러 Click 이벤트를 더블클릭

        private void btnStart_Click(object sender, EventArgs e)

        {

            prb1.Maximum = int.Parse(txtLoopCount.Text);

            prb1.Step = int.Parse(txtLoopCount.Text)/100;

            for(int i = 0; i <= int.Parse(txtLoopCount.Text); i++) {

                prb1.Value = i;

            }

        }

    }


16suQVoipN_tup6jHLyx3FzV4-ae4z2ISlIc4hzL

 

 

(C#교육동영상)C# ADO.NET 실습 ODP.NET/ODAC 설치 오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원

  (C#교육동영상)C# ADO.NET 실습  ODP.NET/ODAC 설치  오라클 함수 호출 실습, C#학원, WPF학원, 닷넷학원, 자바학원 https://www.youtube.com/watch?v=qIPU85yAlzc&list=PLxU-i...