C#

033_StackPanel

iwannabebackendexpert 2022. 4. 12. 23:54

<StackPanel Margin="30">
        <TextBlock HorizontalAlignment="Center" FontSize="18" Margin="10 10 10 0"> 좋아하는 프로그래밍 언어를 선택하세요. </TextBlock>
        <Separator Background="Coral" Margin="20"/>
        <CheckBox x:Name="C" Margin="40,5">C</CheckBox>
        <CheckBox x:Name="C_Plus" Margin="40,5">C++</CheckBox>
        <CheckBox x:Name="C_Sharp" Margin="40,5">C#</CheckBox>
        <CheckBox x:Name="Python" Margin="40,5">Python</CheckBox>
        <CheckBox x:Name="Java" Margin="40,5">Java</CheckBox>
        <Separator Background="Coral" Margin="20"/>
        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
            <Button x:Name="btnSubmit"
                    Height="30"
                    Margin="20"
                    Click="btnSubmit_click"
                    >투표하기</Button>
            <Button x:Name="btnExit"
                    Margin="30"
                    Click="btnExit_click"
                    >나가기</Button>
        </StackPanel>
    </StackPanel>

stackpanel은 위에서 쌓아가는것이다.

그래서 선호 프로그래밍언어 창을 만들어보자.

separator로 분리 시켜줌

그리고 CheckBox x:Name을 지정해주고 margin 지정, text도 지정

한번더 separator로 막대기를 놔준다.

이후 버튼을 정렬하기위해 stackpanel안에 두개의 버튼을 넣고 각각의 이름과 margin, click에 관한 이벤트를 지정한다.

private void btnSubmit_click(object sender, RoutedEventArgs e)
        {
            string str = string.Empty;
            foreach (var cb in cbs)
            {
                if (cb.IsChecked == true)
                    str += cb.Content + ",";
            }
            MessageBox.Show(str, "Language Prefered");
        }

        private void btnExit_click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

click박스 이벤트

string str = string.Empty는 string.str = ""와 같다.

foreach로 체크박스들에서 각 체크박스의 값이 참일때 str에 계속 추가하였고 이름 메세지 박스로 출력했다

Exit박스 이벤트는 this.Close로 종료하게하였다.

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

035_036_ChessBoard  (0) 2022.04.13
034_UserControl  (0) 2022.04.13
032_Grid  (0) 2022.04.12
30_WMP  (0) 2022.04.12
031_WPFHELLO  (0) 2022.04.12