UserControls/SenderInfoControl.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;

namespace ChatworkBulkSender.UserControls
{
    public partial class SenderInfoControl : AbstractUserControl
    {

        public event EventHandler BtnReturnClicked;

        private List<SenderMasterDto> _senderMasterList;

        public SenderInfoControl()
        {
            InitializeComponent();
        }

        private void SenderInfoControl_Load(object sender, EventArgs e)
        {
            if (!IsInDesignMode())
            {
                LoadSenderInfo();
                
                // レイアウトを調整
                AdjustControlLayouts();
            }
        }


        private void LoadSenderInfo()
        {
            var dao = new SenderMasterDao();

            _senderMasterList = dao.GetAll();

            var senderInfo = _senderMasterList.FirstOrDefault();

            apiTokenTxtBox.Text = senderInfo.SenderApiToken;

            testRoomIDTxtBox.Text = senderInfo.TestRoomId;

            senderNameMemoTxtBox.Text = senderInfo.SenderAccountName;
        }

        private void BtnReturn_Click(object sender, EventArgs e)
        {
            BtnReturnClicked?.Invoke(this,EventArgs.Empty);
        }
        
        /// <summary>
        /// コントロールのレイアウトを調整
        /// </summary>
        private void AdjustControlLayouts()
        {
            // テスト送信先ルームID欄の右端位置を取得
            int rightMostPosition = testRoomIDTxtBox.Location.X + testRoomIDTxtBox.Width;
            
            // Chatwork送信用アカウント名欄の幅を調整
            senderNameMemoTxtBox.Width = rightMostPosition - senderNameMemoTxtBox.Location.X;
            
            // 左側の余白を調整(20pxに統一)
            int leftMargin = 20;
            
            // APIトークン関連
            lblApiToken.Location = new Point(leftMargin, lblApiToken.Location.Y);
            apiTokenTxtBox.Location = new Point(leftMargin, apiTokenTxtBox.Location.Y);
            
            // APIトークンのテキストボックスの幅も調整
            apiTokenTxtBox.Width = lblApiTokenNote.Location.X + lblApiTokenNote.Width - leftMargin;
            
            // Chatwork送信用アカウント名関連
            lblSenderNameMemoNote.Location = new Point(leftMargin, lblSenderNameMemoNote.Location.Y);
            lblSenderNameMemo.Location = new Point(leftMargin, lblSenderNameMemo.Location.Y);
            senderNameMemoTxtBox.Location = new Point(leftMargin, senderNameMemoTxtBox.Location.Y);
        }

    }
}