■ MessageBox 클래스를 사용해 메시지 박스를 표시하는 방법을 보여준다.
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MessageBox 클래스 : 메시지 박스 사용하기" FontFamily="나눔고딕코딩" FontSize="16" SizeToContent="WidthAndHeight"> <Window.Resources> <Style TargetType="{x:Type Label}"> <Setter Property="Margin" Value="5" /> </Style> <Style TargetType="{x:Type TextBox}"> <Setter Property="Margin" Value="5" /> </Style> <Style TargetType="{x:Type ComboBox}"> <Setter Property="Margin" Value="5" /> </Style> </Window.Resources> <Grid Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Label Grid.Row="0" Grid.Column="0" Content="소유자 윈도우 연결 여부" /> <CheckBox Name="ownerWindowCheckBox" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" /> <Label Grid.Row="1" Grid.Column="0" Content="메시지" /> <TextBox Name ="messageTextBox" Grid.Row="1" Grid.Column="1" VerticalContentAlignment="Center" Text="샘플 메시지" /> <Label Grid.Row="2" Grid.Column="0" Content="제목" /> <TextBox Name="captionTextBox" Grid.Row="2" Grid.Column="1" VerticalContentAlignment="Center" Text="제목" /> <Label Grid.Row="3" Grid.Column="0" Content="버튼" /> <ComboBox Name="messageBoxButtonComboBox" Grid.Row="3" Grid.Column="1"> <ComboBoxItem IsSelected="True">OK</ComboBoxItem> <ComboBoxItem>OKCancel</ComboBoxItem> <ComboBoxItem>YesNo</ComboBoxItem> <ComboBoxItem>YesNoCancel</ComboBoxItem> </ComboBox> <Label Grid.Row="4" Grid.Column="0" Content="아이콘" /> <ComboBox Name="messageBoxImageComboBox" Grid.Row="4" Grid.Column="1"> <ComboBoxItem>Asterisk</ComboBoxItem> <ComboBoxItem>Error</ComboBoxItem> <ComboBoxItem>Exclamation</ComboBoxItem> <ComboBoxItem>Hand</ComboBoxItem> <ComboBoxItem>Information</ComboBoxItem> <ComboBoxItem IsSelected="True">None</ComboBoxItem> <ComboBoxItem>Question</ComboBoxItem> <ComboBoxItem>Stop</ComboBoxItem> <ComboBoxItem>Warning</ComboBoxItem> </ComboBox> <Label Grid.Row="5" Grid.Column="0" Content="디폴트 결과" /> <ComboBox Name="messageBoxResultComboBox" Grid.Row="5" Grid.Column="1"> <ComboBoxItem>Cancel</ComboBoxItem> <ComboBoxItem>No</ComboBoxItem> <ComboBoxItem IsSelected="True">None</ComboBoxItem> <ComboBoxItem>OK</ComboBoxItem> <ComboBoxItem>Yes</ComboBoxItem> </ComboBox> <Label Grid.Row="6" Grid.Column="0" Content="옵션" /> <ComboBox Name="messageBoxOptionsComboBox" Grid.Row="6" Grid.Column="1"> <ComboBoxItem>DefaultDesktopOnly</ComboBoxItem> <ComboBoxItem IsSelected="True">None</ComboBoxItem> <ComboBoxItem>RightAlign</ComboBoxItem> <ComboBoxItem>RtlReading</ComboBoxItem> <ComboBoxItem>ServiceNotification</ComboBoxItem> </ComboBox> <Button Grid.Row="7" Grid.Column="1" Margin="5" Padding="5" Content="Test" Click="testButton_Click" /> <StatusBar Grid.Row="8" Grid.Column="0" Grid.ColumnSpan="2" Margin="0 5 0 0" Padding="5"> <StatusBarItem> <TextBlock Name="resultTextBlock" FontFamily="나눔고딕코딩" FontSize="16" Text="대기" /> </StatusBarItem> </StatusBar> </Grid> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
using System; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Test 버튼 클릭시 처리하기 - testButton_Click(sender, e) /// <summary> /// Test 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void testButton_Click(object sender, RoutedEventArgs e) { Window window = ((bool)this.ownerWindowCheckBox.IsChecked ? this : null); string message = this.messageTextBox.Text; string caption = this.captionTextBox.Text; MessageBoxButton messageBoxButton = (MessageBoxButton )Enum.Parse(typeof(MessageBoxButton ), this.messageBoxButtonComboBox.Text ); MessageBoxImage messageBoxImage = (MessageBoxImage )Enum.Parse(typeof(MessageBoxImage ), this.messageBoxImageComboBox.Text ); MessageBoxResult messageBoxResult = (MessageBoxResult )Enum.Parse(typeof(MessageBoxResult ), this.messageBoxResultComboBox.Text ); MessageBoxOptions messageBoxOptions = (MessageBoxOptions)Enum.Parse(typeof(MessageBoxOptions), this.messageBoxOptionsComboBox.Text); MessageBoxResult resultMessageBoxResult; if(window == null) { resultMessageBoxResult = MessageBox.Show(message, caption, messageBoxButton, messageBoxImage, messageBoxResult, messageBoxOptions); } else { resultMessageBoxResult = MessageBox.Show(window, message, caption, messageBoxButton, messageBoxImage, messageBoxResult, messageBoxOptions); } this.resultTextBlock.Text = "결과 = " + resultMessageBoxResult.ToString(); } #endregion } } |