'How to put content of a table in a .txt file in windows forms

this app is designed to take input from the user and put it in a text file, i managed to succed with normal textboxes, but i cant put the content from the table

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

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void perfundo_Click(object sender, EventArgs e)
        {
            
            DateTime d = DateTime.Now;
            string fileName = @$"C:\Users\Agron\Documents\c#\{emritb.Text}-{d.ToString("dd-MM-yyyy}")}.txt";
            
            using (StreamWriter sw = File.CreateText(fileName))
            {
                sw.WriteLine($"{emritb.Text}\n{TVtb.Text}\n{textBox1.Text}\n{Ptb.Text}\n");
                //i need to add the contents of the table to this file
            }

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void TVtb_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged_1(object sender, EventArgs e)
        {

        }

        private void Ptb_TextChanged(object sender, EventArgs e)
        {

        }


        public TextBox AddNewTextBox()
        {
            TextBox txt = new TextBox();
            txt.Width = 570;
            return txt;
        }
        private void Add_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.AutoSize = true;
            tableLayoutPanel1.Controls.Add(AddNewTextBox());
            tableLayoutPanel1.Controls.Add(AddNewTextBox());
            tableLayoutPanel1.Controls.Add(AddNewTextBox());
        }

        private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

Basiclly i need to add everything thats on the table into a .txt file but the table can change by pressing the "Shto" button, this button adds another row with 3 textboxes in the table.



Solution 1:[1]

You can add something like that:

foreach (Control c in tableLayoutPanel1.Controls) 
{
    if (c is TextBox)
    {
        TextBox tb = c as TextBox;
        sw.WriteLine(tb.Text);
    }
}

Each textbox content will be written in a separate line. If you have to write the 3 of them in the same line, you may group the textboxes 3 by 3 in a container (panel for instance) and adapt the code above because you'll have first to enumerate the panels and then the textboxes. You may also create a user control with 3 textboxes in it (to replace the panel).

Solution 2:[2]

Basiclly i need to add everything thats on the table into a .txt file but the table can change by pressing the "Shto" button, this button adds another row with 3 textboxes in the table.

Here's how you'd output each row of data (three textboxes per row of the tablelayoutpanel) to a comma delimited file:

using (StreamWriter sw = File.CreateText(fileName))
{
    for(int row=0; row<tableLayoutPanel1.RowCount;row++)
    {
        sw.WriteLine($"{tableLayoutPanel1.GetControlFromPosition(0, row).Text},{tableLayoutPanel1.GetControlFromPosition(1, row).Text},{tableLayoutPanel1.GetControlFromPosition(2, row).Text}");
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Fredy
Solution 2 Idle_Mind