[C# 윈폼] TableLayoutPanel 에 컨트롤 추가하기


안녕하세요.

 

오늘은 C# 윈폼에서 TableLayoutPanel 컨트롤에 대해서 알아 보려고 합니다. 그 중에서도, TableLayoutPanel에 컨트롤을 추가하는 방법에 대해서 알려드리려고 해요.

 

그럼 바로, 예제 프로그램을 만들어서 어떻게 TableLayoutPaenl에 컨트롤을 추가하는지 알아보겠습니다.


 

먼저, 아래와 같이 빈 윈폼 프로젝트를 생성해 주시고 TableLayoutPanel 컨트롤을 배치해 주시기 바랍니다.


빈 윈폼 프로젝트 생성 및 TableLayoutPanel 컨트롤 배치



위와 같이 TableLayoutPanel 컨트롤을 윈폼에 배치를 하였다면, 이제 각각의 Panel 영역에 서로 다른 컨트롤을 소스코드로 추가해 보도록 할게요.

 

[예제 코드]


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace TableLayPanelControlAdd

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

 

            //Load 이벤트 선언

            this.Load += Load_Form;

        }

 

        /// <summary>

        ///  Load 이벤트

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        public void Load_Form(object sender, EventArgs e)

        {

            //버튼 컨트롤 생성

            Button btn1 = CreateButton("button1""버튼 1");

            Button btn2 = CreateButton("button2""버튼 2");

            Button btn3 = CreateButton("button3""버튼 3");

            Button btn4 = CreateButton("button4""버튼 4");

 

            //TableLayoutPanel 버튼 컨트롤 배치하기

            uiTlp_Main.Controls.Add(btn1, 00);

            uiTlp_Main.Controls.Add(btn2, 01);

            uiTlp_Main.Controls.Add(btn3, 10);

            uiTlp_Main.Controls.Add(btn4, 11);

        }

 

        //버튼 컨트롤 생성

        public Button CreateButton(string name, string text)

        {

            Button btn = new Button();

            btn.Name = name;

            btn.Text = text;

            btn.Dock = DockStyle.Fill;

 

            return btn;

        }

    }

}

 

Colored by Color Scripter

cs

 

[실행 결과]



위와 같이 버튼 컨트롤 4개가 각각 좌표에 맞게, TableLayoutPanel 컨트롤에 알맞게 추가된 모습을 확인하실 수 있습니다.

 

감사합니다.^^


728x90

이 글을 공유하기

댓글

Designed by JB FACTORY