2010-07-09 162 views
1
int width, height; 
width = this.Size.Width; 
height = this.Size.Height; 
width /= 3; 
height /= 3; 
btn_1.Size = new Size(width, height); 

我試圖在用戶調整窗體大小時更改按鈕的大小和位置。如何更改按鈕的大小

我如何分配大小的按鈕?

我試圖改變分開的寬度和高度,使之。我知道我可以通過錨定來實現,但我想用純編碼來實現。
也刷新表單不起作用。我可以使用Location屬性輕鬆設置按鈕的位置,但size屬性不起作用。我找不到區別...

以下是完整的代碼,適用於改變對象的位置,但改變大小不起作用:

private void form_counterMain_Resize(object sender, EventArgs e) 
    { 
     int width, height; 
     Point templocation; 
     templocation = new Point(0, 0); 
     width = this.Size.Width; 
     height = this.Size.Height; 
     width /= 3; 
     height /= 3; 
     //:::location::: 
     btn_1.Location = templocation; 
     templocation.X = width; 
     btn_2.Location = templocation; 
     templocation.X = width * 2; 
     btn_3.Location = templocation; 
     templocation.X = 0; 
     templocation.Y = height; 
     btn_4.Location = templocation; 
     templocation.X = width; 
     btn_5.Location = templocation; 
     templocation.X = width * 2; 
     btn_6.Location = templocation; 
     templocation.Y = height * 2; 
     templocation.X = 0; 
     btn_7.Location = templocation; 
     templocation.X = width; 
     btn_8.Location = templocation; 
     templocation.X = width * 2; 
     btn_9.Location = templocation; 

     //:::size::: 
     btn_1.Size = new Size(width, height); 
     this.Refresh(); 
+0

您是否嘗試過設置.width和.height而不是.size? – Fosco 2010-07-09 20:25:28

+0

另外,嘗試在編輯大小後刷新論壇。 – Meiscooldude 2010-07-09 20:26:46

+0

@Meiscooldude,並且您可以使用Application.DoEvents()創建此刷新事件。 – 2010-07-09 20:32:19

回答

0

我不能設法找到它,爲什麼它不會用我的代碼改變它,而它與Jamie的代碼一起工作。但是,我沒有爲此工作,而是用純代碼創建了9個按鈕。所以它給了我改變每一個財產的能力。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace hw02 
{ 
public partial class form_counterMain : Form 
{ 
    int[] b=new int[9]; //initialized the counters 
    Button[] btn= new Button[9]; //initialized the buttons 


    public form_counterMain() 
    { 
     for (int t = 0; t < 9; t++) //this loop makes all the counters 0 
     { 
      b[t] = 0; 
     } 
     for (int t = 0; t < 9;t++) //this loop makes all the buttons assigned to a button 
     { 
      btn[t]=new Button(); 
     } 
     InitializeComponent(); 
     changeFunc(); //first calculation 
     btn[0].Click += new System.EventHandler(btn0Click); //here i assign the functions to buttons 
     btn[1].Click += new System.EventHandler(btn1Click); 
     btn[2].Click += new System.EventHandler(btn2Click); 
     btn[3].Click += new System.EventHandler(btn3Click); 
     btn[4].Click += new System.EventHandler(btn4Click); 
     btn[5].Click += new System.EventHandler(btn5Click); 
     btn[6].Click += new System.EventHandler(btn6Click); 
     btn[7].Click += new System.EventHandler(btn7Click); 
     btn[8].Click += new System.EventHandler(btn8Click); 

    } 
    private void form_counterMain_Resize(object sender, EventArgs e) 
    { 
     changeFunc(); 
    } 
    private void changeFunc() 
    { 
     int width, height; 
     Point templocation = new Point(0, 0); 
     width = this.Size.Width; 
     height = this.Size.Height; 
     width = width/3 -5; //here i calculated the best values for 3 buttons 
     height = height/3-12; 
     for (int i = 0; i < 9; i++) //here i assign some necessary values to buttons and read the count numbers from memory 
     { 
      btn[i].Name = "btn_" + i; //the names are changed! 
      btn[i].TabIndex = i; 
      btn[i].Text = b[i].ToString(); 
      btn[i].Size = new Size(width, height); 
      btn[i].Visible = true; 
      btn[i].Parent = this; 
      btn[i].FlatStyle = System.Windows.Forms.FlatStyle.Flat; 

     } 
     //this lines sets the location of the buttons 
     btn[0].Location = templocation; 
     templocation.X = width; 
     btn[1].Location = templocation; 
     templocation.X = width * 2; 
     btn[2].Location = templocation; 
     templocation.X = 0; 
     templocation.Y = height; 
     btn[3].Location = templocation; 
     templocation.X = width; 
     btn[4].Location = templocation; 
     templocation.X = width * 2; 
     btn[5].Location = templocation; 
     templocation.Y = height * 2; 
     templocation.X = 0; 
     btn[6].Location = templocation; 
     templocation.X = width; 
     btn[7].Location = templocation; 
     templocation.X = width * 2; 
     btn[8].Location = templocation; 

    } 
    //here the functions start, they only increase the integers in the memory and then they force the program to refresh its visual state 
    private void btn0Click(Object sender, EventArgs e) 
    { 
     b[0]++; 
     changeFunc(); 
    } 
    private void btn1Click(Object sender, EventArgs e) 
    { 
     b[1]++; 
     changeFunc(); 
    } 
    private void btn2Click(Object sender, EventArgs e) 
    { 
     b[2]++; 
     changeFunc(); 
    } 
    private void btn3Click(Object sender, EventArgs e) 
    { 
     b[3]++; 
     changeFunc(); 
    } 
    private void btn4Click(Object sender, EventArgs e) 
    { 
     b[4]++; 
     changeFunc(); 
    } 
    private void btn5Click(Object sender, EventArgs e) 
    { 
     b[5]++; 
     changeFunc(); 
    } 
    private void btn6Click(Object sender, EventArgs e) 
    { 
     b[6]++; 
     changeFunc(); 
    } 
    private void btn7Click(Object sender, EventArgs e) 
    { 
     b[7]++; 
     changeFunc(); 
    } 
    private void btn8Click(Object sender, EventArgs e) 
    { 
     b[8]++; 
     changeFunc(); 
    } 

} 
} 

我不知道是否有人需要代碼,我只是粘貼。

+0

在Form_Resize()事件處理程序中設置一個斷點,並檢查發生了什麼。可能是由於表單沒有實際調整大小,事件不是事件觸發。表單的其中一個屬性可能會迫使表單保持固定的大小。或者,按鈕的其中一個屬性可能會導致它保持固定的大小。 – 2010-07-12 10:05:35

0

您需要附加一個事件處理程序Resize您當前表單的事件。然後在這個事件處理程序中,這只是另一種方法,然後可以調整按鈕大小,或者在窗體大小調整時做任何你需要做的事情。

我想你需要先得到一個更好地瞭解事件處理工作在Windows窗體。在這裏閱讀更多 - http://msdn.microsoft.com/en-us/library/aa983610%28VS.71%29.aspx

更新:好吧,我看到你已經附加了事件處理程序。我錯過了這一點,並認爲你不知道如何做到這一點。

確保resize事件仍連接到你已經證明我們在你的答案事件處理方法,然後它應該只是工作,除非你是多線程,或BackgroundWorker的情況下工作。從不同的線程更新用戶界面到主UI線程的用戶界面需要以不同的方式完成,並小心謹慎。

+0

是的肯定它應該這樣工作,但它不會:D。問題是,這是我們的功課,我和我的朋友都不能這樣做。無論如何,謝謝 – gkaykck 2010-07-12 06:42:06

1

有什麼不好設定,以滿足您的需求Alignment屬性?

你也可以把它放在將要停靠/正確對齊一個3x3的表佈局面板裏面......

讓WinForms的工作了;)

1

對於那些仍在尋找一個回答這個問題,記得設置的行爲:按鈕的自動調整屬性設置爲FALSE試圖以編程方式改變它的大小時。

0

從微$經常:

由於尺寸類是值類型(結構在Visual Basic中, 結構在Visual C#),它是由返回值,這意味着訪問 屬性返回副本控制的大小。因此,調整從此屬性 返回的Size的Width或Height屬性的 不會影響控件的寬度或高度。要調整控件的寬度或高度,您必須設置控件的Width或 Height屬性,或者使用新的Size設置Size屬性。

爲了保持更好的性能,不設置在 其構造一個控件的大小。首選方法是覆蓋DefaultSize 屬性。

相關問題