You could do something like this
public void CreateTextBoxes(StackPanel target, int number)
{
if (target.Children.Count < number)
{
for (int i=target.Children.Count; i< number; i++)
{
TextBox tx = new TextBox;
target.Children.Add(tx);
}
}
else
{
while (target.Children.Count > number)
{
target.Children.RemoveAt(target.Children.Count - 1);
}
}
}