-1

I've to create three textboxes when clicked on Add button. Initially I tried to generate a single textbox but can not do the same. I took help from this forum as well as , http://csharp.net-informations.com/gui/dynamic-controls-cs.htm.

I can not see any textboxes when clicked on Add button.

namespace DataDashBoard.UI
{
    public partial class DataForm : Form
    {
        int cLeft = 1;

        public DataForm()
        {
            InitializeComponent();
        }


        public TextBox AddNewTextBox()
        {
            TextBox txt = new TextBox();
            this.Controls.Add(txt);
            txt.Top = cLeft * 25;
            txt.Left = 100;
            txt.Text = "TextBox " + this.cLeft.ToString();
            cLeft = cLeft + 1;
            return txt;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewTextBox();
        }
    }
}

Please help!!!

DeanOC
  • 7,142
  • 6
  • 42
  • 56
user4221591
  • 2,084
  • 7
  • 34
  • 68

2 Answers2

1

Did you associate the button event handler with the button click event? You can do this by selecting the button in the form designer and then showing the events in the properties panel.

If you copied the code from the page you linked without creating the association between the button and the event handler the button will do nothing.

Peter Dongan
  • 1,762
  • 12
  • 17
0

Make sure your btnAdd_Click event handler is called.

Place a breakpoint in it and start your app in debug mode.

Cheers

Luc Morin
  • 5,302
  • 20
  • 39
  • yes, I debug my code. The execution goes to the `btnAdd_Click` event and then the method `public TextBox AddNewTextBox()` is called. But also the textbox do not appear in the form. Please suggest – user4221591 Jul 20 '15 at 10:02