I have created a function which add a row in tablelayoutpanel and textbox in column of the row. After adding few rows, i want to read the value of particular textbox in particular row. How can it be done. Remember one thing that name of all textbox is same in all row.

Here is the code snippit.

private void addtextbox(int x,int j)
        {
            int rowIndex = AddTableRow();

            this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 20F));
          
               Label lbl = new Label();
            TextBox Text1 = new TextBox();
            TextBox Text2 = new TextBox();
            TextBox Text3 = new TextBox();
            TextBox Text4 = new TextBox();

           lbl.Text =  rowIndex.ToString();
            tableLayoutPanel1.Controls.Add(lbl, 0, rowIndex);
            tableLayoutPanel1.Controls.Add(Text1, 1, rowIndex);
            tableLayoutPanel1.Controls.Add(Text2, 2, rowIndex);
            tableLayoutPanel1.Controls.Add(Text3, 3, rowIndex);
            tableLayoutPanel1.Controls.Add(Text4, 4, rowIndex);
            //MessageBox.Show(tableLayoutPanel1.Controls. );
            
        }

        private int AddTableRow()
        {
            int index = this.tableLayoutPanel1.RowCount++;
            RowStyle style = new RowStyle(SizeType.AutoSize);
            tableLayoutPanel1.RowStyles.Add(style);
            //detailTable.RowStyles.Add(style);
            return index;
        }
Re: Dynamically adding a row and textbox in a row in tablelayoutpanel 80 80

give each textBox a name and try to use the Controls.Fine method to access the textBox you want.

Re: Dynamically adding a row and textbox in a row in tablelayoutpanel 80 80

I tried this

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

        private void TB_TextChanged(object sender, EventArgs e)
        {
            TextBox TB = sender as TextBox; // get control that triggered event
            // with knowledge of total rows and cols of tablelayout you should
            // be able to calculate r and c index positions from index
            int index = this.tableLayoutPanel1.Controls.GetChildIndex(TB);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // made form dropped a tablelayout on it (standard 2 rows 2 cols
            //and did this
            TextBox Text1 = new TextBox(); 
            TextBox Text2 = new TextBox(); 
            TextBox Text3 = new TextBox();
            TextBox Text4 = new TextBox();
            // or any event you like eg Click etc.
            Text1.TextChanged += new System.EventHandler(this.TB_TextChanged);
            Text2.TextChanged += new System.EventHandler(this.TB_TextChanged);
            Text3.TextChanged += new System.EventHandler(this.TB_TextChanged);
            Text4.TextChanged += new System.EventHandler(this.TB_TextChanged);
            // add controls to panel
            int rowIndex = 0;
            this.tableLayoutPanel1.Controls.Add(Text1, 0, rowIndex); 
            this.tableLayoutPanel1.Controls.Add(Text2, 1, rowIndex);
            rowIndex = 1;
            this.tableLayoutPanel1.Controls.Add(Text3, 0, rowIndex); 
            this.tableLayoutPanel1.Controls.Add(Text4, 1, rowIndex);
        }
    }
}
Re: Dynamically adding a row and textbox in a row in tablelayoutpanel 80 80

or create it a bitr shorter:

int rowIndex = 0;
            int columnIndex = 0;
            for (int i = 0; i < 4; i++)
            {
                TextBox Text = new TextBox();
                Text.Name = "text+" + i + 1;
                Text.TextChanged += new System.EventHandler(this.TB_TextChanged);
                if (i % 2 == 0 && i > 0)
                    rowIndex++;
                if (i % 2 != 0 && i > 0)
                    columnIndex++;
                else
                    columnIndex = 0;
               this.tableLayoutPanel1.Controls.Add(Text, columnIndex, rowIndex);                
            }
Re: Dynamically adding a row and textbox in a row in tablelayoutpanel 80 80
Re: Dynamically adding a row and textbox in a row in tablelayoutpanel 80 80

@ddanbe
Above code is creating dynamic text box.
Suppose these text box are being created on button click event.
I click this button three times to get 4*3=12 textboxs and some text is entered in the first textbox.Now i want to display message with text written in the first textbox. then how we will refer thix first textbox?

Re: Dynamically adding a row and textbox in a row in tablelayoutpanel 80 80

Try this, add a button and implement Click event:

private void button1_Click(object sender, EventArgs e)
        {
            // this will put the text "test1" in the upper left textbox
            // will not always work, 
            // but if you sure you have labels or texboxes, go ahead
            this.tableLayoutPanel1.Controls[0].Text = "test1";
            // refer to these to calculate other indexes
            // see code of Mitja
            int rows = this.tableLayoutPanel1.RowCount;
            int cols = this.tableLayoutPanel1.ColumnCount;
        }
Re: Dynamically adding a row and textbox in a row in tablelayoutpanel 80 80

Thanks
I was looking for this line .... thanks a lot...


this.tableLayoutPanel1.Controls[0].Text = "test1";

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.