(C# WPF) 버튼을 누르면 메시지 박스 출력되게 만들기.

홍영주 (PolyglotMan)·2021년 10월 17일
0

주제

  • 버튼을 누르면 메시지 박스가 출력되게 만들기.

코드

  • MainWindow.xaml
<Window x:Class="ButtonAndAlarm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ButtonAndAlarm"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Button Content="눌러봐" HorizontalAlignment="Left" Margin="284,178,0,0" VerticalAlignment="Top" Height="68" Width="126" Click="Button_Click"/>

    </Grid>
</Window>
  • MainWindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ButtonAndAlarm
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public MainWindow()
		{
			InitializeComponent();
		}

		#region 버튼 클릭 이벤트 처리
		private void Button_Click(object sender, RoutedEventArgs e)
		{
			MessageBoxResult result = MessageBox.Show("안녕하세요?", "제목표시줄", MessageBoxButton.YesNo ,  MessageBoxImage.Warning);
			if (result == MessageBoxResult.Yes)
			{
				MessageBox.Show("Yes를 눌렀습니다.");
			}else if(result == MessageBoxResult.No)
			{
				MessageBox.Show("No를 눌렀습니다.");
			}
		}
		#endregion
	}
}

느낀점

MessageBox의 결과를 MessageBoxResult의 변수로 결과값을 받고 그에 따른 처리를 할 수가 있다.

profile
안녕하세요~

0개의 댓글