1

I am developing a Windows Form application. One requirement I have is to create new text boxes on a click of button inside a Tab Page. This is what I have done inside the button (btnAdd) click event:

        TextBox textBox1 = new TextBox();
        textBox1.Name = DateTime.Now.ToString() + "textbox"; 
        textBox1.Text = DateTime.Now.ToString() + "textbox"; 
        textBox1.Size = new Size(200, 30);
        textBox1.Location = new Point(tbpEx.Left + 20, loc);
        tbpEx.Controls.Add(textBox1);
        loc = textBox1.Height + 20 ;

So that I can get the text boxes one below the other. But I click the button, the text boxes get added, but after 2 text boxes there are no more text boxes visible.

I have tried to place another button(btnCnt) on the form that counts the controls in the tbpEX (I have no other controls in this tab page (in fact it is the only control - that too a tab page - on the form). In this button click I have this code:

        foreach (Control c in tbpEx.Controls)
        {
            lblMsg.Text = lblMsg.Text + c.Name + ":" + c.Parent.Name + Environment.NewLine;
        }

I have clicked the btnAdd 7 times but I can see only 2 text boxes. However, when I click the btnCnt, lblMsg displays 7 textboxes.

Why I am unable to see the erest of the text boxes?

Erik Philips
  • 53,428
  • 11
  • 128
  • 150
Kumar C
  • 100
  • 2
  • 13
  • maybe they are just not visible, becuase for example the parent (tbpEx) is too small to fit them all? :) – mikus Nov 18 '15 at 16:46
  • After adding two text boxes, there is a lot of area visible in the tab page (vertically).. Also the tab page is set to scroll, so even if th evisible area is small, it should allow me to scroll. – Kumar C Nov 18 '15 at 16:47
  • maybe there is, but the if the size of tbpEx is set explicitly then it would not expand to fit them all, try to set a dock for the tbpEx to cover the whole form, or just set its size to be much bigger than the buttons youre trying to fit, also you can try to decrease the buttons size and see if then more of them would show up :) – mikus Nov 18 '15 at 16:49

1 Answers1

3

You are placing the textboxes one over the other with this line

 loc = textBox1.Height + 20 ;

It should be

 loc = loc + textBox1.Height + 20 ;
Steve
  • 213,761
  • 22
  • 232
  • 286
  • When I do that, the number of text boxes visible are now only 6 though I have clicked hte btnAdd 11 times. The btnCnt shows that thre are 11 textboxes. What I wanted to do is allow the user as many text boxes he wants to add with the btnAdd click and these textboxes should be visible one below the other and if the text boxes are out of the tab page area then there should be a vertical scroll bar. – Kumar C Nov 18 '15 at 16:55
  • 1
    This is a different question now. You should post a new question, however a few hints. You should place the textbox inside a container that automatically has the capability to add a scrollbar if its content exceed the visible area. (FlowLayoutPanel with AutoScroll = true and FlowDirection = TopDown) – Steve Nov 18 '15 at 17:05
  • @Steve Just out of curiosity why cant we achieve the same functionality with Tab Page. I thought Tab Page is also a Container. I have been facing certain issues with the Flow Layout Panel that you suggested. I am now able to see the text boxes as many as I Add, but they are being added horizontally even though I have given the **Flow Direction** to be **TopDown**.. _I will Open a new Question on this since the issue is now different._ – Kumar C Nov 19 '15 at 04:38
  • 1
    I will not be opening a new question since [this](http://stackoverflow.com/questions/23448229/strange-empty-spaces-in-flowlayoutpanel) SO post solved my problem. The problem was solved by adding _'FlowLayoutPanel.SetFlowBreak(textBox1,true)'_. Now the text boxes are displaying one below the other as desired. I still could not get why the same functinality could not be acheived in Tab Page _without adding a Flow Layout Panel._ – Kumar C Nov 19 '15 at 05:04