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.
Related
I am writing the content of a byte array to both the screen and a text file using the for loops below. The 3rd column in the array relates to attributes and rather than displaying the attribute number I want to add a switch statement that takes the given number and swaps it with text.
foreach (ManagementObject FailData in FailDataSet)
{
Byte[] data =(Byte[])FailData.Properties["VendorSpecific"].Value;
var sb = new StringBuilder();
for (int i = 0; i < data[0] - 1; i++)
{
for (int j = 0; j < 12; j++)
{
richTextBox2.Text = richTextBox2.Text + data[i * 12 + j] + "\t";
sb.Append(data[i * 12 + j] + "\t");
}
richTextBox2.Text = richTextBox2.Text + "\n";
sb.AppendLine();
}
String title = "Unknw\tUnknw\tAttribute\tStatus\tUnknw\tValue\tWorst\tRaw\t\tUnknw\r\n";
File.WriteAllText(#"C:\Users\Desktop\WriteText1.txt", title + sb.ToString();
}
This is one way to do it:
foreach (ManagementObject FailData in FailDataSet)
{
Byte[] data =(Byte[])FailData.Properties["VendorSpecific"].Value;
var sb = new StringBuilder();
for (int i = 0; i < data[0] - 1; i++)
{
for (int j = 0; j < 12; j++)
{
string text = data[i * 12 + j];
if (j == 2)
text = GetText(text);
sb.Append(text + "\t");
}
sb.AppendLine();
}
richTextBox2.Text = sb.ToString();
String title = "Unknw\tUnknw\tAttribute\tStatus\tUnknw\tValue\tWorst\tRaw\t\tUnknw\r\n";
File.WriteAllText(#"C:\Users\Desktop\WriteText1.txt", title + sb.ToString());
}
Note that there is a missing closing parenthesis in your code in the last line.
And the function could be something like this:
private string GetText(string numberText)
{
int number;
if (!int.TryParse(numberText, out number))
return string.Empty; // or "invalid" or whatever
switch (number)
{
case 1: return "one";
case 2: return "two";
default: return "other";
}
}
If I understand correctly, the format of your byte[] is as follows:
[0]: count of rows of data (plus 1, really?)
[1]..[12]: first row of data
[13]..[24]: second row of data
etc.
If so, then the first thing I'll point out is that you have a bug in your program. You are indexing the data starting at the element at index 0, but of course that has the row count. You should be adding 1 to your computed index to account for that.
Also, while it's possible that the row count in the data is actually the number of rows plus 1, requiring the subtraction of 1 for your row counter loop comparison, it seems more plausible that's simply yet another bug in the code.
Taking those things into account, it sounds like what you want is something like this:
foreach (ManagementObject FailData in FailDataSet)
{
Byte[] data =(Byte[])FailData.Properties["VendorSpecific"].Value;
var sb = new StringBuilder();
for (int i = 0; i < data[0]; i++)
{
for (int j = 0; j < 12; j++)
{
int dataIndex = i * 12 + j + 1;
string textForColumn = j == 2 ?
GetAttributeText(data[dataIndex]) : data[dataIndex].ToString();
richTextBox2.Text = richTextBox2.Text + textForColumn + "\t";
sb.Append(textForColumn + "\t");
}
richTextBox2.Text = richTextBox2.Text + "\n";
sb.AppendLine();
}
String title = "Unknw\tUnknw\tAttribute\tStatus\tUnknw\tValue\tWorst\tRaw\t\tUnknw\r\n";
File.WriteAllText(#"C:\Users\Desktop\WriteText1.txt", title + sb.ToString();
}
where:
static string GetAttributeText(byte value)
{
switch (value)
{
case 0:
return "First Attribute Text";
case 1:
return "Second Attribute Text";
case 2:
return "Third Attribute Text";
default:
return "<unknown attribute>";
}
}
Note that opinions vary on the readability of a switch statement. The above could also be achieved using a dictionary:
static readonly Dictionary<byte, string> _byteToAttributeText =
new Dictionary<byte, string>()
{
{ 0, "First Attribute Text" },
{ 1, "Second Attribute Text" },
{ 2, "Third Attribute Text" },
};
static string GetAttributeText(byte value)
{
string text;
if (!_byteToAttributeText.TryGetValue(value, out text))
{
return "<unknown attribute>";
}
return text;
}
I have 10 textboxes, named textBox1, textBox2 to textBox10. I would like to assign string text in each:
textBox1.text = "Stackoverflow1";
textBox2.text = "Stackoverflow2";
...
textBox10.text = "Stackoverflow10";
Is is possible to make it happen in a loop? Something like:
for(int i=1; i<=10; i++)
{
???? = "Stackoverflow" + i.ToString();
}
I was thinking to use a list, but could I possibly adding the "variable" to a list? Say if I create a list:
List<String> Testing = new List<String>();
Testing.add(textBox1.text);
Testing.add(textBox2.text);
...
Testing.add(textBox10.text);
for(int i=1; i<=10; i++)
{
Testing[i-1] = "Stackoverflow" + i.ToString();
}
But this could not work because Testing[] is a string, not a variable in this case. How would I achieve the purpose?
You almost had it. Just put the text boxes themselves in the list, not the strings.
List<TextBox> textBoxes = new List<TextBox>();
textBoxes.Add(textBox1);
textBoxes.Add(textBox2);
textBoxes.Add(textBox3);
// etc.
for (int i = 0; i < textBoxes.Count; ++i)
{
textBoxes[i].Text = "Stackoverflow" + (i + 1).ToString();
}
I want my form to display 1,2,3,4,5
but all it does is replace the text again and again.
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text = Convert.ToString(i);
}
I know it's because of the .Text that it always overrides itself. But how can i leave them in the form so it will display:
1
2
3
4
5
The problem is that in your loop, you're completely replacing the text with each iteration. So the text is left with whatever the last value of i was.
Try putting adding to the current text (with +=) and putting a new line (Environment.NewLine or "\n") between each number:
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text += Environment.NewLine + Convert.ToString(i);
}
Or alternatively, a little Linq can make your life a lot easier:
richTextBox1.Text = string.Join(Environment.NewLine, Enumerable.Range(1, 5));
Try this:
for (int i = 1; i <= 5; i++)
{
richTextBox1.Text += Convert.ToString(i) + Environment.NewLine;
}
Edit:
Just noticed that you want to print one number per line. \n is the NewLine character and will give you a carriage return on the end of the line.
Environment.NewLine is also a good choice, because it will give you the newline character based on the environment the app is running in.
append text, don't just assign it.
richTextBox1.Text += Convert.ToString(i);
this is equivelant of
richTextBox1.Text = richTextBox1.Text + Convert.ToString(i);
richTextBox1.AppendText(i.ToString()+Environment.NewLine);
This will also do:
for (int i = 1; i <= 5; i++)
{
richTextBox1.AppendText(i + Environment.NewLine);
}
I want to read a text file in order to build map.
For example I have this map:
0#0000000
0#0000000
0#0000000
000000000
000000000
000000000
000000000
I know I should use this:
StreamReader reader = new StreamReader(Application.StartupPath+#"/TestMap.MMAP");
string line = reader.ReadToEnd();
reader.Close();
Now, for example, I want read line 2 char "#". how can i do this?
please help me.
Solved:
Thank you (#L.B AND #user861114), at last my problem was solved:
string[,] item = new string[9, 7];
string[] line = File.ReadAllLines(Application.StartupPath + #"/TestMap.MMAP");
for (int j = 0; j < 7; j++)
{
for (int i = 0; i < 9; i++)
{
item[i, j] = line[j].Substring(i, 1);
Console.WriteLine(i + " " + j + "=" + item[i, j]);
}
}
string[] lines = File.ReadAllLines(your path);
then you can access
char ch = lines[1][1]; //second line's second char
I think it's little bit easy :
string[] strs = string.split(myString, "\n"); // split to array of string by delimiter endline
char[] chars = strs[1].ToCharArray(); // you can get second char "#"
I'm trying to create a loop to avoid copy pasting these lines 30 times.
The names are:
sum1 to sum30
br1txt1 to br30txt1
br1txt2 to br30txt2
//decimal sum30 = decimal.Parse(br30txt1.Text) + decimal.Parse(br30txt2.Text);
//sumTxt30.Text = sum30.ToString();
But the error I'm getting is that the textbox array seems to try to put the value of the textbox not the text box refrenc it self in to the array, how should I fix this?
private void sumX()
{
TextBox[] sumTextboxNames;
sumTextboxNames = new TextBox[29];
for (int i = 1; i < 31; i++)
{
if (sumTextboxNames[0] == null)
{
int y = 0;
foreach (Control c in this.Controls)
{
if (c is TextBox && c.Name.StartsWith("sum"))
{
sumTextboxNames[y] = (TextBox)c;
y++;
}
}
}
else
{
}
string1 = "br" + i + "txt" + 1 + ".Text";
string2 = "br" + i + "txt" + 2 + ".Text";
string3 = "sumTxt" + i + ".Text";
sum = decimal.Parse(string1) + decimal.Parse(string2);
int x = i - 1;
sumTextboxNames[x].Text = sum.ToString();
}
}
The following lines won't work at all:
string1 = "br" + i + "txt" + 1 + ".Text";
string2 = "br" + i + "txt" + 2 + ".Text";
As 1 and 2 are not strings and can not be concatenated to a string. That should give a compiler error right away.
In the following line, you're trying to add up the names of the text boxes as numbers - won't work, the names contain non-number characters.
sum = decimal.Parse(string1) + decimal.Parse(string2);
Anyway, you don't need to use the TextBox array at all. What you could do is:
for (int i = 1; i <= 30; i++)
{
TextBox s = (TextBox)this.Controls[<Name for the S-Textbox>];
TextBox b1 = (TextBox)this.Controls[<Name for the first sum textbox>];
TextBox b2 = (TextBox)this.Controls[<Name for the second sum textbox>];
s.Text = Decimal.Parse(b1.Text) + Decimal.Parse(b2.Text);
}
EDIT
Sorry, quoted wrong line from OP's source code.
EDIT 2
Forgot to cast to TextBox - this is required of course... Thanks for pointing it out, everybody.
Thorsten Dittmar's answers is the way you should go.
However, with respect to this code:
foreach (Control c in this.Controls)
{
if (c is TextBox && c.Name.StartsWith("sum"))
{
sumTextboxNames[y] = (TextBox)c;
y++;
}
}
You should try a solution that uses LINQ.
For example
TextBox [] sumTextBoxes = (from t in this.Controls.Cast<Control>
where t is TextBox
&& t.Name.StartsWith("sum")
select t).Cast<TextBox>().ToArray();
Thanks to Thorsten this is what I ended up with:
string string1;
string string2;
string string3;
private void sumX()
{
for (int i = 1; i < 31; i++)
{
string1 = "br" + i + "txt" + '1';
string2 = "br" + i + "txt" + '2';
string3 = "sumTxt" + i;
TextBox s = (TextBox)this.Controls[string3];
TextBox b1 = (TextBox)this.Controls[string1];
TextBox b2 = (TextBox)this.Controls[string2];
decimal sum = Decimal.Parse(b1.Text) + Decimal.Parse(b2.Text);
s.Text = sum.ToString();
}