0

I want to add a Textfield to my Form in C#, when I press a button.

####################
|   ______         |
|  [__ADD_]        |
|__________________|

####################
|   ______         |
|  |  TF  |        |
|  |______|        |
|   ______         |
|  [__ADD_]        |
|__________________|

This is, how my Program mainly should look, before and after pressing the Button. Right now I'm approaching the Problem like this:

public partial class MainWindow : Window
{
    int i = 0;
    TextBox[] t = new TextBox[80];
    Button[] b = new Button[80];

    public MainWindow()
    {
        InitializeComponent(); 
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        t[i] = new TextBox();
        b[i] = new Button();
        i++;
    }

}

So actually, I'm allready creating the Fields, but I can't show them.

How do I show them?

Saggex
  • 3,390
  • 3
  • 22
  • 37

1 Answers1

1

When you create your textboxes, you have to set their coordinates(x,y) and dimensions(width,height) or, if you are using a grid or stackpanel add them.

t[i] = new TextBox();
t[i].Text = "new textbox";
t[i].textBox2.Name = "textBox1";

Grid1.Children.Add(t[i]);
//or SomeStackPanel.Children.Add(t[i]);

in this case i added the textbox to a grid, but you can add them also to a stackpanel, etc, etc.

Marco Cadei
  • 145
  • 2
  • 4
  • 14