I'm trying to make a button that saves Multiple text boxes in to 10 lines and 16 characters per line(only found stuff for VB).Also just to make sure for my clear button would I put:

textBoxX1.Clear();

Edited 6 Years Ago by GAME: n/a

>I'm trying to make a button that saves Multiple text
>boxes in to 10 lines and 16 characters per line

Be specific about the number and contents of each text box. Are we talking ten text boxes of up to 16 characters?

>only found stuff for VB
VB can be converted to C# with minimal effort.

>I'm trying to make a button that saves Multiple text
>boxes in to 10 lines and 16 characters per line

Be specific about the number and contents of each text box. Are we talking ten text boxes of up to 16 characters?

>only found stuff for VB
VB can be converted to C# with minimal effort.

160 Text boxes, 1 character per text box. 10 collums and 16 rows

>160 Text boxes, 1 character per text box.
That's...interesting.

>10 collums and 16 rows
Alrighty, it's not terribly difficult. Assuming you're pulling the text boxes from the top level of the form:

StringBuilder sb = new StringBuilder();
int n = 0;

foreach (Control ctrl in this.Controls) {
  if (ctrl is TextBox) {
    sb.Append(ctrl.Text);

    if (++n == 16) {
      sb.Append(Environment.NewLine);
      n = 0;
    }
  }
}

Edited 6 Years Ago by Narue: n/a

>160 Text boxes, 1 character per text box.
That's...interesting.

>10 collums and 16 rows
Alrighty, it's not terribly difficult. Assuming you're pulling the text boxes from the top level of the form:

StringBuilder sb = new StringBuilder();
int n = 0;

foreach (Control ctrl in this.Controls) {
  if (ctrl is TextBox) {
    sb.Append(ctrl.Text);

Would I put that for my save button? I'm confused.

    if (++n == 16) {
      sb.Append(Environment.NewLine);
      n = 0;
    }
  }
}

Where would I put the code? Would I put it for the save button?

[IMG]http://image.beyluxe.com/pictures/76a041fe4973e6d7588b423477bdf205.jpg [/IMG]

Edited 6 Years Ago by GAME: n/a

>Where would I put the code?
Dude, do some thinking for yourself. I gave you the algorithm you needed, I'm not going to glue it into your code, debug the whole thing, and put a bow on it for you.

Ok, how would I be able to get this code to work.

{
     SaveFileDialog SaveFile = new SaveFileDialog();
     SaveFile.FileName = "";
     SaveFile.Filter = "Text Files (*.txt)|*.txt";
     SaveFile.Title = "Save";
     SaveFile.ShowDialog();
     try {
       System.IO.StreamWriter Write = new System.IO.StreamWriter(SaveFile.FileName);
         Write.Write(textBoxX1.Text,  textBoxX2, textBoxX3, textBoxX4, textBoxX5, textBoxX4, textBoxX4, textBoxX5, textBoxX6, textBoxX7, textBoxX8, textBoxX9, textBoxX10, textBoxX11, textBoxX12, textBoxX13, textBoxX14, textBoxX15, textBoxX16, textBoxX17, textBoxX18, textBoxX19, textBoxX20, textBoxX21, textBoxX22, textBoxX23, textBoxX25, textBoxX26, textBoxX27, textBoxX28, textBoxX29, textBoxX30, textBoxX31, textBoxX32, textBoxX33, textBoxX34, textBoxX35, textBoxX36, textBoxX37, textBoxX38, textBoxX39, textBoxX40, textBoxX41, textBoxX42, textBoxX43, textBoxX44, textBoxX45, textBoxX46, textBoxX47, textBoxX48, textBoxX49, textBoxX50, textBoxX51, textBoxX52, textBoxX53, textBoxX54, textBoxX55, textBoxX56, textBoxX57, textBoxX58, textBoxX59, textBoxX60, textBoxX61, textBoxX62, textBoxX63, textBoxX64, textBoxX65, textBoxX66, textBoxX67, textBoxX68, textBoxX69, textBoxX70, textBoxX71, textBoxX72, textBoxX73, textBoxX74, textBoxX75, textBoxX76, textBoxX77, textBoxX78, textBoxX79, textBoxX80, textBoxX81, textBoxX82, textBoxX83, textBoxX84, textBoxX85, textBoxX86, textBoxX87, textBoxX88, textBoxX89, textBoxX90, textBoxX91, textBoxX92, textBoxX93, textBoxX94, textBoxX95, textBoxX96, textBoxX97, textBoxX98, textBoxX99, textBoxX100, textBoxX101, textBoxX102, textBoxX103, textBoxX104, textBoxX105, textBoxX106, textBoxX107, textBoxX108, textBoxX109, textBoxX110, textBoxX111, textBoxX112, textBoxX113, textBoxX114, textBoxX115, textBoxX116, textBoxX117, textBoxX118, textBoxX119, textBoxX120, textBoxX121, textBoxX122, textBoxX123, textBoxX124, textBoxX125, textBoxX126, textBoxX127, textBoxX128, textBoxX129, textBoxX130, textBoxX131, textBoxX132, textBoxX133, textBoxX134, textBoxX135, textBoxX136, textBoxX137, textBoxX138, textBoxX139, textBoxX140, textBoxX141, textBoxX142, textBoxX143, textBoxX144, textBoxX145, textBoxX146, textBoxX147, textBoxX148, textBoxX149, textBoxX150, textBoxX151, textBoxX152, textBoxX153, textBoxX154, textBoxX155, textBoxX156, textBoxX157, textBoxX158, textBoxX159, textBoxX160);
        Write.Close();
     }
     catch (Exception ex) {
        
     }
 }

>Where would I put the code?
Dude, do some thinking for yourself. I gave you the algorithm you needed, I'm not going to glue it into your code, debug the whole thing, and put a bow on it for you.

Ok, I got it to work but it only copies my first text box. Any suggestions?

>Any suggestions?
I have two suggestions:

  1. Oh my god, don't manually write out 160 text boxes! Every class derived from Control has a Controls property that lists out all of the controls contained by that control. If you have a form and 160 text boxes directly on that form, you can loop through formName.Controls to get the text boxes (like in my example).
  2. StreamWriter.Write isn't magic. On top of being the wrong overload, how are you going to tell Write to add a newline every 16 characters? Please read the documentation on methods before trying to use them. At least then you'll have a chance at getting it right.
Comments
Good suggestions
Wouldn't answer my question.

Why can't you put all your textboxes in a list? Like this:

List<TextBox> MyTBList = new List<TextBox>();

Look up what you can do with a List, you will be suprised!

To expand on what Narue has provided, here's a way to capture the information and format it and then clear the textboxes. Uses Linq, extension methods, etc.

var controls = this.Controls.Cast<Control>();
            string[] texts = (from control in controls
                              where control is TextBox
                              select control.Text).ToArray();

            StringBuilder builder = new StringBuilder();
            int columns = 10;
            int rows = 16;
            for (int i = 0; i < rows; i++)
            {
                builder.Append(string.Join(string.Empty, texts, i * columns, columns));
                builder.Append("\n");
            }

            // clear out textboxes
            controls.Where(control => control is TextBox).ToList().ForEach(tb => tb.Text = string.Empty);
This article has been dead for over six months. Start a new discussion instead.