首页 新闻 搜索 专区 学院

C#动态生成TextBox间隔问题

0
悬赏园豆:5 [已解决问题] 解决于 2012-04-25 16:41

for (int i = 0; i < 9; i++)
            {
                for ( int j = 0; j < 9; j++)
                {
                    TextBox TextBox1 = new TextBox();
                    TextBox1.Text = "";
                    TextBox1.Size = new Size(50,50);
                    TextBox1.Margin = new System.Windows.Forms.Padding(0);
                    TextBox1.Location = new Point(150+ TextBox1.Width * j, 80+i*50);
                    TextBox1.Name = "TextBox" + i.ToString()+j.ToString();
                  

                    this.Controls.Add(TextBox1);
                }
            }

这样动态生成的TextBox怎么样设置他们中间没有间隔呢,还有怎么生成正方形,我那样都不行的的

Wandroid的主页 Wandroid | 初学一级 | 园豆:123
提问于:2012-04-22 20:59
< >
分享
最佳答案
0

C# winForm中

TextBox在其属性MultiLine设置为false的时候 手动设置他的Size中height是没有意义的,以为它的height取决于其中字体的大小。要想生成正方形的的先将MultiLine设置为true在将textBox.Size=new Size(50,50);正方形的TextBox就OK了,另外这时候如果要文本居中的话 要去设置 TextAlign这个属性其中的一般都是够用的了。

至于边距那个很简单 生成控件之间的间隙 不仅仅在margin 和 location上 它的border也是要占用像素的如果要真的连在一起的话 要将BorderStyle设置为None

截图:

 

代码:

            private System.Windows.Forms.TextBox textBox1;
            private System.Windows.Forms.TextBox textBox2;
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            // 
            // textBox1
            // 
            this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox1.Location = new System.Drawing.Point(40, 40);
            this.textBox1.Margin = new System.Windows.Forms.Padding(0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(50, 50);
            this.textBox1.TabIndex = 0;
            this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // textBox2
            // 
            this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox2.Location = new System.Drawing.Point(90, 40);
            this.textBox2.Margin = new System.Windows.Forms.Padding(0);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(50, 50);
            this.textBox2.TabIndex = 1;
            this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            // 
            // UserControl1
            // 
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
收获园豆:5
菜田小鸟 | 初学一级 |园豆:110 | 2012-04-23 03:06

不行呀,加了 this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
之后TextBox都没有显示了,,我那个列与列之间是没有间隔的,但是行与行就有了..

Wandroid | 园豆:123 (初学一级) | 2012-04-23 16:32

@Wandroid: 你贴一下 关键代码 啊 你这样说 我也不知道 你哪儿有问题啊  还有  列与列 行与行 是什么意思

菜田小鸟 | 园豆:110 (初学一级) | 2012-04-23 17:07

@菜田小鸟: 代码如下:

 public void createTextBox()
        {
            TextBox TextBox1;
            panel = new Panel();
            panel.Size = new Size(450, 450);
            panel.Location = new Point(50, 20);
            this.Controls.Add(panel);
            for (int i = 0; i < 9; i++)
            {
                for ( int j = 0; j < 9; j++)
                {
                    TextBox1 = new TextBox();
                    TextBox1.Text = "";
                    TextBox1.Multiline = true;
                    TextBox1.Size = new Size(30,30);
                  
                //    TextBox1.Margin = new System.Windows.Forms.Padding(0);
                    TextBox1.Location = new Point(150+ TextBox1.Width * j,50+i*50);
                    TextBox1.Name = "TextBox" + i.ToString()+j.ToString();
                   
                    panel.Controls.Add(TextBox1);
                }
           //     TextBox1.Margin = new System.Windows.Forms.Padding(0);
            }

        }

Wandroid | 园豆:123 (初学一级) | 2012-04-24 19:41

@Wandroid: 加了 this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None;
之后TextBox都没有显示了
是因为你这个背景色 也是白色的所以没有任何显示 其实 是存在的 你把背景色 换一下就OK了

菜田小鸟 | 园豆:110 (初学一级) | 2012-04-24 22:09

@菜田小鸟: 你好,我要的效果是这样的每个TextBox贴在一起但还是可以看出来的,你那样实现就完全没有间隔了

Wandroid | 园豆:123 (初学一级) | 2012-04-25 15:32

@菜田小鸟: 行了,是我自己把放的位置弄错了  TextBox1.Location = new Point(150+ TextBox1.Width * j,50+i*30);

Wandroid | 园豆:123 (初学一级) | 2012-04-25 16:40
其他回答(1)
0

如果是Winform就直接在flowtalbe....那个以这个开头的控件,单词我也忘记了。在这个控件中写就是的了。它会自己相邻的。

Asp.net:

protected void Page_Load(object sender, EventArgs e)
    {
        int n = 5;
        for (int i = 0; i < n; i++)
        {
            TextBox txt = new TextBox();
            txt.ID = "txt_" + i.ToString();
            Console.WriteLine();
            
            Panel1.Controls.Add(txt);
   Panel1.Controls.Add(new Literal("<br />")); 
           


        }

    }
Winform:
Method 1

private void Method1()
{
for(int i = 0; i<boxes; i++)
{
TextBox tb = new TextBox();
tb.Location = new System.Drawing.Point(40, 120 + i * 20);
tb.Name = "TextBoxName" + i.ToString();
tb.Size = new System.Drawing.Size(184,20);
tb.TabIndex = i + 2;
tb.Text = String.Empty;
panel1.Controls.Add(tb);
}
}

Method 2

private ArrayList textboxes;
private int Tboxes = 3;
private void Method2()
{
int i = 0;
this.textboxes = new ArrayList();
for(i = 0; i<Tboxes; i++)
{
this.textboxes.Add( new TextBox() );
((TextBox)this.textboxes[i]).Location = new System.Drawing.Point(40, 36 + i * 20);
((TextBox)this.textboxes[i]).Name = "TextBoxName" + i.ToString();
((TextBox)this.textboxes[i]).Size = new System.Drawing.Size(184,20);
((TextBox)this.textboxes[i]).TabIndex = i + 2;
((TextBox)this.textboxes[i]).Text = String.Empty;
panel1.Controls.Add(((TextBox)this.textboxes[i]));
}
}

Dynamically create CheckBox with C# on Windows Form

CheckBox[] chk = new CheckBox[10];
int height = 1;
int padding = 10;
for (int i = 0; i <= 9; i++)
{
chk[i] = new CheckBox();
chk[i].Name = i.ToString();
chk[i].Text = i.ToString();
chk[i].TabIndex = i;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(10, 20 + padding + height, 40, 22);
panel1.Controls.Add(chk[i]);
height += 22;
}

 

 
悟行 | 园豆:12371 (专家六级) | 2012-04-22 23:42

即使用了Panel还是不行,不会自动取消间隔

 public void createTextBox()
        {
            TextBox TextBox1;
            panel = new Panel();
            panel.Size = new Size(450, 450);
            panel.Location = new Point(50, 20);
            this.Controls.Add(panel);
            for (int i = 0; i < 9; i++)
            {
                for ( int j = 0; j < 9; j++)
                {
                    TextBox1 = new TextBox();
                    TextBox1.Text = "";
                    TextBox1.Multiline = true;
                    TextBox1.Size = new Size(30,30);
                  
                //    TextBox1.Margin = new System.Windows.Forms.Padding(0);
                    TextBox1.Location = new Point(150+ TextBox1.Width * j,50+i*50);
                    TextBox1.Name = "TextBox" + i.ToString()+j.ToString();
                   
                    panel.Controls.Add(TextBox1);
                }
           //     TextBox1.Margin = new System.Windows.Forms.Padding(0);
            }

        }

支持(0) 反对(0) Wandroid | 园豆:123 (初学一级) | 2012-04-23 16:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册