JY Tech

윈폼으로 설문조사 결과산출 프로그램 만들기(4) 본문

Programming/MiniProject

윈폼으로 설문조사 결과산출 프로그램 만들기(4)

J.Dragon 2023. 4. 22. 22:05

이제 ▲, ▼, ◀, ▶ 버튼에 기능을 넣어주자. 이 부분은 기본 알고리즘은 동일하고 앞서 만들었던 다수의 사용자 정의 컨트롤에 기능을 넣어주는 것이기 때문에 코드의 길이만 길 뿐 어렵지도 않고 설명할 부분도 없다.


먼저 ▲에 기능을 넣어주자. 행의 개수를 줄이는 버튼이다

DialogResult dr = MessageBox.Show("행을 삭제할 경우 기존 입력 내용이 초기화 됩니다\n마지막 행을 삭제할까요?", "경고!", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                if (line > 2)
                {
                    if (ans == "1")
                    {
                        flowLayoutPanel1.Controls.Remove(btn21[line - 1]);
                        line -= 1;
                        DataTable table = new DataTable();
                        for (int i = 1; i <= 2; i++)
                        {
                            table.Columns.Add(i + "번 응답", typeof(string));
                        }
                        for (int i = 1; i < line - 1; i++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "2")
                    {

                        flowLayoutPanel1.Controls.Remove(btn31[line - 1]);
                        line -= 1;
                        DataTable table = new DataTable();
                        for (int i = 1; i <= 3; i++)
                        {
                            table.Columns.Add(i + "번 응답", typeof(string));
                        }
                        for (int i = 1; i < line - 1; i++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "3")
                    {

                        flowLayoutPanel1.Controls.Remove(btn41[line - 1]);
                        line -= 1;
                        DataTable table = new DataTable();
                        for (int i = 1; i <= 4; i++)
                        {
                            table.Columns.Add(i + "번 응답", typeof(string));
                        }
                        for (int i = 1; i < line - 1; i++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "4")
                    {
                        flowLayoutPanel1.Controls.Remove(btn[line - 1]);
                        line -= 1;
                        DataTable table = new DataTable();
                        for (int i = 1; i <= 5; i++)
                        {
                            table.Columns.Add(i + "번 응답", typeof(string));
                        }
                        for (int i = 1; i < line - 1; i++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "5")
                    {

                        flowLayoutPanel1.Controls.Remove(btn61[line - 1]);
                        line -= 1;
                        DataTable table = new DataTable();
                        for (int i = 1; i <= 6; i++)
                        {
                            table.Columns.Add(i + "번 응답", typeof(string));
                        }
                        for (int i = 1; i < line - 1; i++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "6")
                    {
                        flowLayoutPanel1.Controls.Remove(btn71[line - 1]);
                        line -= 1;
                        DataTable table = new DataTable();
                        for (int i = 1; i <= 7; i++)
                        {
                            table.Columns.Add(i + "번 응답", typeof(string));
                        }
                        for (int i = 1; i < line - 1; i++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                }
                else
                {
                    MessageBox.Show("최소 두개 이상의 행을 추가해야 추가한 행을 제거할 수 있습니다!", "경고!");
                    return;
                }


DialogResult는 예, 아니오로 응답을 받을 수 있게 해준다

 


이런식으로 말이다.

위 코드의 알고리즘은 간단하다



사용자가 다이얼로그에서 "예"를 누르면 ProMode에서 Setting했던 ans값을 다시 확인하고 

라인을 하나 제거한다.

 

public UserControl1[] btn = new UserControl1[501]; //userControl1번을 저장하기 위한 배열


앞서 각 버튼들을 위와같이 배열로 구성한 이유가 이때문이다. 현재 flowLayoutPanel에 있는 줄 수를 알아내기 위해 배열의 인덱스 값을 사용한 것이다. 

 

이후 다시 ProMode에 처음 접속했을 때 처럼 새로 DataTable을 재생성하여 테이블을 수정한다.

사실 값을 줄일 때마다 다시 flowLayoutPanel과 dataGridView를 구성하는게 비효율적이라고 생각되지만 ProMode의 경우 Setting 화면에서 최초구성을 완료하기 때문에 크게 수정할 상황이 많지 않을 것 같아서 이렇게 구성했다.

 

▼에도 기능을 넣어주자. 행의 개수를 늘리는 버튼이다.

▲와 차이점은 행을 늘리냐 줄이냐 일 뿐 알고리즘은 완전 동일하다.

 

if(line<=500)
            { 
                DialogResult dr = MessageBox.Show("행을 추가할 경우 기존 입력 내용이 초기화 됩니다\n행을 추가할까요?", "경고!", MessageBoxButtons.YesNo);
                if (dr == DialogResult.Yes)
                {
                    if (ans == "1")
                    {
                        this.userControl21 = new UserControl2();
                        flowLayoutPanel1.Controls.Add(userControl21);
                        userControl21.label1.Text = Convert.ToString(line);
                        btn21[line] = userControl21;
                        line += 1;
                        DataTable table = new DataTable();
                        for (int j = 1; j <= 2; j++)
                        {
                            table.Columns.Add(j + "번 응답", typeof(string));
                        }
                        for (int j = 1; j < line - 1; j++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "2")
                    {
                        this.userControl31 = new UserControl3();
                        flowLayoutPanel1.Controls.Add(userControl31);
                        userControl31.label1.Text = Convert.ToString(line);
                        btn31[line] = userControl31;
                        line += 1;
                        DataTable table = new DataTable();
                        for (int j = 1; j <= 3; j++)
                        {
                            table.Columns.Add(j + "번 응답", typeof(string));
                        }
                        for (int j = 1; j < line - 1; j++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "3")
                    {
                        this.userControl41 = new UserControl4();
                        flowLayoutPanel1.Controls.Add(userControl41);
                        userControl41.label1.Text = Convert.ToString(line);
                        btn41[line] = userControl41;
                        line += 1;
                        DataTable table = new DataTable();
                        for (int j = 1; j <= 4; j++)
                        {
                            table.Columns.Add(j + "번 응답", typeof(string));
                        }
                        for (int j = 1; j < line - 1; j++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "4")
                    {
                        this.userControl11 = new UserControl1();
                        flowLayoutPanel1.Controls.Add(userControl11);
                        userControl11.label1.Text = Convert.ToString(line);
                        btn[line] = userControl11;
                        line += 1;
                        DataTable table = new DataTable();
                        for (int j = 1; j <= 5; j++)
                        {
                            table.Columns.Add(j + "번 응답", typeof(string));
                        }
                        for (int j = 1; j < line - 1; j++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    if (ans == "5")
                    {
                        this.userControl61 = new UserControl6();
                        flowLayoutPanel1.Controls.Add(userControl61);
                        userControl61.label1.Text = Convert.ToString(line);
                        btn61[line] = userControl61;
                        line += 1;
                        DataTable table = new DataTable();
                        for (int j = 1; j <= 6; j++)
                        {
                            table.Columns.Add(j + "번 응답", typeof(string));
                        }
                        for (int j = 1; j < line - 1; j++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                    else if (ans == "6")
                    {
                        this.userControl71 = new UserControl7();
                        flowLayoutPanel1.Controls.Add(userControl71);
                        userControl71.label1.Text = Convert.ToString(line);
                        btn71[line] = userControl71;
                        line += 1;
                        DataTable table = new DataTable();
                        for (int j = 1; j <= 7; j++)
                        {
                            table.Columns.Add(j + "번 응답", typeof(string));
                        }
                        for (int j = 1; j < line - 1; j++)
                        {
                            table.Rows.Add("");
                        }
                        dataGridView1.DataSource = table;
                    }
                }
            }
            else
            {
                MessageBox.Show("행은 최대 500개까지만 추가할 수 있습니다!", "경고!");
                return;
            }

 

▶에도 기능을 넣어주자. 열의 개수를 늘리는 버튼이다.

DialogResult dr = MessageBox.Show("선택지의 개수를 추가할 경우 기존 입력 내용이 초기화 됩니다\n선택지의 개수를 추가할까요?", "경고!", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                if (ans == "1")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "2";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl31 = new UserControl3();
                        flowLayoutPanel1.Controls.Add(userControl31);
                        userControl31.label1.Text = Convert.ToString(line);
                        btn31[line] = userControl31;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "2")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "3";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl41 = new UserControl4();
                        flowLayoutPanel1.Controls.Add(userControl41);
                        userControl41.label1.Text = Convert.ToString(line);
                        btn41[line] = userControl41;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "3")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "4";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl11 = new UserControl1();
                        flowLayoutPanel1.Controls.Add(userControl11);
                        userControl11.label1.Text = Convert.ToString(line);
                        btn[line] = userControl11;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "4")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "5";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl61 = new UserControl6();
                        flowLayoutPanel1.Controls.Add(userControl61);
                        userControl61.label1.Text = Convert.ToString(line);
                        btn61[line] = userControl61;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "5")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "6";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl71 = new UserControl7();
                        flowLayoutPanel1.Controls.Add(userControl71);
                        userControl71.label1.Text = Convert.ToString(line);
                        btn71[line] = userControl71;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "6")
                {
                    MessageBox.Show("선택지의 개수는 최대 7개까지만 설정할 수 있습니다!","경고");
                }
            }

이번에는 ▲▼와 비슷하면서도 다르다.

 

먼저 사용자가 선택지 2개짜리를 선택해 

이런식의 화면을 구성한 상태라고 가정해보자

현재 화면에는 UserControl2가 띄워진 것이다.(이 부분은 필자와 이름을 다르게 설정하면 UserControl2가 아닐 수도 있다)

그 상태에서 ▶ 버튼을 누르면 프로그램은 현재의 ans값을 인식할 것이다.(이 경우 ans는 1이 될 것이다)

cLine에 현재 line값을 임시로 저장한다. ans를 현재 ans보다 1만큼 증가시키고

for문을 돌면서 이전 line에 저장된 만큼 새로운 사용자 정의 컨트롤(이 경우 UserControl3)을 생성한다.

 

결국 ProModeSetting에서 ProMode로 넘어오면서 했던 작업들의 반복인 것이다.

 

◀에도 기능을 넣어주자. 열의 개수를 줄이는 버튼이다.

알고리즘은 ▶와 유사하다

 

DialogResult dr = MessageBox.Show("선택지의 개수를 줄일 경우 기존 입력 내용이 초기화 됩니다\n선택지의 개수를 줄일까요?", "경고!", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                if (ans == "1")
                {
                    MessageBox.Show("선택지의 개수는 최소 2개까지만 설정할 수 있습니다!","경고!");
                }
                else if (ans == "2")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "1";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl21 = new UserControl2();
                        flowLayoutPanel1.Controls.Add(userControl21);
                        userControl21.label1.Text = Convert.ToString(line);
                        btn21[line] = userControl21;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "3")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "2";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl31 = new UserControl3();
                        flowLayoutPanel1.Controls.Add(userControl31);
                        userControl31.label1.Text = Convert.ToString(line);
                        btn31[line] = userControl31;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "4")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "3";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl41 = new UserControl4();
                        flowLayoutPanel1.Controls.Add(userControl41);
                        userControl41.label1.Text = Convert.ToString(line);
                        btn41[line] = userControl41;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "5")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "4";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl11 = new UserControl1();
                        flowLayoutPanel1.Controls.Add(userControl11);
                        userControl11.label1.Text = Convert.ToString(line);
                        btn[line] = userControl11;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
                else if (ans == "6")
                {
                    cLine = line - 1;
                    line = 1;
                    ans = "5";
                    flowLayoutPanel1.Controls.Clear();
                    for (int i = 0; i < cLine; i++)
                    {
                        this.userControl61 = new UserControl6();
                        flowLayoutPanel1.Controls.Add(userControl61);
                        userControl61.label1.Text = Convert.ToString(line);
                        btn61[line] = userControl61;
                        line += 1;
                    }
                    DataTable table = new DataTable();
                    for (int j = 1; j <= Convert.ToInt64(ans) + 1; j++)
                    {
                        table.Columns.Add(j + "번 응답", typeof(string));
                    }
                    for (int j = 1; j < line - 1; j++)
                    {
                        table.Rows.Add("");
                    }
                    dataGridView1.DataSource = table;
                }
            }

 

이번에는 별 내용은 없었지만 코드의 길이만 길었다..

다음에는 UserControl8을 추가하여 주관식응답을 관리해 보자

 

해당 프로젝트는 7회까지 이어질 것 같다