winfrom 动态添加控件,以及删除

时间:2023-03-09 16:18:54
winfrom 动态添加控件,以及删除

winfrom 动态添加控件,以及删除

winfrom 动态添加控件,以及删除

  private void btnadd_Click(object sender, EventArgs e)
        {
            int fileCount = 0;
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c is TextBox)
                {
                    ++fileCount;
                }
            }
            if (fileCount >= 4)
            {
                MessageBox.Show("最多添加5张图!");
                return;
            }
            string controlMark = Guid.NewGuid().ToString();
            TextBox t1 = new TextBox();
            t1.Name = "txt_" + controlMark;
            t1.Click += new EventHandler(btnClickEvent);
            t1.Width = 300;
            flowLayoutPanel1.Controls.Add(t1);             Button btnDel = new Button();
            btnDel.Name = "del_" + controlMark;
            btnDel.Text = "删除";
            btnDel.Click += new EventHandler(delFileControl);
            flowLayoutPanel1.Controls.Add(btnDel);
        }
///点击删除按钮删除生成的控件
        public void delFileControl(object sender, EventArgs e)
        {
            Button btnAction = sender as Button;
            string id = btnAction.Name.Split('_')[1];
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c.Name == "txt_" + id)
                {
                    flowLayoutPanel1.Controls.Remove(c);
                }
            }
            foreach (Control c in this.flowLayoutPanel1.Controls)
            {
                if (c.Name == "del_" + id)
                {
                    flowLayoutPanel1.Controls.Remove(c);
                }
            }
            pictureBox1.Image = null;
        }
        public void btnClickEvent(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            TextBox txtAction = sender as TextBox;
            OpenFileDialog openFileDialogTemp = new OpenFileDialog();
            DialogResult dr = openFileDialogTemp.ShowDialog();
            if (dr == DialogResult.OK)
            {
                string id = txtAction.Name.Split('_')[1];
                foreach (Control c in this.flowLayoutPanel1.Controls)
                {
                    if (c.Name == "txt_" + id)
                    {
                        c.Text = openFileDialogTemp.FileName;
                        pictureBox1.Image = Image.FromFile(openFileDialogTemp.FileName);
                    }                 }
            }
        } ///保存
 foreach (Control c in this.flowLayoutPanel1.Controls)
                    {
                        if (c is TextBox)
                        {
                            if (File.Exists(c.Text.Trim()))
                            {
                                string time = DateTime.Now.ToString("yyyy-MM");
                                if (!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + @"File\" + time + @"\"))
                                {
                                    Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + @"File\" + time + @"\");
                                }
                                string FileName = time + @"\" + Guid.NewGuid().ToString() + ".jpg";
                                string tempFileName = AppDomain.CurrentDomain.BaseDirectory + @"File\" + FileName;
                                File.Copy(c.Text.Trim(), tempFileName);
                            }
                        }                     }