hahahia

C# GUI 구구단 프로그램 본문

Windows Programming/Windows::C#

C# GUI 구구단 프로그램

hahahia 2012. 2. 16. 21:39



// form1.cs 

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;
// textBox1의 Text를 읽어서 gugudan함수를 호출하여 구구단을 출력시키는 프로그램이다.
namespace gugudan
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) // 버튼1을 클릭했을 때 이벤트발생
        {
            gugudan(int.Parse(textBox1.Text)); //구구단 함수 출력함수호출, 그리고 받아들이는 textBox1.Text는 string형이므로 int형으로 형변환을 해야한다.
        }
        private void gugudan(int number)
        {
            textBox1.Text = "";
            label1.Text = "";
            for (int i = 2; i <= 9; i++) // for문 이용하여 *2~*9까지 출력
            label1.Text += number.ToString() + " * " + i.ToString() + " = " + (number *               i).ToString() + "\n"; 
// 출력하는 방식은 간단히 문자열을 더한다고 생각하면 편하겠다. 그리고 label1.Text는 마찬가지로 string형이기 때문에 ToString()이라는 메서드를 이용하여 문자열로 다시 바꾸어서 저장시킨다.
            }
        }

        private void button2_Click(object sender, EventArgs e) // 버튼2(초기화)
        {
            label1.Text = "";  // 구구단이 출력된 것을 지워준다,초기화
        }
    }
}

12를 입력하고 출력버튼을 눌렀을 경우 오른쪽 화면에서와 같이 구구단이 출력된다....

'Windows Programming > Windows::C#' 카테고리의 다른 글

[C#] 메서드 재정의(virtual, override)  (0) 2013.03.31
[C#] 상속 예제  (0) 2013.03.31
윈도우즈 폼 예제  (0) 2012.02.16
C# 콘솔 예제(성적관리 프로그램)  (0) 2012.02.16
Comments