Search code examples
c#layoutgridwindows-store-apps

generating textboxes in a Windows Store app


I'm trying to generate textboxes in a Windows store app using a button so that each new textbox generated (after recupering its value from a textBox) sits bellow the previous one

I've tried with

private void addBtn(object sender, RoutedEventArgs {
   int t = 100;
   TextBlock myTextBlock = new TextBlock();
   myGrid.Children.Add(myTextBox);
   myTextBlock.Text = taskTb.Text;
   myTextBlock.Width = 300;
   myTextBlock.Height = 300;
   myTextBlock.FontSize = 25;
   myTextBox.Margin = new Thickness(20,t,100,100);
   t = t + 100;
}

But it didn't work, what I am missing here?


Solution

  • The root cause of the error is that every time you are putting a new TextBox at exactly the same position.

    The variable t is always 0 at the time you are adding a new TextBox, it would not increment as you expect, you should declare t as a field of the class, not as a temporary variable inside the method.

    Or try StackPanel, let the StackPanel arranges (vertically stacks) the TextBoxes for you.