C# Write each array element to different textbox on form - c#

Hi all hoping for some direction. I have a text file that I am reading into a C# Form as a simple string array. I want to take each element and put them into their own text box on the form. I know I can do it as
textBox1.Text = myArray[0];
textBox2.Text = myArray[1];
and so forth but I was hoping for some sort of for or foreach statement to iterate through the array and textboxes.
For reference the form has 50 boxes on it and the array will always have 50 elements.
Thanks in advance!
Update, I appreciate both comments it got me pointed in the right direction. I was really struggling to think through it. What I ended up coming up with is:
//Read Data
string[] lines = File.ReadAllLines(Globals.savePath);
//Write Data To Form
for (int i = 1; i <= 20; i++)
{
TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
textBox.Text = lines[i-1];
}
for (int i = 21; i <= 40; i++)
{
TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
for (int i = 41; i <= 60; i++)
{
TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
for (int i = 61; i <= 80; i++)
{
TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
textBox.Text = lines[i - 1];
}
I am sure there is a cleaner way to do this. But this worked for me and was much better then doing the whole
textbox1.Text = myArray[0]
Plus it had the added benefit of translating over when to the next part where I was editing text boxes and than writing back to the text file.
string[] lines = new string [81];
for (int i = 1; i <= 20; i++)
{
TextBox textBox = (TextBox)groupBox1.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 21; i <= 40; i++)
{
TextBox textBox = (TextBox)groupBox2.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 41; i <= 60; i++)
{
TextBox textBox = (TextBox)groupBox3.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
for (int i = 61; i <= 80; i++)
{
TextBox textBox = (TextBox)groupBox4.Controls["textBox" + i];
lines[i - 1] = textBox.Text;
}
lines[81] = "End";
File.Delete(Globals.savePath);
File.WriteAllLines(Globals.savePath, lines);
Once again Thank You to those that commented!

This is my first attempt at an answer, so fingers crossed this is helpful. I would store the textboxes in a list, then when you loop through your array, just do something like the following:
var maxItems = myArray.Length;
for(int loop = 0; loop < maxItems; loop++)
{
textBoxItemList[loop].Text = myArray[loop];
}
This could be tweaked to determine the loop size using the smallest array size, so you didn't go out of bounds. The maxItems is there just as an example of where you would set this up.
Hope that helps.

Related

C# 2 dimensional array output to textbox

Trying to get a simple 2 dimensional array to display to a text box. Output should look like this:
Student Class House
Jack Math Oxford
Bender Chem Trent
All previous attempts have failed and I am not sure how to output the code.
String[,] text = {
{ "Student", "Class", "House" },
{ "Jack", "Math", "Oxford" },
{ "Bender", "Chem", "Trent" } };
//string textString;
for (int i = 0; i < text.GetUpperBound(0); i++)
{
string first = text[i, 0];
string middle = text[i, 1];
string last = text[i, 2];
TextBox1.Text = first;
TextBox1.Text = middle;
TextBox1.Text = last;
}
You need to concatenate the text onto your strings and pass the strings to the texbox only once, after you're all done. You'll want to add in line breaks to have the data go over several lines as well.
Something like this (untested):
String[,] text = {
{ "Student", "Class", "House" },
{ "Jack", "Math", "Oxford" },
{ "Bender", "Chem", "Trent" } };
string output = '';
//string textString;
for (int i = 0; i < text.GetUpperBound(0); i++)
{
output += text[i, 0] + text[i, 1] + text[i, 2] + Environment.NewLine;
}
TextBox1.Text = output;
At the moment you're just adding things to the textbox and then overwriting them on the next loop.
so i think you problem that you are using textbox try textarea.
the reason is that textbox only take only line but textare you can have multiple lines and you are overwriting the text of the textbox every time by doing this TextBox1.Text = first;
TextBox1.Text = middle;
TextBox1.Text = last;
so try this better
TextBox1.Text += first + " " + middle+" " + last;
If both dimensions are dynamic, you can use a second loop nested it in the one you have. Each loop will be going through one dimension:
for (int i = 0; i < text.GetLength(0); i++)
{
for (int j = 0; j < text.GetLength(1); j++)
{
TextBox1Text += text[i, j] + "\t";
}
TextBox1.Text += "\r\n";
}
Note: I used tab \t to separate the columns and new line \r\n to separate the rows. However, this format will not be visible in a textbox, you'll need to use a textarea. Here is a demo.

Listbox not removing values in c# GUI

The user basically enters a number of hex values into a textbox separated by commas eg. AA,1B,FF. These are then displayed in a listbox box. if the number of hex values in the textbox exceeds the size to transfer defined by the user, the listbox only displays the this number of values or if the size to transfer is bigger that adds zero values to the listbox.
this works fine until you enter a value with a zero in front of it such as AA,BB,CC,DD,EE,0F, if sizeToTransfer = 2, the listbox should display 0xAA and 0xBB. but instead it only removes the 0F value?
I'm pretty new to programming so it may be something obvious I'm missing any help would be appreciated.
private void WriteSPI1_Click(object sender, EventArgs e)
{
string hexstring = textbox1.Text;
HexValues.Items.Clear();
string[] hexarray = hexstring.Split((",\r\n".ToCharArray()), StringSplitOptions.RemoveEmptyEntries);
byte[] hexbytes = new byte[hexarray.Length];
uint num = Convert.ToUInt32(hexarray.Length);
for (int j = 0; j < hexarray.Length; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
if (hexarray.Length > sizeToTransfer)
{
diff = num - sizeToTransfer;
for (i = 0; i < diff+1; i++)
{
HexValues.Items.Remove("0x" + x);
}
}
else
{
diff = sizeToTransfer - num;
for (i = 0; i < diff; i++)
{
HexValues.Items.Add("0x00");
}
}
}
CHANGE
for (int j = 0; j < sizeToTransfer; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
WITH
for (int j = 0; j < hexarray.Length; j++)
{
hexbytes[j] = Convert.ToByte(hexarray[j], 16);
Hexlist.Add(hexbytes[j]);
writebuff = Hexlist.ToArray();
x = writebuff[j].ToString("X2");
HexValues.Items.Add("0x" + x);
}
and remove the if stantment that follow

Split the content of textbox into respective individual textboxes at run time in c#?

I want the text contents of a TextBox to split into respective individual Textbox carrying a single character using c# windows application form.
Eg : A single Textbox containing text as-[Orange]
Expected output:-
in 20 separate textboxes
[o][r][a][n][g][e][][][][][][][][][][][][][][]
Till now I have done this..
`string name = textBox1.Text;
string str = name;
int chunkSize = 1;
Int32 stringLength = str.Length;
for (int i = 0; i < stringLength; i += chunkSize)
{
TextBox txtbox = new TextBox();
//if (i + chunkSize > stringLength)
//chunkSize = stringLength - i;
string singlechar = str.Substring(i, chunkSize);
txtbox.TextAlign = HorizontalAlignment.Center;
txtbox.BorderStyle = BorderStyle.FixedSingle;
txtbox.Font = new Font(txtbox.Font, FontStyle.Bold);
txtbox.Text = singlechar;
//txtbox.MaxLength = 1;
int a = 30;
int x = (i + 10) * a;
txtbox.Text = txtbox.Text.ToUpper();
txtbox.Location = new System.Drawing.Point(x, 100);
txtbox.BackColor = Color.White;
txtbox.Size = new System.Drawing.Size(30, 20);
this.Controls.Add(txtbox);
}`
I think you want this:-
textBox1 = mainTextBox.Text[0] ;
textBox2 = mainTextBox.Text[1] ;
// and so on..
assuming other TextBoxs in Panel named panel1 you can write something like :
for (int i = 0; i < textBox1.Text.Length; i++)
((TextBox)panel1.Controls[i]).Text = textBox1.Text[i].ToString();

from dataGridView to TextBox, take headername

I have a question about converting from dataGridView to textbox. I want when see x replace it with the Headername
I have dataGridview here:
Name A B C D ...
kiki x x ...
lola x ...
maja x x x ...
I want to have in my textbox:
kiki A,C ...
lola B ...
maja A,C,D ...
My code would look like this i think;
string abc = "";
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 1; i < dataGridView1.Columns.Count - 1; i++)
{
if(dataGridView1.Rows[i].Cells[j].Value.ToString() != "x")
abc = dataGridView1.Columns[j].HeaderText.ToString();
richTextBox1.Text += dataGridView1.Rows[i].Cells["Name"].Value.ToString() + " " + abc;
//or something like that, can some1 give me a key?
}
}
Can someone help me with it? don't know how to do it, please give me any ideas
This should work for you. this code is putting everything into one multiline richTextBoxand each row is add in a new line:
for (int i = 0; i < dataGridView1.Rows.Count-1; i++)
{
string text = dataGridView1.Rows[i].Cells["Name"].Value.ToString() + " " ;
bool added = false;
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value.ToString() == "x")
{
text += dataGridView1.Columns[j].HeaderText.ToString() + ",";
if (!added)
added = true;
}
}
if(added)
{
text = text.Remove(text.Length - 1); //to remove ',' at the end
}
richTextBox1.Text += text + Environment.NewLine; //add to richTextbox1 with newline
}

how to update in to a 2D array?

im tunning this loop and what to populate the array with the output of my methods, im not sure about that last part "array2DB[i,i] =" how shold i do this.
updated loop based on replyes
private void BackGroundLoop()
{
for (int i = 1; i < 31; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}
I'm not 100% sure what you want to do, but you want probably this instead of your last line:
array2DB[0, i - 1] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1, i - 1] = int.Parse(UpdateFormClass.nyeSaker.ToString());
-1 in index is needed, because arrays are indexed from 0 in .NET.
This is the most you can do, without running into exception:
int[,] array2DB = new int[2, 30];
for (int i = 0; i < 30; i++)
{
string txbName = "br" + i + "txt" + '3';
TextBox txtBCont1 = (TextBox)this.Controls[txbName];
string string1 = txtBCont1.Text.ToString();
UpdateFormClass.runUserQuery(string1);
array2DB[0,i] = int.Parse(UpdateFormClass.gamleSaker.ToString());
array2DB[1,i] = int.Parse(UpdateFormClass. nyeSaker.ToString());
}
Note that you can't have array2DB[2, *] or above because it will generate an arrayoutofbound exception.
You have to use two for loops. One for each the x and y axis of the array.
for (int i = 0; i < 2; i++){
for (int j = 0; j < 30; j++)
{
....
array2DB[i,j] = int.Parse(UpdateFormClass.gamleSaker.ToString())
, int.Parse(UpdateFormClass.nyeSaker.ToString());
}
}

Categories