【问题标题】:How to change a button's size如何更改按钮的大小
【发布时间】:2010-07-09 20:22:33
【问题描述】:
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();

【问题讨论】:

  • 您是否尝试过设置 .width 和 .height 而不是 .size?
  • 另外,编辑大小后尝试刷新论坛。
  • @Meiscooldude,您可以使用 Application.DoEvents(); 进行此刷新事件;
  • 即使你让它工作,当你的表单被调整大小时,你通常不应该手动调整按钮或任何其他控件的大小。相反,请尝试以特定方式停靠/锚定您的按钮以获得您想要的。
  • 它们都没有工作,而且我不想使用任何锚定或对接,它们不能以我想要的方式工作

标签: c# winforms button


【解决方案1】:

设置 Alignment 属性以满足您的需要有什么问题?

您也可以将它放在一个 3x3 表格布局面板中,该面板将正确停靠/对齐...

让winforms解决它;)

【讨论】:

    【解决方案2】:

    对于仍在寻找此问题答案的用户,请记住在尝试以编程方式更改其大小时将按钮的 Behaviour:Autosize 属性设置为 FALSE。

    【讨论】:

      【解决方案3】:

      我无法找到它,为什么当它与 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();
          }
      
      }
      }
      

      我不知道是否有人需要代码,我只是粘贴了。

      【讨论】:

      • 在 Form_Resize() 事件处理程序中设置断点,然后检查发生了什么。由于表单实际上没有调整大小,因此事件可能不是事件触发。表单的属性之一可能是强制表单保持固定大小。或者,按钮的属性之一可能会导致它保持固定大小。
      【解决方案4】:

      您需要为当前表单的 Resize 事件附加一个事件处理程序。然后在这个事件处理程序中,这只是另一种方法,然后你可以调整按钮的大小,或者在调整表单大小时做任何你需要做的事情。

      我认为您首先需要更好地了解事件处理在 Windows 窗体中的工作原理。阅读此处了解更多信息 - http://msdn.microsoft.com/en-us/library/aa983610%28VS.71%29.aspx

      更新: 好的,我看到您已经附加了事件处理程序。我错过了这一点,并假设您不知道如何执行此操作。

      确保 resize 事件仍附加到您在回答中向我们展示的事件处理程序方法,然后它应该可以正常工作,除非您正在使用多个线程或 BackgroundWorker 实例。将用户界面从不同的线程更新到主 UI 线程的用户界面需要以不同的方式进行,并且要小心。

      【讨论】:

      • 是的,它肯定应该这样工作,但它不会 :D 。问题是,这是我们的作业,我和我的朋友都无法做到这一点。还是谢谢
      【解决方案5】:

      来自docs.microsoft.com documentationControl.Size 属性:

      因为 Size 类是一个值类型(Visual Basic 中的结构, Visual C#中的struct),它是按值返回的,这意味着访问 属性返回控件大小的副本。所以,调整 从此属性返回的 Size 的 Width 或 Height 属性 不会影响控件的宽度或高度。调整 控件的宽度或高度,必须设置控件的宽度或 Height 属性,或将 Size 属性设置为新的 Size。

      为了保持更好的性能,不要将控件的大小设置为 它的构造函数。首选方法是覆盖 DefaultSize 属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-29
        • 1970-01-01
        • 2017-11-24
        • 2011-02-24
        相关资源
        最近更新 更多