끄적이는 메모장

[C#] Windows Form 개발 (3) 본문

C, C++, C#/C# 메모장

[C#] Windows Form 개발 (3)

밥보92 2018. 5. 16. 08:20
반응형

#3 라벨 알아보기

 

윈도우 폼 프로젝트를 생성하고 생성된 창을 디자인 하는 것을 알아 보았다면, 이제 실제로 창 내에 어떤 폼을 추가하고 어떻게 동작하게 할 것인지를 알아보자.

 

1. 라벨

라벨은 텍스트 표현하는 폼이다. (= 안드로이드에서 텍스트뷰 정도로 생각할 수 있을 것 같다.)

- Toolbox에서 Label을 찾아 윈도우에 배치 해보자

 

- 라벨 안에 쓰여진 텍스트를 바꾸기 위해서는 Form1.Designer.cs 파일에서 this.label.Text 부분을 수정 해주면된다.

            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(123, 44);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(38, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "Test1";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(214, 126);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(38, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "Test2";
            //
            // label3
            //
            this.label3.AutoSize = true;
            this.label3.Location = new System.Drawing.Point(100, 157);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(38, 12);
            this.label3.TabIndex = 2;
            this.label3.Text = "Test3"; 

 

- 텍스트 꾸미기

a. 폰트 색상 변경

 this.label1.ForeColor = System.Drawing.Color.Red;

b. 정렬

 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter

# 정렬은 총 아홉가지의 경우의 수가 있다.

 TopLeft

 TopCenter

 TopRight

 MiddleLeft

 MiddleCenter

 MiddleRigth

 BottomLeft

 BottomCenter

 BottomRight

c. 음영

 this.label1.BackColor = System.Drawing.Color.Aqua

d. 글꼴, 글자크기, 글자스타일

this.label1.Font = new System.Drawing.Font("Gulim", 9F, ((System.Drawing.FontStyle)(((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Italic)
                | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(129))); 

# 글꼴은 Gulim, GulimChe, Dotum, DotumChe, Mlgun Gothic, Gungsuh, GungsuhChe 등 시스템의 기본 글꼴을 사용할 수 있다.

# 글자크기는 9F, 10F, 11F 등 원하는크기 + F를 붙여 나타낸다.

# 글자 스타일은 여러개를 지정하는 경우 |를 이용할 수 있다.

- 굵은 글씨 System.Drawing.FontStyle.Bold

- 기울임 System.Drawing.FontStyle.Italic

- 밑줄 System.Drawing.FontStyle.Underline

- 취소선 ystem.Drawing.FontStyle.Strikeout

 

- 라벨 내 텍스트 동적으로 바꾸기

초기에 라벨에 설정된 텍스트 내용을 코드 내에서 동적으로 변경 가능하다.

namespace ExampleWindowForm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.label1.Text = "메롱";
        }       
    }

Form1.cs 파일에서 다음과 같이 Form이 초기화 될 때, label1의 텍스트 내용을 메롱으로 바꾼다고 해보자.

실제 창 내에서 Test1으로 입력되었던 텍스트 내용이 프로그램 실행 시 메롱으로 변경 되어 있는 것을 확인 할 수 있다.

다른 .cs 파일에서 Form1.cs 파일의 label 내의 텍스트를 바꾸고 싶은 경우

 Form1 form = new Form1();
 form.label1.Text = "바보"; 

다음과 같이 form 객체를 생성하여 label내의 텍스트를 바꾸어 줄 수 있다.

반응형

'C, C++, C# > C# 메모장' 카테고리의 다른 글

[C#] Windows Form 개발 (2)  (0) 2018.05.15
[C#] Windows Form 개발 (1)  (0) 2018.05.15
[C#] csv 파일 만들기  (0) 2018.05.11
[C#] csv 파일 읽기  (1) 2018.05.11