0

I am trying to write inputs from different text boxes into one string. I have written the following code to create the array:

private void btnSave_Click(object sender, EventArgs e)
        {
            contents[0] = txtName.Text;
            contents[1] = txtSurname.Text;
            contents[2] = dtpDOB.Text;
            contents[3] = lblGenUsername.Text;
            contents[4] = lblPassword.Text;
        }

I want the final output to be saved onto a notepad in the following format when the data is received from the inputs:

Tim Forest 25-03-1999 u12345678 password

  • 4
    Have you tried anything? Seems like instead of an array it would be easier to just concatenate the string together, or use [`String.Format`...](https://stackoverflow.com/q/10512349/215552) – Heretic Monkey Mar 10 '20 at 14:40
  • 1
    What have you tried so far? What issue do you face? Please also have a look on [How to ask](https://stackoverflow.com/help/how-to-ask) and give us a minimal code example that reproduces your issue – rekcul Mar 10 '20 at 14:40
  • Take a look at the following code snippet, in my opinion is quite similar to what you are trying to achieve. https://www.codeproject.com/Questions/467339/How-to-save-Textbox-data-in-a-file – vasilisdmr Mar 10 '20 at 14:42

2 Answers2

0

This should solve your problem:

private void btnSave_Click(object sender, EventArgs e)
{
    string text = txtName.Text 
     + " " + txtName.Text
     + " " + txtSurname.Text
     + " " + dtpDOB.Text
     + " " + lblGenUsername.Text
     + " " + lblPassword.Text;
    File.WriteAllText("C:\\myFile.txt", text);
}
0

If you just need the exact strings within the text boxes concatenating is the way to go.

 string text = txtName.Text 
 + " " + txtName.Text 
 + " " + txtSurname.Text
 + " " + dtpDOB.Text
 + "u" + lblGenUsername.Text
 + " " + lblPassword.Text;

to add the U and have it predefined you would have to add it before the username.

@Guillaume Ladeuille

Keron Tzul
  • 73
  • 4