如何在Panel中添加TextBoxes没有空格? [英] How to add TextBoxes in Panel without spaces?

查看:60
本文介绍了如何在Panel中添加TextBoxes没有空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我想以下列方式在Panel中添加TextBoxes

Hi All,
I wants to add TextBoxes in Panel in following way

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;



前10个TextBoxes添加没有问题,但在文本框之间的一些空间左边。我不知道为什么会这样?

请帮帮我。



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.

推荐答案





我已经尝试过你的代码了,就像你说的那样。但我找不到你问题的原因。


但是我有一个替代解决方案,在这种情况下,你可以使用 FlowLayoutPanel 。它解决了你的问题。并设置 FlowLayoutPanel 的以下属性的值。

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.
AutoScroll = True
FlowDirection = TopDown
WrapContents = False


为什么使用一个面板和一堆文本框?

尝试使用DataGridView,并关闭行和列标题 - 从长远来看,你将获得一个更简单的系统。
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.


试试这个:
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.


这篇关于如何在Panel中添加TextBoxes没有空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆