1. xaml 소스 코드
<Window x:Class="cal.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:cal"
mc:Ignorable="d"
Title="MainWindow" Height="628.153" Width="406.745">
<Grid HorizontalAlignment="Left" Width="404" Margin="0,0,-5,-1">
<Grid.RowDefinitions>
<RowDefinition Height="200"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
<RowDefinition Height="75"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<Label x:Name="resultLabel"
Content="0"
Grid.ColumnSpan="4"
Grid.Row="0"
HorizontalContentAlignment="Right"
VerticalContentAlignment="Bottom"
FontSize="100"/>
<!-- 숫자 버튼 0 -->
<Button x:Name="ZeroButton"
Grid.Column="0"
Grid.Row="5"
Content="0"
Margin="5,5,5,-13"
Grid.ColumnSpan="2"
Foreground="white"
Background="Gray"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 1 -->
<Button x:Name="OneButton"
Grid.Column="0"
Grid.Row="4"
Content="1"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 2 -->
<Button x:Name="TwoButton"
Grid.Column="1"
Grid.Row="4"
Content="2"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 3 -->
<Button x:Name="ThreeButton"
Grid.Column="2"
Grid.Row="4"
Content="3"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 4 -->
<Button x:Name="FourButton"
Grid.Column="0"
Grid.Row="3"
Content="4"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 5 -->
<Button x:Name="FiveButton"
Grid.Column="1"
Grid.Row="3"
Content="5"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 6 -->
<Button x:Name="SixButton"
Grid.Column="2"
Grid.Row="3"
Content="6"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 7 -->
<Button x:Name="SevenButton"
Grid.Column="0"
Grid.Row="2"
Content="7"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!-- 숫자 버튼 8 -->
<Button x:Name="EightButton"
Grid.Column="1"
Grid.Row="2"
Content="8"
Foreground="white"
Background="Gray"
FontSize="30"
Margin="5,5,5,5"
Click="numberButton_Click"/>
<!-- 숫자 버튼 9 -->
<Button x:Name="NineButton"
Grid.Column="2"
Grid.Row="2"
Content="9"
Foreground="white"
Background="Gray"
Margin="5,5,5,5"
FontSize="30"
Click="numberButton_Click"/>
<!--연산버튼 AC-->
<Button x:Name="AcButton"
Grid.Column="0"
Grid.Row="1"
Content="AC"
FontSize="30"
Margin="5,5,5,5"
Click="AcButton_Click"/>
<!-- 연산버튼 Plus/Minus -->
<Button x:Name="PlusMinusButton"
Grid.Column="1"
Grid.Row="1"
Content="+/-"
FontSize="30"
Margin="5,5,5,5"
Click="PlusMinusButton_Click"/>
<!-- 연산버튼 Percent -->
<Button x:Name="PercentButton"
Grid.Column="2"
Grid.Row="1"
Content="%"
FontSize="30"
Margin="5,5,5,5"
Click="PercentButton_Click"/>
<!-- 연산버튼 Divide -->
<Button x:Name="DivideButton"
Grid.Column="3"
Grid.Row="1"
Content="/"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Multiple -->
<Button x:Name="MultipleButton"
Grid.Column="3"
Grid.Row="2"
Content="*"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Minus -->
<Button x:Name="MinusButton"
Grid.Column="3"
Grid.Row="3"
Content="-"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Plus -->
<Button x:Name="PlusButton"
Grid.Column="3"
Grid.Row="4"
Content="+"
Background="Green"
Margin="5,5,5,5"
FontSize="30"
Click="operationButton_Click"/>
<!-- 연산버튼 Dot -->
<Button x:Name="DotButton"
Grid.Column="2"
Grid.Row="5"
Content="."
Foreground="white"
Background="Gray"
FontSize="30"
Margin="5,5,5,-13"
Click="DotButton_Click"/>
<!-- 연산버튼 Equl -->
<Button x:Name="EqulButton"
Grid.Column="3"
Grid.Row="5"
Content="="
Background="Green"
Margin="5,5,5,-13"
FontSize="30"
Click="EqulButton_Click"/>
</Grid>
</Window>
2. 이벤트 함수 소스 코드
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 cal
{
/// <summary>
/// MainWindow.xaml에 대한 상호 작용 논리
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
//각 버튼에 대한 이벤트 함수를 안만들고 아래와 같이 해도 상관 없음.
//AcButton.Click += AcButton_Click;
}
public enum SelectedOperator
{
Addition,
Subtraction,
Multipleication,
Division
}
public class SimpleMath
{
public static double Add(double n1, double n2)
{
return n1 + n2;
}
public static double Subtract(double n1, double n2)
{
return n1 - n2;
}
public static double Multiple(double n1, double n2)
{
return n1 * n2;
}
public static double Divide(double n1, double n2)
{
return n1 / n2;
}
}
// 아래의 이벤트 함수를 쓰려면 xaml에서 Click="numberButton_Click"으로 해줘야 함
//
private void numberButton_Click(object sender, RoutedEventArgs e)
{
//MessageBox.Show("버튼 눌림");
int selectedValue = 0;
if (sender == ZeroButton)
{
selectedValue = 0;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == OneButton)
{
selectedValue = 1;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == TwoButton)
{
selectedValue = 2;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == ThreeButton)
{
selectedValue = 3;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == FourButton)
{
selectedValue = 4;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == FiveButton)
{
selectedValue = 5;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == SixButton)
{
selectedValue = 6;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == SevenButton)
{
selectedValue = 7;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == EightButton)
{
selectedValue = 8;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
if (sender == NineButton)
{
selectedValue = 9;
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = selectedValue.ToString();
}
else
{
resultLabel.Content += selectedValue.ToString();
}
}
}
// Plus, Minus, Multiple, Divide 4개가 있는 이벤트 함수
private void operationButton_Click(object sender, RoutedEventArgs e)
{
if(sender == PlusButton)
{
}
if (sender == MinusButton)
{
}
if (sender == MultipleButton)
{
}
if (sender == DivideButton)
{
}
}
// AC 버튼 클릭
private void AcButton_Click(object sender, RoutedEventArgs e)
{
resultLabel.Content = "0";
}
// PlusMinus 버튼 클릭
private void PlusMinusButton_Click(object sender, RoutedEventArgs e)
{
double lastNumber;
if(double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber = lastNumber * -1;
resultLabel.Content = lastNumber.ToString();
}
}
// Percent 버튼 클릭
private void PercentButton_Click(object sender, RoutedEventArgs e)
{
double lastNumber;
if (double.TryParse(resultLabel.Content.ToString(), out lastNumber))
{
lastNumber = lastNumber * 0.01;
resultLabel.Content = lastNumber.ToString();
}
}
// Dot 버튼 클릭
private void DotButton_Click(object sender, RoutedEventArgs e)
{
}
// Equl 버튼 클릭
private void EqulButton_Click(object sender, RoutedEventArgs e)
{
}
// 숫자 0 버튼 클릭
/*private void ZeroButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "0";
}
else
{
resultLabel.Content += "0";
}
}
// 숫자 1 버튼 클릭
private void OneButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "1";
}
else
{
resultLabel.Content += "1";
}
}
// 숫자 2 버튼 클릭
private void TwoButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "2";
}
else
{
resultLabel.Content += "2";
}
}
// 숫자 3 버튼 클릭
private void ThreeButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "3";
}
else
{
resultLabel.Content += "3";
}
}
// 숫자 4 버튼 클릭
private void FourButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "4";
}
else
{
resultLabel.Content += "4";
}
}
// 숫자 5 버튼 클릭
private void FiveButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "5";
}
else
{
resultLabel.Content += "5";
}
}
// 숫자 6 버튼 클릭
private void SixButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "6";
}
else
{
resultLabel.Content += "6";
}
}
// 숫자 7 버튼 클릭
private void SevenButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "7";
}
else
{
resultLabel.Content += "7";
}
}
// 숫자 8 버튼 클릭
private void EightButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "8";
}
else
{
resultLabel.Content += "8";
}
}
// 숫자 9 버튼 클릭
private void NineButton_Click(object sender, RoutedEventArgs e)
{
if (resultLabel.Content.ToString() == "0")
{
resultLabel.Content = "9";
}
else
{
resultLabel.Content += "9";
}
}
*/
/*
// Divide 버튼 클릭
private void DivideButton_Click(object sender, RoutedEventArgs e)
{
}
// Multiple 버튼 클릭
private void MultipleButton_Click(object sender, RoutedEventArgs e)
{
}
// Minus 버튼 클릭
private void MinusButton_Click(object sender, RoutedEventArgs e)
{
}
// Plus 버튼 클릭
private void PlusButton_Click(object sender, RoutedEventArgs e)
{
}
*/
}
}
3. 해야할 것들
1) number 버튼 이벤트 함수 코드 간소화
'Language > C# WPF Programming' 카테고리의 다른 글
C# WPF 프로그래밍 TreeView 아이템 컨트롤 구현하기 (0) | 2019.04.19 |
---|---|
C# WPF 프로그래밍 탭 컨트롤(Tab Control) 구현하기 (0) | 2019.04.18 |
C# WPF 프로그래밍 메뉴(Menu) 아이템 컨트롤 구현하기 (0) | 2019.04.17 |
C# WPF 프로그래밍 아이템 컨트롤 리스트 박스(list box)구현 (0) | 2019.04.16 |
C# WPF 프로그래밍 계산기 프로그램 만들기(Calculator) - 2(xaml 코드 정리 및 이벤트 함수 생성 2가지) (0) | 2019.04.11 |
C# WPF 프로그래밍 계산기 프로그램 만들기(Calculator) - 1(xaml 구현) (0) | 2019.04.10 |
C# WPF 프로그래밍 label과 TextBlock (0) | 2019.04.09 |
C# WPF 프로그래밍 Radio Button 이벤트 (0) | 2019.04.08 |
최근댓글