I am developing one app. In this I want to get the data from text boxes which is added dynamically. Below is my code
for (int i = 0; i < result.Count; i++)
{
TextBox TxtBoxU = new TextBox() { Width = 30 };
TextBox TxtBoxE = new TextBox() { Width = 20 };
RadioButton radioButton1 = new RadioButton();
RadioButton radioButton2 = new RadioButton();
TextBlock tb1 = new TextBlock();
TextBlock tb2 = new TextBlock();
TextBlock tb3 = new TextBlock();
TxtBoxU.Name = "TextBoxU" + i.ToString();
TxtBoxE.Name = "TextBoxE" + i.ToString();
tb1.FontSize = 20;
tb2.FontSize = 20;
tb3.FontSize = 20;
tb1.Name = "tb1" + i.ToString();
tb2.Name = "tb2" + i.ToString();
tb3.Name = "tb3" + i.ToString();
tb1.Text = "name " + (i + 1).ToString() + " : ";
tb2.Text = "Age : ";
tb3.Text = "Gender : ";
radioButton1.Content = "Male";
radioButton1.GroupName = "Gender";
radioButton2.Content = "Female";
radioButton2.GroupName = "Gender";
MyStackPanel.Children.Add(tb1);
MyStackPanel.Children.Add(TxtBoxU);
MyStackPanel.Children.Add(tb2);
MyStackPanel.Children.Add(TxtBoxE);
MyStackPanel.Children.Add(tb3);
MyStackPanel.Children.Add(radioButton1);
MyStackPanel.Children.Add(radioButton2);
}
In the above code I want to get the text from TxtBoxU1 ,TxtBoxE1,TxtBoxU2,TxtBoxE2,......so on
how will I get the data from textboxes
Thanks in advance
You can just iterate through the MyStackPanel.Children
as it's a regular collection (implements IEnumerable).
foreach (var child in MyStackPanel.Children)
{
if (!(child is TextBox))
{
continue;
}
var textbox = child as TextBox;
if (textbox.Name == "TextBoxU1")
{
var text = textbox.Text; //whatever you want to do here
}
}
However I'd recommend you to read about DataBinding and MVVM pattern.