投稿者 kai  (高校生) 投稿日時 2024/8/13 19:07:45
いろいろな意見ありがとうございます。
意見を参考にもう一回調べ直したりして書き直してみました。
namespace マルチメモ帳
{
    public partial class MainSystem : Form
    {
        private List<DraggableRichTextBox> Textcards = new List<DraggableRichTextBox>();
        private List<DraggablePictureBox> Imagecards = new List<DraggablePictureBox>();
        public MainSystem()
        {
            InitializeComponent();
        }

        private void AddCardButton_Click(object sender, EventArgs e)
        {
            //RichTetxtBoxの初期プロパティを設定
            DraggableRichTextBox newDraggableRichTextBox = new DraggableRichTextBox()
            {
                Size = new Size(200, 100),
                Location = new Point(10, 30 + ((Textcards.Count + Imagecards.Count) * 120)),
                BackColor = Color.White,
                ForeColor = Color.Black,
                BorderStyle = BorderStyle.Fixed3D,
            };
            this.cardPanel.Controls.Add(newDraggableRichTextBox);
           Textcards.Add(newDraggableRichTextBox);
        }
        private void AddImageButton_Click(object sender, EventArgs e)
        {

            // OpenFileDialogのインスタンスを作成
            OpenFileDialog openFileDialog = new OpenFileDialog
            {
                Filter = "画像ファイル|*.jpg;*.jpeg;*.png;*.bmp"
            };

            // ダイアログを表示
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // 選択されたファイルのパスを取得
                string selectedFilePath = openFileDialog.FileName;

                // ImageCardのインスタンスを作成
                DraggablePictureBox newpictureBox = new DraggablePictureBox()
                {
                    Size = new Size(200, 200),
                    Location = new Point(10, 30 + ((Textcards.Count + Imagecards.Count) * 120)),
                    BorderStyle = BorderStyle.Fixed3D,
                };


                // 画像を設定
                newpictureBox.Image = Image.FromFile(selectedFilePath);
                newpictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
                // フォームに表示
                this.cardPanel.Controls.Add(newpictureBox);
            }
        }
    }
    public class DraggableRichTextBox : RichTextBox
    {
        private DragHandler dragHandler = new DragHandler();

        public DraggableRichTextBox()
        {
            this.MouseDown += new MouseEventHandler(dragHandler.HandleMouseDown);
            this.MouseMove += new MouseEventHandler(dragHandler.HandleMouseMove);
            this.MouseUp += new MouseEventHandler(dragHandler.HandleMouseUp);
        }
    }
    public class DraggablePictureBox : PictureBox
    {
        private DragHandler dragHandler = new DragHandler();

        public DraggablePictureBox()
        {
            this.MouseDown += new MouseEventHandler(dragHandler.HandleMouseDown);
            this.MouseMove += new MouseEventHandler(dragHandler.HandleMouseMove);
            this.MouseUp += new MouseEventHandler(dragHandler.HandleMouseUp);
        }
    }
    public class DragHandler
    {
        private bool isDragging = false;
        private Point startPoint;

        public void HandleMouseDown(object sender, MouseEventArgs e)
        {
            isDragging = true;
            startPoint = e.Location;
            
        }

        public void HandleMouseMove(object sender, MouseEventArgs e)
        {
            if (isDragging)
            {
                Control ctrl = sender as Control;
                ctrl.BringToFront();
                ctrl.Left += e.X - startPoint.X;
                ctrl.Top += e.Y - startPoint.Y;
            }
        }

        public void HandleMouseUp(object sender, MouseEventArgs e)
        {
            isDragging = false;
        }
    }
}
これ自体はやりたいことの条件を満たしているのですが,意見をもらっての疑問で、
インタフェースの処理が良いとの意見があって最初はそちらでやろうとしたのですが、調べてもインタフェースの活用がわからなかったので教えてほしいです。