Forms/M_CustomerMasterIndividualEdit.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ChatworkBulkSender.Daos;
using ChatworkBulkSender.Dtos;
using ChatworkBulkSender.UserControls;
using ChatworkBulkSender.Utils;

namespace ChatworkBulkSender.Forms
{
    public partial class M_CustomerMasterIndividualEdit : Form,ICreatable<CustomerMasterDto>,IUpdateble<CustomerMasterDto>
    {
        public enum ButtonMode
        {
            Register, // 新規登録
            Update    // 更新
            ,
        }

        private CustomerIndividualEditControl _customerIndividualEdit = null;

        private BtnRegisterControl _btnRegister = null;

        private BtnUpdateControl _btnUpdate = null;

        public event EventHandler<CustomerMasterDto> DataCreated;
        public event EventHandler<CustomerMasterDto> DataUpdated;

        // 初期値は、新規登録
        private ButtonMode _btnMode = ButtonMode.Register;


        public M_CustomerMasterIndividualEdit(ButtonMode buttonMode = ButtonMode.Register, CustomerMasterDto customer = null)
        {
            InitializeComponent();

            _btnMode = buttonMode;

            this.ClientSize = new Size(1600,900);
            this.MinimumSize = new Size(1200,700);
            this.StartPosition = FormStartPosition.CenterScreen;

            _customerIndividualEdit = new CustomerIndividualEditControl();
            _customerIndividualEdit.Dock = DockStyle.Fill;

            if (_btnMode == ButtonMode.Update)
            {
                _btnUpdate = new BtnUpdateControl();
                _btnUpdate.Dock = DockStyle.Fill;
                panel2.Controls.Add(_btnUpdate);
                if (customer != null)
                {
                    _customerIndividualEdit = new CustomerIndividualEditControl(customer);
                }
            }
            else
            {
                _btnRegister = new BtnRegisterControl();
                _btnRegister.Dock = DockStyle.Fill;
                panel2.Controls.Add(_btnRegister);
                _customerIndividualEdit = new CustomerIndividualEditControl();
                CheckBox chkIsUnused = _customerIndividualEdit.Controls.Find("chkUnusedData", true).FirstOrDefault() as CheckBox;
                chkIsUnused.Visible = false;
            }
            panel1.Controls.Add(_customerIndividualEdit);

        }

        private void M_CustomerMasterIndividualEdit_Load(object sender, EventArgs e)
        {
            this.ActiveControl = panel1;

            if (_btnMode == ButtonMode.Update)
            {
                _btnUpdate.BtnUpdateClicked += BtnUpdate_Click;
                _btnUpdate.BtnReturnClicked += BtnReturn_Click;

            }
            else
            {
                _btnRegister.BtnRegisterClicked += BtnRegister_Click;
                _btnRegister.BtnReturnClicked += BtnReturn_Click;
            }

        }

        private void DisposeViewControls()
        {
            panel1.Controls.Clear();
            panel2.Controls.Clear();

            _customerIndividualEdit.Dispose();
            
            if (_btnMode == ButtonMode.Update)
            {
                _btnUpdate.Dispose();
            }
            else
            {
                _btnRegister.Dispose();
            }
        }

        private void BtnRegister_Click(object sender, EventArgs e)
        {
            try
            { 
                var newCustomer = _customerIndividualEdit.GetCustomerData();

                var dao = new CustomerMasterDao();

                var createdId = dao.Insert(newCustomer);

                if (createdId == 1)
                {
                    DataCreated?.Invoke(this, newCustomer);


                }

            }
            catch(Exception ex)
            {
                MessageBoxUtil.ShowErr($"{ex.Message}\n\n");
            }
        }

        private void BtnUpdate_Click(object sender, EventArgs e)
        {
            DataUpdated?.Invoke(this,new CustomerMasterDto());
        }

        private void BtnReturn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void M_CustomerIndividualEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("顧客マスタ 個別編集画面を終了してもよろしいですか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            if (result == DialogResult.No)
            {
                e.Cancel = true;

                return;
            }

            // イベント登録の解除
            if (_btnMode == ButtonMode.Update)
            {
                _btnUpdate.BtnReturnClicked -= BtnReturn_Click;
            }
            else
            {
                _btnRegister.BtnReturnClicked -= BtnReturn_Click;
            }

            // 明示的に解放する
            this.DisposeViewControls();

            // 設定メニューを表示する
            this.Owner.Show();

            // 再描写を行う
            this.Owner.Refresh();
        }
    }



}