Forms/T_SendHistoryDetail.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.UserControls;
using ChatworkBulkSender.Dtos;
using ChatworkBulkSender.Daos;
namespace ChatworkBulkSender.Forms
{
    public partial class T_SendHistoryDetail : Form
    {
        private MessageSendResultControl _messageSendResult = null;

        private BtnReturnControl _btnControl = null;

        private SendHistoryDto _sendHistory = null;

        public T_SendHistoryDetail(SendHistoryDto sendHistory = null)
        {
            InitializeComponent();

            _sendHistory = sendHistory;

            this.ClientSize = new Size(1600, 900);

            this.StartPosition = FormStartPosition.CenterScreen;

            _messageSendResult = new MessageSendResultControl();
            _btnControl = new BtnReturnControl();

            // ユーザーコントロールの表示設定
            SetupMessageSendResult();

            // Dockの設定
            _messageSendResult.Dock = DockStyle.Fill;
            // パネルへ追加
            panelHistoryDetail.Controls.Add(_messageSendResult);
            panelBtn.Controls.Add(_btnControl);

        }

        private void T_SendHistoryDetail_Load(object sender, EventArgs e)
        {
            _btnControl.BtnReturnClicked += BtnReturn_Click;
            
            // 追加:再送ボタンのイベントハンドラを登録
            _messageSendResult.SwitchToMessageInputOnlyFailed += MessageSendResult_SwitchToMessageInputOnlyFailed;

            // 送信履歴データを表示
            if (_sendHistory != null)
            {
                // MessageSendResultControlのRenderResultsメソッドを使用してヘッダー情報も含めて表示
                _messageSendResult.RenderResults(_sendHistory.SendHistoryId);

                // 送信タイプに応じてモードを変更
                _messageSendResult.ChangeMode((Utils.Constants.SEND_TYPE)_sendHistory.SendType);
            }
        }

        /// <summary>
        /// ユーザーコントロール「MessageSendResultControl」の表示設定を行う。
        /// </summary>
        private void SetupMessageSendResult()
        {
            // ラベル1を検索し、最初に一致したラベルを取得後、非表示にする
            Label label1 = _messageSendResult.Controls.Find("label1", false).FirstOrDefault() as Label;
            if (label1 != null)
            {
                label1.Visible = false;
            }

            // txtResultsを再帰的に検索し、最初に一致したテキストボックスを取得後、編集不可に設定する
            TextBox txtResults = _messageSendResult.Controls.Find("txtResults", true).FirstOrDefault() as TextBox;
            if (txtResults != null)
            {
                txtResults.ReadOnly = true;
            }

            // txtErrorResultsを再帰的に検索し、最初に一致したテキストボックスを取得後、編集不可に設定する
            TextBox txtErrorResults = _messageSendResult.Controls.Find("txtErrorResults", true).FirstOrDefault() as TextBox;
            if (txtErrorResults != null)
            {
                txtErrorResults.ReadOnly = true;
            }

            // 所得したタブ内のページからテキストボックスのコントロールを検索し取得する
            TextBox actualContents = _messageSendResult.Controls.Find("txtActualContents", true).FirstOrDefault() as TextBox;
            if (actualContents != null)
            {
                // 編集不可に設定にする
                actualContents.ReadOnly = true;
            }

            // 所得したタブ内のページからテキストボックスのコントロールを検索し取得する
            TextBox attachmentFolder = _messageSendResult.Controls.Find("txtAttachmentFolder", true).FirstOrDefault() as TextBox;
            if (attachmentFolder != null)
            {
                // 編集不可に設定する
                attachmentFolder.ReadOnly = true;
            }

            // ボタンコントロールを検索し、取得後非表示にする
            Button button = _messageSendResult.Controls.Find("btnBack", true).FirstOrDefault() as Button;
            if (button != null)
            {
                button.Visible = false;
            }
        }

        /// <summary>
        /// フォームに表示するユーザーコントロールをパネルから削除した後、明示的なDisposeで解放する。
        /// </summary>
        private void DisposeViewControls()
        {
            // パネルから削除する
            panelHistoryDetail.Controls.Clear();
            panelBtn.Controls.Clear();

            // 削除だけでは解放されないので、明示的に解放する
            _messageSendResult.Dispose();
            _btnControl.Dispose();
        }

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

        /// <summary>
        /// 送信失敗データ再送ボタンクリック時の処理
        /// </summary>
        /// <param name="reSendHistoryId">再送する履歴ID</param>
        private void MessageSendResult_SwitchToMessageInputOnlyFailed(int reSendHistoryId)
        {
            try
            {
                // 送信タイプを取得(定期送信か不定期送信か)
                var sendType = (Utils.Constants.SEND_TYPE)_sendHistory.SendType;
                
                // T_MessageSenderフォームのインスタンスを生成
                T_MessageSender messageSender = new T_MessageSender(sendType);
                
                // 現在のフォームを親フォームとして設定
                messageSender.Owner = this;
                
                // 現在のフォームを非表示にする
                this.Hide();
                
                // 送信失敗データを読み込んで表示
                messageSender.SwitchToMessageInputOnlyFailedRequested(reSendHistoryId);
                
                // モーダルダイアログとして表示
                messageSender.ShowDialog();
            }
            catch (Exception ex)
            {
                // エラーが発生した場合はメッセージを表示
                MessageBox.Show($"送信失敗データの再送画面を開く際にエラーが発生しました。\n{ex.Message}", 
                               "エラー", MessageBoxButtons.OK, MessageBoxIcon.Error);
                
                // フォームを再表示
                this.Show();
            }
        }

        private void T_SendHistoryDetail_FormClosing(object sender, FormClosingEventArgs e)
        {
            DialogResult result = MessageBox.Show("送信履歴管理 詳細画面を終了してもよろしいですか?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

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

                return;
            }

            // イベント登録の解除
            _btnControl.BtnReturnClicked -= BtnReturn_Click;
            
            // 追加:再送ボタンのイベントハンドラの登録解除
            _messageSendResult.SwitchToMessageInputOnlyFailed -= MessageSendResult_SwitchToMessageInputOnlyFailed;

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

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

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

        }
    }
}