2016-12-28 52 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的答案:) –

相關問題