C# Array Question

Hello,

I have 3 textbox's that will take a number (i.e. tb1: 52. tb2: 98, tb3: 28)
I want to assign the value to an array
I want to create a loop write a line the number of times in the text box tb1, 52 times.

Line Number: 01
Line Number: 02.... (52 times)

This is what I have:

int[] quan = new int[2];
            quan[0] = int.Parse(number1TextBox.Text);
            quan[1] = int.Parse(number2TextBox.Text);
            quan[2] = int.Parse(number3TextBox.Text);

           while (quan[0] < number1TextBox.Text)
{
console.writeLine("The quantity of textbox x is: quan[0]; //this line needs to get written 52 times and will change
            }

Open in new window


I'm lost!
LVL 3
Computer GuyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ol muserTechnology GeneralistCommented:
Not sure why you are writing to the console when you have a GUI with text boxes.

Anyways, during execution of the while loop, do you expect the value of the text box to change? If so you need to handle that event.

Assuming that is not the case, going by your comment "//this line needs to get written 52 times and will change", the while loop should be like this.

        int i = 0;
        while (i < quan[0])
        {
            Console.WriteLine("Printing " + i + "th iteration of " + quan[0] + " from textbox1");
            i++;
        }

Open in new window

The code could be improved but is this close to what you are looking for?
0
Ryan ChongCommented:
or a simple loop will do, like:
int[] quan = new int[3];
            quan[0] = 52; //int.Parse(number1TextBox.Text);
            quan[1] = 98; //int.Parse(number2TextBox.Text);
            quan[2] = 28; //int.Parse(number3TextBox.Text);
            
for(int i = 0;i<quan[0];i++) {
...

Open in new window

if you need to do a nested loop, you can try like:
...
    for(int j = 0;j<quan[1];j++) {
        for(int k = 0;k<quan[2];k++) {
            Console.WriteLine("i="+(i+1) + ", j="+(j+1)+", k="+(k+1)+"The quantity of textbox x is: "+quan[0]);
        }
    }
}
...

Open in new window

0
Daniel Van Der WerkenIndependent ConsultantCommented:
There are two ways to do what you want to do. One will fill in the array ordered as q[0]  -> TextBox1, and q[1] -> TextBox2, etc.

The other will fill in them unordered and somewhat randomly, as in q[0] -> TextBox23, q[1] -> TextBox5, etc.

Here's the code for both ways. Note I simply created a WindowsForm application with four text boxes labeled TextBox1, TextBox2, TextBox3, and TextBox4. I filled them in and then have a button which gets the values.

Now, you can have as many textboxes as you want and there is no hardcoding of the textbox name. You just increment the number, ToString() it, and you have the values.

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;

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

            textBox1.Text = "5";
            textBox2.Text = "6";
            textBox3.Text = "99";
            textBox4.Text = "1001";
        }

        private void getTextBoxInfo()
        {
            Form1 f1 = new Form1();
            const int NUM_TEXTBOX_CONTROLS = 4;

            int[] qInt = new int[NUM_TEXTBOX_CONTROLS];
            int qIndex = 0;

            // Fill in ordered
            for (int i = 0; i < NUM_TEXTBOX_CONTROLS; i++)
            {
                string txtBoxName = "textBox" + (i + 1).ToString();
                Control[] cArray = f1.Controls.Find(txtBoxName, true);

                if (cArray != null && cArray.Count() > 0)
                {
                    if (cArray[0] is TextBox)
                    {
                        TextBox cTextBox = (TextBox)cArray[0];
                        int iControl = int.MinValue;
                        if (int.TryParse(cTextBox.Text, out iControl))
                        {
                            qInt[i] = iControl;
                        }
                        else
                        {
                            qInt[i] = 0;
                        }
                    }
                }
            }

            // Fill in unordered
            Control.ControlCollection cc = f1.Controls;
            if (cc != null && cc.Count > 1)
            {
                foreach (Control c in cc)
                {
                    if (c is TextBox)
                    {
                        TextBox tb = (TextBox)c;
                        int iText = int.MinValue;
                        if (int.TryParse(tb.Text, out iText))
                        {
                            qInt[qIndex] = iText;
                        }
                        else
                        {
                            qInt[qIndex] = 0;
                        }
                        qIndex++;
                    }
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            getTextBoxInfo();
        }
    }
}

Open in new window

0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Computer GuyAuthor Commented:
At the end of the loop, how to I advance to the next array [1]

My ultimate goal is to create one file for each array index.

int[] quan = new int[3];
            quan[0] = 52; //int.Parse(number1TextBox.Text);
            quan[1] = 98; //int.Parse(number2TextBox.Text);
            quan[2] = 28; //int.Parse(number3TextBox.Text);
           
for(int i = 0;i<quan[0];i++) {
Make file 1 but run the loop 52 times
for(int i = 0;i<quan[1];i++) {
Make file 2 but run the loop 98 times

and so one
0
Computer GuyAuthor Commented:
and have a unique file name for each round
0
Computer GuyAuthor Commented:
Ok, here is a better description of my code:

To clarify, I have 5 text boxes that take in a number.
I need to create 5 unique files.

Inside each file, a loop will run (x times per the textbox input) to generate a gallery with path and photo number

 int[] links = new int[5];
            links[0] = int.Parse(1TextBox.Text);
            links[1] = int.Parse(2TextBox.Text);
            links[2] = int.Parse(3TextBox.Text);
            links[3] = int.Parse(4TextBox.Text);
            links[4] = int.Parse(5TextBox.Text);


            string[] fileName = new String[5];
            fileName[0] = "one";
            fileName[1] = "two";
            fileName[2] = "three";
            fileName[3] = "four";
            fileName[4] = "five";

            int numLinks = 0;
            int ii = 0;

            for( int i = 0; 1 < fileName.Length; i++)
            {
                StreamWriter writer = new StreamWriter("C:\\csharp\\" + fileName[i] + ".html");
                writer.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
                while (count < links[ii])
                {
                    string photoNum = startingPhotoNumber.ToString("D3");
					writer.Write("<img src=" + "photos/" + fileName[i]+photoNum + ".jpg' alt='Mountain View' style='width:304px;height:228px;'>");
                    count++;
                    ++startingPhotoNumber;
                }
                writer.Write("</div>");
                writer.Close();
                i++;
                MessageBox.Show("File Created");
            }

Open in new window

1
ol muserTechnology GeneralistCommented:
Most of your problem is in the nested loops. Pls note the lines I have modified and/or commented.

             int[] links = new int[5];
            links[0] = 2; //int.Parse(1TextBox.Text);
            links[1] = 3; //int.Parse(2TextBox.Text);
            links[2] = 3; //int.Parse(3TextBox.Text);
            links[3] = 2; //int.Parse(4TextBox.Text);
            links[4] = 3; //int.Parse(5TextBox.Text);


            string[] fileName = new String[5];
            fileName[0] = "one";
            fileName[1] = "two";
            fileName[2] = "three";
            fileName[3] = "four";
            fileName[4] = "five";

            //int numLinks = 0;
            //int ii = 0;

            for( int i = 0; i < fileName.Length; i++)
            {
                StreamWriter writer = new StreamWriter("C:\\csharp\\" + fileName[i] + ".html");
                writer.Write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
				int count = 0; //running count of links
                while (count < links[i])
                {
                    string photoNum = links[i].ToString(); //startingPhotoNumber.ToString("D3");
					writer.Write("<img src=" + "photos/" + fileName[i]+photoNum + ".jpg' alt='Mountain View' style='width:304px;height:228px;'>");
                    count++;
                    //++startingPhotoNumber;
                }
                writer.Write("</div>");
                writer.Close();
                //i++;
                MessageBox.Show("File Created");
            }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Computer GuyAuthor Commented:
Hi,

That worked! Thanks.

Also, is there a way add in an alt tag based on the array as well?

            string[] altTag = new String[5];
            altTag[0] = "1";
            altTag[1] = "2";
            altTag[2] = "3";
            altTag[3] = "4";
            altTag[4] = "5";

Open in new window

0
ol muserTechnology GeneralistCommented:
For the array that you have shown just change the line 27 in my earlier comment as below:

writer.Write("<img src=" + "photos/" + fileName[i]+photoNum + ".jpg' alt='" + altTag[i] + "' style='width:304px;height:228px;'>");

Open in new window


that should work, if not post your question
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.