2016-12-28 62 views
-2

我有一个Windows窗体应用程序,我试图添加按钮来模仿一个计算器。我的Windows窗体按钮没有显示,因为我想

public class myform : Form 
{ 
    public myform() 
    { 
     //setting size of form 
     this.Text = "Calculator"; 
     this.Height = 600; 
     this.Width = 400; 

     //creating buttons from 0-9 
     Button[] b = new Button[10]; 
     int x = 0; 
     int y = 0; 
     string ch; 
     for (int i = 0; i < b.Length; i++) 
     { 
      ch = Convert.ToString(i); 
      x = 0; 
      y = y + 50; 
      b[i] = new Button(); 
      b[i].Height = 40; 
      b[i].Width = 40; 
      b[i].Text = ch; 
      for (int j = 0; j < 3; j++) 
      { 
       x = x + 50; 
       b[i].Location = new Point(x, y); 
      } 
     } 

     for (int i = 0; i < b.Length; i++) 
     { 
      this.Controls.Add(b[i]); 
     } 

    } 
} 

这里是我创建上述myform类的对象的表单类。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 

     InitializeComponent(); 
     myform mf = new myform(); 
     mf.Show(); 
    } 
} 
+1

尝试寻找一个'TableLayoutPanel' –

+1

如果你不能更准确地描述这个问题,我们不能真正帮助你 – goto

回答

0

这个问题似乎是你总是把x值设置为x = x + 150;我建议你将你的x值改为x =(i%3)* 50;你的y到y =(i/3)* 50; 应该为您提供一个很好的按钮阵列。

 ch = Convert.ToString(i); 
     x = (i%3)*50; 
     y = (i/3)*50; 
     b[i] = new Button(); 
     b[i].Height = 40; 
     b[i].Width = 40; 
     b[i].Text = ch; 
     b[i].Location = new Point(x, y); 

将是你的新循环体。

+0

非常感谢它的工作原理...你美... –

+0

@saad bin sami。确保标记为prof1990的答案:) –

相关问题