Hello, I’m trying to figure out if it’s possible to create a textBox array where-in I can save & load contents to/from an external .XML file.
Let me preface, I’m a novice. Programming is something I only dabble in periodically so my understanding of terminologies isn’t great, my code isn’t the best, and written examples would be appreciated if alternative solutions are a objectively better route.
To example what I’m trying to do, here is an example of what I have working:
callingFunction
{
xml("Load")
}
private void xml(string input)
{
int i = 0
string[] export = { "tb1", "tb2", "tb3", "tb4" }
XmlDocument xmlDoc = new XmlDocument();
if (input == "Load")
{
xmlDoc.Load(./xml/settings.xml);
XmlNodeList functionNodes = xmlDoc.SelectNodes("//settings/setting");
foreach (XmlNode functionNode in functionNodes)
{
if (i == 0) { textBox1.Text = functionNode.Attributes["tb1"].Value; }
else if (i == 1) { textBox2.Text = functionNode.Attributes["tb2"].Value; }
else if (i == 2) { textBox3.Text = functionNode.Attributes["tb3"].Value; }
else if (i == 3) { textBox4.Text = functionNode.Attributes["tb4"].Value; }
i++
}
}
else if (input == "Save")
{
XmlNode rootNode = xmlDoc.CreateElement("settings");
xmlDoc.AppendChild(rootNode);
foreach (string text in export)
{
XmlNode functionNode = xmlDoc.CreateElement("setting");
XmlAttribute attribute = xmlDoc.CreateAttribute(text);
if (i == 0) { attribute.Value = textBox1.Text; }
else if (i == 1) { attribute.Value = textBox2.Text; }
else if (i == 2) { attribute.Value = textBox3.Text; }
else if (i == 3) { attribute.Value = textBox4.Text; }
i++
}
xmlDoc.Save(./xml/settings.xml);
}
}
Hopefully I wrote all of that out correctly.
So as I said this does work for both loading a profile and saving a profile but if I could I’d really like to step through the textBoxes like an array. For example:
foreach (XmlNode functionNode in functionNodes)
{
textBox[i].Text = functionNode.Attributes["tb" + i].Value;
i++
}
I did find some information I thought would aid me but I can’t figure out if I’m just wasting my time with it or if I’m writing it wrong:
TextBox[] textBox = { textBox1, textBox2, textBox3, textBox4 }
Then going into the foreach loop but this just throws a “Object reference not set to an instance of an object.” error.
I’m lost as to if what I’m trying to do here is even possible. I view it as trying to create a reverse array. Instead of an array of data values to assign to form objects I’m trying to create a array of form objects to push/pull data values to/from.
All thoughts and inputs are appreciated. I’ll take criticism so long as it’s constructive but I’ll need it to be explained in layman’s terms. I won’t understand talking in code.