Reputation: 25
I added 89 textboxes dynamically. I want to get the value of the textboxes to xml. I can add the textboxes just fine the problem is I cant get the values on those dynamically added textboxes.
For example i want to get value from textBox1 to the node "F1" in XML ,from textBox2 to node "F2" in XML.
private void button1_Click(object sender, EventArgs e)
{
XmlNodeList xnList;
XmlDocument doc = new XmlDocument();
string dosyayolu = Application.StartupPath + "\\coupling.xml";
doc.Load(dosyayolu);
if (globaller.hangimenu == "TWT1")
{
xnList = doc.SelectNodes("/coup/TWT1");
}
else
{
xnList = doc.SelectNodes("/coup/TWT2");
}
for (int i = 0; i < 89; i++)
{
foreach (XmlNode xn in xnList)
{
xn["F" + (i + 1).ToString()].InnerText = "k";
// xn["F1"].InnerText = textBox1.Text;
}
}
doc.Save(dosyayolu);
}
Upvotes: 1
Views: 270
Reputation: 1372
you can do it like this:
//create
StackPanel sp = new StackPanel();
for(int i=0;i<89;i++)
{
TextBox tb = new TextBox();
sp.Children.Add(tb);
}
//get
foreach(TextBox tb in sp.Children)
{
}
or you can add all the textboxs to a list,and get them from list by index
Upvotes: 0
Reputation: 1649
i'm guessing your textboxes are of name textBox1, textBox2, .... also your xml nodes start on F1 instead of F2 so i altered the For loop a bit
for (int i = 1; i < 90; i++)
{
foreach (XmlNode xn in xnList)
{
// this if textboxes on form, yourUserControlName if it is under a usercontrol
var tb = (TextBox)this.Controls["textBox" + i];
xn["F" + i].InnerText = tb.Text;
}
}
Edited in favor of comment of Hassan Nisar
Upvotes: 1
Reputation: 4487
You could use the extension ChildrenOfType<T>()
.
Assuming that grid
is the parent of all your TextBoxes..
var textBoxes= grid.ChildrenOfType<TextBox>().ToArray();
for (int i = 0; i < 89; i++)
{
foreach (XmlNode xn in xnList)
{
xn["F" + (i + 1).ToString()].InnerText = "k";
xn["F1"].InnerText = textBoxes[ i ].Text;
}
}
Upvotes: 1