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

时间:2015-01-04 12:13:07

标签: c# textbox

我有一个程序,当我点击按钮时动态创建可移动的图片框。我需要做点什么,比如当我点击图片框时,当我可以写这个图片框(name,...)的描述时,这个点击会为我动态创建的图片框添加一个新的文本框。此文本框应该能够与picturebox一起移动。

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");


            }
    }

感谢您的帮助:)。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码向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

另请注意,您无法在Designer中执行此操作; PictureBox并非真正意味着充当Container ..

PictureBox的创建方式并不重要,只要你有参考它。