Computer Guy
asked on
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:
I'm lost!
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
}
I'm lost!
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++) {
...
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]);
}
}
}
...
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.
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();
}
}
}
ASKER
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
My ultimate goal is to create one file for each array index.
int[] quan = new int[3];
quan[0] = 52; //int.Parse(number1TextBox
quan[1] = 98; //int.Parse(number2TextBox
quan[2] = 28; //int.Parse(number3TextBox
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
ASKER
and have a unique file name for each round
ASKER
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
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");
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi,
That worked! Thanks.
Also, is there a way add in an alt tag based on the array as well?
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";
For the array that you have shown just change the line 27 in my earlier comment as below:
that should work, if not post your question
writer.Write("<img src=" + "photos/" + fileName[i]+photoNum + ".jpg' alt='" + altTag[i] + "' style='width:304px;height:228px;'>");
that should work, if not post your question
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.
Open in new window
The code could be improved but is this close to what you are looking for?