본문 바로가기
C#

C# 콘솔

by improve 2024. 2. 13.

콘솔 애플리케이션을 사용해본다.

 

 

 

namespace WinFormsApp3
{
    public partial class Form1 : Form
    {
        private Button button1;

        private Button button2;
        public Form1()
        {
            InitializeComponent();
            button1 = new Button();
            // 
            // button1
            // 
            button1.Location = new Point(440, 122);
            button1.Name = "button1";
            button1.Size = new Size(162, 86);
            button1.TabIndex = 0;
            button1.Text = "button1";
            button1.UseVisualStyleBackColor = true;
            button1.Click += button1_Click;
            Controls.Add(button1);

            button2 = new Button();
            button2.Location = new Point(440, 200);
            button2.Name = "button2";
            button2.Size = new Size(162, 86);
            button2.TabIndex = 1;
            button2.Text = "button2";
            button2.UseVisualStyleBackColor = true;
            button2.Click += button2_Click;
            Controls.Add(button2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("안녕하세요");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Console.WriteLine("손수 만들었습니다.");
        }
    }
}

 

도구상자를 사용하지 않고 직접 버튼을 만들수 있다.

 

 

이렇게 된다.

 

●반복문 

namespace WinFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private int y = 30;

        private void button1_Click(object sender, EventArgs e)
        {
            // 자동정렬
            for (int i = 0; i < 10; i++)
            {
              
                Button btn1 = new Button();
                btn1.Text = "OK";
                btn1.Location = new Point(300, 10+y*i);

                Controls.Add(btn1);
            }

            
        }
    }
}

for구문을 사용해서 여러개의 버튼을 만들수 있다.

 

●Random

 

    private void button1_Click(object sender, EventArgs e)
    {
        // 자동정렬 ctrl +k ->f
        for (int i = 0; i < 10; i++)
        {            
            Button btn1 = new Button();
            btn1.Text = "OK";
            btn1.Location = new Point(300, 10+y*i);
            btn1.UseVisualStyleBackColor = false;
            btn1.Click += Btn1_Click;
            Controls.Add(btn1);
        }

        
    }

    private void Btn1_Click(object? sender, EventArgs e)
    {

        Random random = new Random();
        int x = random.Next(1, 300);
        int y = random.Next(10, 200);
        Console.WriteLine(x);
        Console.WriteLine(y);
        // 안전한 형변환
        //(Button)sender; -> 예외....
        Button btn = sender as Button;
        if (btn.BackColor == Color.Red)
        {
            btn.BackColor = Color.White;
            btn.Text = "OK";
            btn.Location = new Point(0, 0);
            btn.Location = new Point(x, y);
        }
        else
        {
            btn.BackColor = Color.Red;    
            btn.Text = "not ok";
        }
        Console.WriteLine("ok 버튼 누름");
    }
}

 

버튼을 직접 만들고 x,y 좌표를 랜덤으로 만들 수 있다.

'C#' 카테고리의 다른 글

원 처럼 움직이기  (0) 2024.02.19
라벨 이용하기  (0) 2024.02.16
c# oracle에서 연동, listbox에 보여주기.  (0) 2024.02.08
계산기  (0) 2024.02.08
visual studio 2022 와 oracle 연동 해보기  (0) 2024.02.07