如何将文本框添加到动态创建的图片框

How to add textboxs to the dynamically created pictureboxes

我有一个程序可以在我点击按钮时动态创建可移动的图片框。我需要做一些事情,比如当我点击图片框时,当我可以写这个图片框的描述(名称,......)时,这个点击添加到我动态创建的图片框一个新的文本框。这个文本框应该可以随图片框一起移动。

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;


    namespace WindowsFormsApplication1
    {

        public partial class Form1 : Form
        {
            List<PictureBox> pictureboxes = new List<PictureBox>();

            public Form1()
            {
                InitializeComponent();
            }

            private void AddPictureBox(string imagePath)
            {
                var pb = new PictureBox();
                pb.Name = "picturebox" + pictureboxes.Count;
                pb.Location = new Point(pictureboxes.Count * 100, 100);
                pb.Size = new Size(70, 70);
                pb.BorderStyle = BorderStyle.None;
                pb.SizeMode = PictureBoxSizeMode.StretchImage;
                this.Controls.Add(pb);

                pb.Image = Image.FromFile(imagePath);
                pb.Refresh();
                pb.MouseDown += new MouseEventHandler(picMouseDown);
                pb.MouseMove += new MouseEventHandler(picMouseMove);
                pb.MouseUp += new MouseEventHandler(picMouseUp);

                pictureboxes.Add(pb);

                Invalidate();
            }

            private void router_Click(object sender, EventArgs e)
            {
                AddPictureBox(@"D:\router.jpg");

            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            int x = 0;
            int y = 0;
            bool drag = false;

            private void picMouseDown(object sender, MouseEventArgs e)
            {
                // Get original position of cursor on mousedown
                x = e.X;
                y = e.Y;
                drag = true;
            }

            private void picMouseMove(object sender, MouseEventArgs e)
            {
                if (drag)
                {
                    PictureBox pb = (PictureBox)sender;
                    // Get new position of picture
                    pb.Top += e.Y - y;
                    pb.Left += e.X - x;
                    pb.BringToFront();

                    Invalidate();
                }
            }

            private void picMouseUp(object sender, MouseEventArgs e)
            {
                drag = false;
            }

            private void switch1_Click(object sender, EventArgs e)
            {
                AddPictureBox(@"D:\HP ProBook 450\Desktop\Grafika\switch1.png");

            }

            private void panel1_Paint(object sender, PaintEventArgs e)
            {

            }

            private void pc_Click(object sender, EventArgs e)
            {
                AddPictureBox(@"D:\HP ProBook 450\Desktop\pc.jpg");

            }

            private void server_Click(object sender, EventArgs e)
            {
                AddPictureBox(@"D:\HP ProBook 450\Desktop\server.png");


            }
    }

感谢您的帮助:)。

您可以使用如下代码将 TextBox 添加到 PictureBox

TextBox newTextBox = new TextBox();
newTextBox.Parent = yourPictureBox;
// place it e.g. to the left bottom:
newTextBox.Location = new Point(10, yourPictureBox.Height - newTextBox.Height); 

请注意,这会将 TextBox 添加到 PB 的 Controls 集合中;所以它会位于 PictureBox 顶部 之上;所以,是的,它会随着 PictureBox 一起移动,但它也会隐藏一部分或 PB!

如果您只想将它​​们分组,请将它们都添加到 Panel 之类的内容中,再次将其设置为它们的 Parent

另请注意,您不能在设计器中执行此操作; PictureBox 并不是真的要充当 Container..

PictureBox 是如何创建的并不重要,只要您有对它的引用即可。