2010-07-09 168 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 属性。