demonslayer1
demonslayer1

Reputation: 859

How to create a new Text Box once a Button is clicked ?

I want to write a program which will create a new TextBox, once Button3 is clicked. For some reason C# doesn't recognize txtRun. It says the name txtRun does not exist in the current context. Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text += "a";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text += "b";
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

          txtRun = new TextBox();
          txtRun.Name = "txtDynamic";
          txtRun.Location = new System.Drawing.Point(20, 18);
          txtRun.Size = new System.Drawing.Size(200, 25);
          // Add the textbox control to the form's control collection         
              this.Controls.Add(txtRun);
      }

        }



        }
    }
}

Upvotes: 1

Views: 7255

Answers (5)

VhsPiceros
VhsPiceros

Reputation: 426

try with

  var txtRun = new TextBox();
  txtRun.Name = "txtDynamicTemp";
  txtRun.Location = new System.Drawing.Point(20, 18);
  txtRun.Size = new System.Drawing.Size(200, 25);
  // Add the textbox control to the form's control collection         
      this.Controls.Add(txtRun);

Upvotes: 2

CodeCamper
CodeCamper

Reputation: 6980

Question:

For some reason C# doesn't recognize txtRun. It says the name txtRun does not exist in the current context.

Answer:

In your statement

txtRun = new TextBox();

You have created a variable txtRun but have not given it a type. Just like when you create a string or an int TextBox is no exception you must use the name of the class TextBox before txtRun as such

TextBox txtRun = new TextBox();

You could also replace the word TextBox with var and let the compiler guess for you.

This will resolve your immediate question.

However the real issue here is that you are creating an infinite number of TextBoxes if the user were to click the Button an infinite number of times. Also you may not realize it but the second time you press the button it places a TextBox underneath the original so you may not realize it because the text you type in it will remain but I assure you that you are creating a bunch of TextBoxes which is no good. I am not sure exactly what you are accomplishing depending on your scenario you can approach this a couple different ways.

Scenario 1(You want only 1 dynamically created TextBox)

Create a variable to check if the TextBox has already been created so it only makes one. *note you should really create or call the TextBox outside of your click method somehow because you will lose scope of it.

Scenario 2(You want to create more than 1 TextBox dynamically)

Create a TextBox list or array so it is easier to track your dynamically created TextBoxes Using something such as the FlowLayoutPanel or TableLayoutPanel to handle placing your TextBoxes especially if you want to make something like a grid of them or insure somehow the location is unique for each new TextBox. *note you still should really create or call the TextBox outside of your click method somehow because you will lose scope of it.

If you need please tell me and I will go into more detail of whatever the case may be.

Upvotes: 0

lb.
lb.

Reputation: 5776

In addition to the answers given and looking at the code snipped you've provided, it appears you've got too many closing curly braces. More specifically the last two - you don't need those.

Upvotes: 0

VhsPiceros
VhsPiceros

Reputation: 426

Maybe the problem is you not declare the type

var txtRun = new TextBox();

Upvotes: 2

shf301
shf301

Reputation: 31394

You need to declare a variable in C# before using it.

Use either

TextBox txtRun = new TextBox();

or use an implicitly typed variable:

var txtRun = new TextBox();

Upvotes: 3

Related Questions