Hey! I am trying to put some data from a textbox to an existing array.
Basically when I double click a node, the program will split the information from a text file to an array, parts[], and search for the relevant data to display in the textboxes.
Say the users make changes in the textboxes, I would like to read the information and put it back into parts[] and join back parts[] to a string and save it back to the original text file. (My code follows below.) When I run it, an error says Index was outside the bounds of the array. Did I do something wrong?
[edited] Here the full code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Field[] fieldArray = new Field[100];
private string[] parts;
public Form1()
{
InitializeComponent();
}
private void populateTree(string path, TreeNode tv1)
{
string[] dir = Directory.GetDirectories(path);
foreach (string d in dir)
{
string entry = Path.GetFileName(d);
TreeNode t = tv1.Nodes.Add("Folder", entry, 0);
populateTree(d, t);
}
string[] files = Directory.GetFiles(path);
foreach (string f in files)
{
string entry = Path.GetFileName(f);
tv1.Nodes.Add(f, entry, 1);
}
}
private void Form1_Load(object sender, EventArgs e)
{
//populate the tree
TreeNode t = treeView1.Nodes.Add("Units");
populateTree(@"..\units\", t);
for (int i = 0; i < 100; i++)
{
fieldArray[i] = new Field();
}
fieldArray[0].label = new Label();
fieldArray[0].label.AutoSize = true;
fieldArray[0].label.Location = new System.Drawing.Point(323, 9);
fieldArray[0].label.Name = "Programtittle";
fieldArray[0].label.Text = "UAI UnitDef Editor";
this.Controls.Add(fieldArray[0].label);
fieldArray[0].save = new Button();
fieldArray[0].save.Location = new System.Drawing.Point(549, 404);
fieldArray[0].save.Name = "Save";
fieldArray[0].save.Size = new System.Drawing.Size(75, 23);
fieldArray[0].save.Text = "Save";
fieldArray[0].save.UseVisualStyleBackColor = true;
this.Controls.Add(fieldArray[0].save);
this.fieldArray[0].save.Click += new System.EventHandler(this.Save_Click);
int clabel = 36;
fieldArray[1].varName = new string[] { "unitname", "name", "buildCostEnergy", }; //define labels
//popluate label
for (int i = 0; i < fieldArray[i].varName.Length; i++)
{
fieldArray[1].label = new Label();
fieldArray[1].label.AutoSize = true;
fieldArray[1].label.Location = new System.Drawing.Point(323, clabel);
fieldArray[1].label.Name = "label";
this.Controls.Add(fieldArray[1].label);
fieldArray[1].label.Text = fieldArray[1].varName[i];
clabel = clabel + 26;
}
//populate textbox
int cbox = 33;
for (int i = 0; i < fieldArray[i].varName.Length; i++)
{
fieldArray[i].txtBox = new TextBox();
fieldArray[i].txtBox.Location = new System.Drawing.Point(410, cbox);
fieldArray[i].txtBox.Name = "txtBox";
fieldArray[i].txtBox.Size = new System.Drawing.Size(100, 50);
this.Controls.Add(fieldArray[i].txtBox);
cbox = cbox + 26;
}
}
private void populateLabelTxtBox(string path)
{
//f.txtBox.Multiline = true; //added for testing purpose;
//read,split file
string text = System.IO.File.ReadAllText(path);
char[] delimiters = new char[] { '{', '=', ';', '}' };
parts = text.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
//fieldArray[1].varName = new string[] { "unitname", "name", "buildCostEnergy", };
//display info in textbox
int a;
int strNumber;
int strIndex = 0;
for (a = 0; a < fieldArray[1].varName.Length; a++)
{
for (strNumber = 0; strNumber < parts.Length; strNumber++)
{
strIndex = parts[strNumber].IndexOf(fieldArray[1].varName[a]);
if (strIndex >= 0)
break;
}
strNumber = strNumber + 1;
fieldArray[a].join = new int[]{strNumber};
fieldArray[a].txtBox.Text = parts[strNumber];
}
}
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (treeView1.SelectedNode.Name != "Folder")
{
string text = System.IO.File.ReadAllText(treeView1.SelectedNode.Name);
//f.txtBox.Text = text;
populateLabelTxtBox(treeView1.SelectedNode.Name);
}
}
private void Save_Click(object sender, EventArgs e)
{
//fieldArray[0].txtBox.Text = "Happy"; //on click save happy is display testing
//join[a] > holds number where item is taken
//parts[] store split text
for (int a = 0; a < fieldArray[1].varName.Length; a++)
{
parts[fieldArray[a].join[a]] = fieldArray[a].txtBox.Text;
}
//join parts[] up with . as connector
string combine = string.Join(".", parts);
fieldArray[0].txtBox.Text = combine;
}
}
}
there is also the code of the fields:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
class Field
{
public string[] varName;
public TextBox txtBox;
public Label label;
public ToolTip tip;
public Button save;
public int[] join;
public Field()
{
varName= new string[3];
txtBox = null;
label = null;
tip = null;
save = null;
join = new int[3];
}
}
}
fieldArray[1].varName.Length? Is there any guarantee thatfieldArraywill be the same length, or thatfieldArray[a].join[a]will be valid? What about the length ofpartscompared withfieldArray[1].varName.Length?