dynamically create multiple textboxes C#

The array creation call just initializes the elements to null. You need to individually create them.

TextBox[] txtTeamNames = new TextBox[teams];
for (int i = 0; i < txtTeamNames.Length; i++) {
  var txt = new TextBox();
  txtTeamNames[i] = txt;
  txt.Name = name;
  txt.Text = name;
  txt.Location = new Point(172, 32 + (i * 28));
  txt.Visible = true;
}

Note: As several people have pointed out in order for this code to be meaningful you will need to add each TextBox to a parent Control. eg this.Controls.Add(txt).


You need to initialize your textbox at the start of the loop.

You also need to use a for loop instead of a foreach.


You need to new up your TextBoxes:

for (int i = 0; i < teams; i++)
{
    txtTeamNames[i] = new TextBox();
    ...
}

You are doing it wrong, you have to add textbox instances to the array, and then add it to the form. This is how you should do it.

public void createTxtTeamNames()
        {
            TextBox[] txtTeamNames = new TextBox[10];

for (int u = 0; u < txtTeamNames.Count(); u++)
            {
                txtTeamNames[u] = new TextBox();
            }
            int i = 0;
            foreach (TextBox txt in txtTeamNames)
            {
                string name = "TeamNumber" + i.ToString();

                txt.Name = name;
                txt.Text = name;
                txt.Location = new Point(0, 32 + (i * 28));
                txt.Visible = true;
                this.Controls.Add(txt);
                i++;
            }
        }

    private void button2_Click(object sender, EventArgs e)
    {
        TextBox tb = new TextBox();
        tb.Name = abc;
        tb.Text = "" + i;

        Point p = new Point(20 + i, 30 * i);
        tb.Location = p;
        this.Controls.Add(tb);
        i++;
    }


    private void button3_Click(object sender, EventArgs e)
    {
        foreach (TextBox item in this.Controls.OfType<TextBox>())
        {
            MessageBox.Show(item.Name + ": " + item.Text + "\\n");   
        }
    }

Comments

  1. Alvin

    • 2018/4/15

    The array creation call just initializes the elements to null . You need to individually create them. TextBox[] txtTeamNames = new 

  2. Wells

    • 2017/11/5

    public class TextInput : Form { public TextBox TxtBox { get; private set; } public Control Container { get; private set; } public TextInput(Control c, int a, int b, int c, int d, string e) { this.Container = c; this.TxtBox = new TextBox(); var txtBox1 = this.TxtBox; txtBox1.Visible = true; c.Controls.Add(txtBox1); txtBox1.Focus(); txtBox1.Size = new Size(a, b); txtBox1.Location = new Point(c, d); txtBox1.Text = (e).ToString(); txtBox1.Visible = true; } }

  3. Santoro

    • 2019/7/1

    t[i].ID = "TestBox" + i.ToString(); declare that textbox object outside the button click event. Now try to access the textbox using their ID.

  4. Apollo

    • 2015/6/4

    To add multiple controls as you asked, use a for loop: for (int i = 0; i < 2; ) { TextBox textBox = new TextBox(); textBox.Text = "Hi"; textBox.Name = "textBox" + i.ToString(); form2.Controls.Add(textBox); } But the controls (textboxes) overlaps. You need to organize their locations. EDIT: E.g

  5. Palumbo

    • 2020/2/1

    Text; MessageBox.Show("The value of count just before the loop is " + count.ToString()); for (int i = 3; i <= count; i++) { com.

  6. Smith

    • 2019/10/11

    1. Add CSS and JavaScript file references I need to add a CSS and jQuery reference to create a tab on a page: 2. Create a UI for entering the number of TextBoxes you want to generate, also create the tabs I use a table for creating a TextBox and a Button. The following figure shows the structure table: And after creating the table for entering

  7. Landen

    • 2016/8/4

    Dynamically Generate Multiple TextBoxes in ASP.Net · <body> · <form id="form1" runat="server"> · <table> · <tr> · <td> · <asp:Label ID="lbltextno" 

  8. Kingston

    • 2017/1/31

    // Declaring the variable with limit int n= 1; for (int i= 0;i<n;i++) { TextBox MyTextBox= new TextBox(); // Assigning the textbox ID name MyTextBox.ID = " txtb_" + i; MyTextBox.Width = 540; MyTextBox.Height = 60; MyTextBox.TextMode = TextBoxMode.MultiLine; this.Controls.Add(MyTextBox); }

  9. Khalil

    • 2016/1/29

    Vb Code to Create Multiple Textboxes · SqlCommand(sQuery) With cmd .Connection = con con.Open() cmd. · ExecuteNonQuery() con.Close() addStudents = "Success" End 

  10. Edgar

    • 2020/4/19

    dynamically add multiple textBoxes in the form using C#. Discussion in 'C#' started by rick001, Jan 7, 2013. No, create an account now. Yes, my password is:

  11. Preston

    • 2018/9/29

    this is my code:: TextBox[] tbArray = new TextBox[] { TextBox1, What exactly you want?? to create textboxes dynamically or to just show 

  12. Oscar

    • 2020/10/20

    TextBox txtName = new TextBox(); txtName.Width = 16; txtName.ID = "txtboxname" + colIndex; txtName.AutoPostBack = true; e.Row.Cells[colIndex].Controls.Add(txtName); TextBox txtName1 = new TextBox(); txtName1.Width = 16; txtName1.BackColor = System.Drawing.Color.LightBlue; txtName1.ID = " txtboxname1 " + colIndex;

  13. Durand

    • 2020/6/18

    HelloI have copied coding from your website for adding textbox dynamically as seen below But it is create only 1 textbox I want to create multiple textboxes 

  14. Wood

    • 2021/7/7

    function InputData(text) { let self = this; self.text = text; } function InputViewModel() { let self = this; // Your array of HTML inputs self.inputs = ko.observableArray([new InputData("Default value")]); self.output = ko.observable(); self.addInput = function() { // Dynamically adds new input on user click button "Add" self.inputs.push(new InputData("")); }; self.proceed = function() { // Process all input with their values for (var i = 0; i < self.inputs().length; i++) { console.log(self

  15. Royce

    • 2019/9/2

    HiI want to add and romove entire row containing two textboxes and save each row data as a single record in sql server databaseI am attaching screenshot of 

  16. Kobe

    • 2020/5/11

    Answer #1: The array creation call just initializes the elements to null . You need to individually create them. Note: As several people have pointed out in 

  17. Ace

    • 2018/5/12

    For creating ASP.Net Textbox Control I have created a function and called the function page load event. I have used for loop to create multiple Textbox but 

Comments are closed.

Recent Posts