ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • C# 두개의 폼 띄우기
    C#/WindowForm 2022. 3. 30. 01:25

    첫번째 폼이다.

    첫번째 폼

    아래는 첫번째 폼의 코드이다

    namespace _016
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                this.ClientSize = new System.Drawing.Size(500, 500); //폼1의 크기
                button1.Width = 100; //버튼의 가로
                button1.Height = 50; //버튼의 세로
                button1.Location = new System.Drawing.Point(500 / 2 - button1.Width / 2, 500 / 2 - button1.Height / 2);
                button1.Text = "Form2 생성";
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Form f2 = new Form2();//f2는 Form2임
                this.AddOwnedForm(f2); //f2를 이 폼에 포함시킴
                f2.Show(); //f2를 보여줌
    
            }
        }
    }

    2번째 폼

    2번쨰 폼

    namespace _016
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
                button1.Width = 150;
                button1.Text = "Close Form2";
                button1.Location = new Point(this.ClientSize.Width / 2 - button1.Width / 2, this.ClientSize.Height / 2 - button1.Height / 2);
    
    
            }
    
            private void Form2_Load(object sender, EventArgs e)
            {
                CenterToParent(); //부모의 중앙에 로드됨
    
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                this.Close(); //2번째 폼을 종료
            }
        }
    }

    this.AddOwnedForm(f2)를 사용하지 않을 경우 CenterToParent()의 부모가 없어 적용되지 않는다.

     

    실행결과

    실행결과 1
    실행결과 2

    Close Form2를 누를경우 2번째 폼만 종료된다.

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

    ComboBox  (0) 2022.04.06
    C# 라디오버튼과 그룹박스  (0) 2022.03.30
    C# 체크박스  (0) 2022.03.30
    C# Flag  (0) 2022.03.30
    C# 윈도우 폼 메시지 박스  (0) 2022.03.30

    댓글

Designed by Tistory.