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?

5
  • Indexes in C# are 0-based; so you should start i at 0. Commented Aug 25, 2014 at 8:08
  • 1. Open form designer file and you can find how it is done. 2. support.microsoft.com/kb/319266 Commented Aug 25, 2014 at 8:09
  • Oh, yeah... I know... Forgot to mention, but the first one (index 0) will be loaded statically at the beginning. But yeah, you are right! Commented Aug 25, 2014 at 8:09
  • @Reniuz: I am not trying to use Windows Forms. I need to do it in WPF Commented Aug 25, 2014 at 8:13
  • Oh yeah I spotted that later, sorry :) Commented Aug 25, 2014 at 8:16

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.