C# tab control에 winform 넣기

2019. 4. 20. 20:49기타

C#을 쓰다 안 쓰다가 하니깐.. 이런것도 헷갈리네.. -_-

 

1. WinForm(Form1)에다가 tab control 1개(tabControl), 버튼 3개(btnAddType1, btnAddType2, btnClose) 입력

 

2. WinForm을 2개 더 생성(Form2, Form3)

3. 다음과 같이 코딩

        private void btnAddType1_Click(object sender, EventArgs e)
        {
            Form2 form = new Form2();
            form.TopLevel = false;
            tabControl.TabPages.Add((tabControl.TabPages.Count + 1).ToString());
            tabControl.TabPages[tabControl.TabPages.Count - 1].Controls.Add(form);
            tabControl.SelectedIndex = tabControl.TabPages.Count - 1;
            tabControl.TabPages[tabControl.TabPages.Count - 1].Controls.Add(form);
            form.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            form.Show();
        }

        private void btnAddType2_Click(object sender, EventArgs e)
        {
            Form3 form = new Form3();
            form.TopLevel = false;
            tabControl.TabPages.Add((tabControl.TabPages.Count + 1).ToString());
            tabControl.TabPages[tabControl.TabPages.Count - 1].Controls.Add(form);
            tabControl.SelectedIndex = tabControl.TabPages.Count - 1;
            tabControl.TabPages[tabControl.TabPages.Count - 1].Controls.Add(form);
            form.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            form.Show();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            if (tabControl.TabPages.Count > 0)
                tabControl.TabPages.Remove(tabControl.TabPages[tabControl.TabPages.Count - 1]);
            else
                MessageBox.Show("삭제할 탭이 없습니다");
        }

 

4. 테스트 : 추가 삭제 테스트