Showing posts with label Dynamically add textboxs in Windows forms Click Button Catch that text box values. Show all posts
Showing posts with label Dynamically add textboxs in Windows forms Click Button Catch that text box values. Show all posts

Thursday, June 19, 2014

Dynamically add textboxs in Windows forms Click Button Catch that text box values



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Linq;
using System.Drawing;


namespace asdf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
             
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Label label = new Label();
            int count = panel1.Controls.OfType<Label>().ToList().Count;
            label.Location = new Point(10, (25 * count) + 2);
            label.Size = new Size(40, 20);
            label.Name = "label_" + (count + 1);
            label.Text = "label " + (count + 1);
            panel1.Controls.Add(label);

            TextBox textbox = new TextBox();
            count = panel1.Controls.OfType<TextBox>().ToList().Count;
            textbox.Location = new System.Drawing.Point(60, 25 * count);
            textbox.Size = new System.Drawing.Size(80, 20);
            textbox.Name = "textbox_" + (count + 1);
           // textbox.TextChanged += new System.EventHandler(this.TextBox_Changed);
            panel1.Controls.Add(textbox);

            Button button = new Button();
            count = panel1.Controls.OfType<Button>().ToList().Count;
            button.Location = new System.Drawing.Point(150, 25 * count);
            button.Size = new System.Drawing.Size(60, 20);
            button.Name = "button_" + (count + 1);
            button.Text = "Button " + (count + 1);
            button.Click += new System.EventHandler(this.Button_Click);
            panel1.Controls.Add(button);


        }
        private void Button_Click(object sender, EventArgs e)
        {
            Button button = (sender as Button);

            TextBox rc = (TextBox)FindDynamicControlByName("textbox_1");
            if (rc != null)
            {
                MessageBox.Show("hai");
            }

        }
        private Control FindDynamicControlByName(string controlName)
        {
            return Controls.Find(controlName, true).FirstOrDefault();
        }

    }
}