Click here to Skip to main content
15,913,115 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,
I wants to add TextBoxes in Panel in following way
C#
int startPos = 0;
        int srno = 1;
        private void button2_Click(object sender, EventArgs e)
        {
            TextBox tb = new TextBox();
            tb.Location = new Point(0, startPos);
            tb.Text = srno.ToString(); srno += 1;
            panel1.Controls.Add(tb);
            startPos += tb.Height;
        }


panel1.AutoScroll=true;

first 10 TextBoxes added fine with no problem, but after that some space lefts between textboxes. I don't Know why this happens?
Please Help me.
Posted
Comments
Ralf Meier 1-Oct-15 6:46am    
I tested your code-snipped.
It works like it should - so : where is the difference between you and me ?
Is there anything you application does further ?
Shruti91 1-Oct-15 6:50am    
try to add more than 10 textboxes it will not in continus row
Ralf Meier 1-Oct-15 6:52am    
I added 30 textboxes, got the scroll-sliders but all textboxes are positioned well ...
Shruti91 1-Oct-15 7:05am    
use panel1 with size 150 X 150
Ralf Meier 1-Oct-15 10:00am    
That makes no difference ... :(

Which Framework do you use ?
Forms or WPF ?

Hi,

I have tried your code, it happens as you said. But I can't find the reason for your problem.

But I have an alternate solution for you, In this case, you can use FlowLayoutPanel. It solves your problem. And set the value of following properties of FlowLayoutPanel.
VB
AutoScroll = True
FlowDirection = TopDown
WrapContents = False
 
Share this answer
 
Comments
Shruti91 1-Oct-15 7:35am    
Thanks for solution.
Why are you using a panel and a bunch of textboxes at all?
Try using a DataGridView instead, and turn the row and column headers off - you will get a much simpler system in the long run.
 
Share this answer
 
Comments
Shruti91 1-Oct-15 6:44am    
You are correct but, in my case I have UserControl instead of TextBox in above code and that User control contains 2/3 rows of textboxes.
Try this:
C#
int srno = 1;

private void button2_Click(object sender, EventArgs e)
{
    TextBox tb = new TextBox();
    tb.BorderStyle = BorderStyle.None;
    tb.Margin = new Padding(0);
    tb.Padding = new Padding(0);
    tb.Dock = DockStyle.Top;
    tb.Text = srno.ToString();

    tb.BackColor = (srno % 2 == 0) ? Color.LightBlue : Color.AliceBlue;
    
    srno ++;
    
    panel1.Controls.Add(tb);
    tb.BringToFront();
}
Adjust the different parameters above to taste.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900