2 Replies - 636 Views - Last Post: 11 March 2015 - 09:02 AM Rate Topic: -----

#1 n00bc0d3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-February 15

Cannot put multiple textBox.Text into a 2d array

Posted 09 March 2015 - 04:11 PM

Hello once again all. I have a question that I hope someone can help me with. I have been on this for literally 4 weeks and just cant go on without help. I have been trying to take ten user input from multiple textBox fields and put them into a 2d array. My problem is that it only saves the last user entry. If there this way is not possible or too difficult can someone please recommend an alternative. I feel like i drove my self crazy over this. Thanks in advance.

Here is a snippet of my code.

namespace UserData
    public partial class Form1 : Form
    {   string[][] dataInfo = new string[10][];
        protected int count = 0;
        public Form1()

        private void Form1_Load(object sender, EventArgs e)

        private void button1_Click(object sender, EventArgs e)
            Form2 f2 = new Form2();

            for (int i = 0; i < 10; i++)
                dataInfo[i] = new string[10];

            //get 3 users into into 2d array
            for (int i = 0; i < 3; i++)
                dataInfo[i][0] = textBox1.Text;
                dataInfo[i][1] = textBox2.Text;
                dataInfo[i][2] = textBox3.Text;
                dataInfo[i][3] = comboBox1.Text;
                dataInfo[i][4] = textBox4.Text;
                dataInfo[i][5] = textBox5.Text;
                dataInfo[i][6] = textBox6.Text;
                dataInfo[i][7] = textBox7.Text;
                dataInfo[i][8] = textBox8.Text;
                dataInfo[i][9] = textBox9.Text;

                   // f2.Show();

                    if (count == 3)
                        Form3 f3 = new Form3(dataInfo);
                        MessageBox.Show("That is 10 Records.");

                    // reset the data fields after every entry in the first form1
                    textBox1.Text = String.Empty;
                    textBox2.Text = String.Empty;
                    textBox3.Text = String.Empty;
                    comboBox1.Text = String.Empty;
                    textBox4.Text = String.Empty;
                    textBox5.Text = String.Empty;
                    textBox6.Text = String.Empty;
                    textBox7.Text = String.Empty;
                    textBox8.Text = String.Empty;
                    textBox9.Text = String.Empty;


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot put multiple textBox.Text into a 2d array

#2 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Cannot put multiple textBox.Text into a 2d array

Posted 09 March 2015 - 06:11 PM

just a quick glance at your logic driving your for loop.

You enter your loop
you assign the array a value thats in your text box
you clear your text box

then you loop again
you assign the array a value thats in your now EMPTY text box
you clear your text box


that doesn't seem right

This post has been edited by Damage: 09 March 2015 - 06:13 PM

Was This Post Helpful? 1
  • +
  • -

#3 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon

Reputation: 5104
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Cannot put multiple textBox.Text into a 2d array

Posted 11 March 2015 - 09:02 AM

What are you using a multi-dim array for?

Most of the time, when people use that in C#, it means that they don't understand the concept of classes. There are real use cases, but most of them involve matrix math and stuff like that.

If you're just doing it to store correlated data, you'd be much, much better off making a class, and using an array of that class.

Now, as to what the problem with your code is...Damage is correct; you're wiping out your textboxes, then immediately looping again. My guess is that you meant to allow a user to input a second (and third) round of data. You can't use a loop for that. You will have to keep count of what user they are entering, fill in the data, then end the method; don't loop and start again, because you'll just be writing garbage to the next records.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1