Home > Back-end >  How to write all textboxes content to text file using streamwritter?
How to write all textboxes content to text file using streamwritter?

Time:10-23

I am a civil engineer who is learning to code (so please don't judge). I am using streamwriter to write textboxes data to txt file but I have hundreds of textboxes. Writing texboxes name one by one is making me crazy and database is not an option. I searched internet for more than 3 hours now but couldn't find the appropriate solution. The following code works fine but there must be a way to avoid writing all textboxes names. I would really appreciate any guideline here on how to write all textboxes content in text file?.

        SaveFileDialog saveFileDialog1 = new SaveFileDialog();

        //saveFileDialog1.InitialDirectory = @"C:\";

        saveFileDialog1.Title = "Save Project Files";

        saveFileDialog1.DefaultExt = "txt";
        saveFileDialog1.FileName = "1";

        saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

        saveFileDialog1.FilterIndex = 2;

        saveFileDialog1.RestoreDirectory = true;

        string pdfPath = "";

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            using(StreamWriter writer = new StreamWriter(saveFileDialog1.FileName))
            {
                writer.WriteLine(Tb_ProjectName.Text);
                writer.WriteLine(tb_ClientName.Text);
                writer.WriteLine(tb_ProjectPic.Text);
                writer.WriteLine(tb_AgencyName.Text);
                writer.WriteLine(tb_AgencyLogo.Text);
                writer.WriteLine(tb_ContractorName.Text);
                writer.WriteLine(tb_ContractorLogo.Text);
            }

            pdfPath = saveFileDialog1.FileName;
            
            

        }

CodePudding user response:

David already gave the correct answer in his comment. So here written out with an additional tip:

In your code, you can iterate over all available text boxes and write their content into a file. There are two thing you have to consider. First, in which order should all textboxes be written. Second, are there any text boxes that should NOT be written into the file.

For both cases you should use the visual editor to set of all text boxes the TabIndex property to set the order in which the content should be written and you can use the property Tag to contain something to mark a text box for not being written into the file.

The code sketch would look something like this:

using(StreamWriter writer = new StreamWriter(saveFileDialog1.FileName))
{
    var textBoxesToWrite = this.Controls
        .OfType<TextBox>()
        .Where(textBox => textBox.Tag != null)
        .OrderBy(textBox => textBox.TabIndex)

    foreach (var textBox in textBoxesToWrite)
    {
        writer.WriteLine(textBox.Text);
    }
}

CodePudding user response:

1: Create a panel and put all your textboxes in it.

2: Use a foreach loop to get all textboxes from this panel:

string allTextBoxContent = string.Empty;
foreach (var control in panel1.Controls)
{
    if (control is TextBox textbox)
    {
         allTextBoxContent  = textbox.Text;
    }
}
  • Related