C# 入門指南

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;
using EncryptNamespace;
using System.IO;

namespace WpfAppDemo
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        private Encrypt encryptObject;
        private string result;

        internal Encrypt EncryptObject { get => encryptObject; set => encryptObject = value; }
        public string Result { get => result; set => result = value; }

        public MainWindow()
        {
            InitializeComponent();
        }

        private void NewButton_Click(object sender, RoutedEventArgs e)
        {
            EncryptObject = new Encrypt();
            DisplayBlock.Text = "密碼表:" + EncryptObject.Code;
        }

        private void LoadButton_Click(object sender, RoutedEventArgs e)
        {
            string path = "code.text";
            if (File.Exists(path))
            {
                StreamReader sr = new StreamReader(path);
                EncryptObject = new Encrypt(sr.ReadLine());
                sr.Close();
                DisplayBlock.Text = "載入成功!";
            }
            else
            {
                DisplayBlock.Text = "載入失敗!";
            }
        }

        private void SaveButton_Click(object sender, RoutedEventArgs e)
        {
            string path = "code.text";
            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
            {

                if (EncryptObject != null)
                {
                    StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                    sw.WriteLine(EncryptObject.Code);
                    sw.Close();
                    DisplayBlock.Text = "存檔完成!";
                }
                else
                {
                    DisplayBlock.Text = "沒有編碼物件!";
                }
            }
        }

        private void Encodebutton_Click(object sender, RoutedEventArgs e)
        {
            if (InputText.Text == "")
            {
                DisplayBlock.Text = "沒有輸入英文句子!";
            }
            else
            {
                if (EncryptObject == null)
                {
                    DisplayBlock.Text = "沒有密碼物件!";
                }
                else
                {
                    Result = EncryptObject.ToEncode(InputText.Text);
                    OutputText.Text = Result;
                    DisplayBlock.Text = "編碼成功!";
                }
            }
        }

        private void DecodeButton_Click(object sender, RoutedEventArgs e)
        {
            if (InputText.Text == "")
            {
                DisplayBlock.Text = "沒有輸入英文句子!";
            }
            else
            {
                if (EncryptObject == null)
                {
                    DisplayBlock.Text = "沒有密碼物件!";
                }
                else
                {
                    Result = EncryptObject.ToDecode(InputText.Text);
                    OutputText.Text = Result;
                    DisplayBlock.Text = "解碼成功!";
                }
            }
        }

        private void CopyButton_Click(object sender, RoutedEventArgs e)
        {
            if (Result != "")
            {
                Clipboard.SetText(Result);
                DisplayBlock.Text = "已拷貝!";
            }
            else
            {
                DisplayBlock.Text = "無資料!";
            }
        }

        private void ClearButton_Click(object sender, RoutedEventArgs e)
        {
            InputText.Text = "";
            OutputText.Text = "";
            EncryptObject = null;
            Result = "";
            DisplayBlock.Text = "已清除!";
        }

        private void InputText_TextChanged(object sender, TextChangedEventArgs e)
        {
            DisplayBlock.Text = InputText.Text;
        }
    }
}

//《程式語言教學誌》的範例程式
// http://kaiching.org/
// 專案:WpfAppDemo
// 檔名:MainWindow.xaml.cs
// 功能:Encrypt 的 Windows App 類別
// 作者:張凱慶

回 C# 入門指南首頁
回 C# 教材首頁
回程式語言教材首頁