簡體   English   中英

如何創建C#按鈕數組?

[英]How do I create a C# Array of Buttons?

如何在Winforms應用程序中構建按鈕數組?

我想要做的是:我有一些日歷安排的按鈕,表示時間段。 IE:周一0700Button,周一0730Button,周一0800Button,依此類推30分鍾。

我有一個xml數據庫,其中一個約會字段是<Duration>當持續時間= 0.5小時, <Time>字段等於“07:00 am”時,為'Monday0700Button'着色。 當持續時間為1.0小時時,我希望它填充'Monday0700Button'以及'Monday0730Button'的下一個時間段按鈕。

有任何想法嗎? 謝謝。

是的,您可以構建一個按鈕列表,如下所示。

List<Button> listOfButtons = new List<Button>();
listOfButtons.Add(yourButton);

是的,構建一個按鈕數組或任何對象都沒問題。 您將無法在Visual Studio設計器中看到它們,但它們可以正常工作。

很久以前我使用二維數組按鈕來構建計算器應用程序的UI。 我很長一段時間都使用過HP-15C,而且錯過了它。

替代文字

陣列方法運行良好。

  Button[] numberButtons=new Button[] { btn0, btn1, btn2, btn3, btn4, btn5, btn6, btn7, btn8, btn9, btnDecimalPt};
  Button[] operationButtons=new Button[] { btnDiv, btnMult, btnSubtract, btnAdd };

  foreach (var b in numberButtons)
       b.Click += new System.EventHandler(this.Number_Click);

  foreach (var b in operationButtons)
      b.Click += new System.EventHandler(this.Operation_Click);

  // etc

  Button[][] allButtons=
  {
      new Button[] {btnSqrt, btnExp, btn10x, btnPow,btnMultInverse, btnCHS, null, null, null, null}, 
      new Button[] {btnN, btnInterest, btnPMT, btnPV, btnFV, null, btn7, btn8, btn9, btnDiv}, 
      new Button[] {btnLn, btnLog, btnSine, btnCosine, btnTangent, btnPi, btn4, btn5, btn6, btnMult}, 
      new Button[] {btnRoll, btnSwap, btnCLRfin, btnCLX, btnCLR, btnEnter, btn1, btn2, btn3, btnSubtract}, 
      new Button[] {btnInt, btnFrac, btnFix, btnStore, btnRecall, null, btn0, btnDecimalPt, btnNotUsed, btnAdd}
  };

  // programmatically set the location
  int col,row;
  for(row=0; row < allButtons.Length; row++)
  {
      Button[] ButtonCol= allButtons[row];
      for (col=0; col < ButtonCol.Length; col++)
      {
          if (ButtonCol[col]!=null)
          {
              ButtonCol[col].TabIndex = col + (row * allButtons.Length) +1;
              ButtonCol[col].Font = font1; 
              ButtonCol[col].BackColor = System.Drawing.SystemColors.ControlDark;
              ButtonCol[col].Size=new System.Drawing.Size(stdButtonWidth, stdButtonHeight);
              ButtonCol[col].Location=new Point(startX + (col * stdButtonWidth), 
                                                startY + (row * stdButtonHeight) ) ;
          }
      }
  }

是的,絕對有可能,但可能沒必要。

如果我理解正確,您應該能夠向表單添加FlowLayoutPanel ,然后循環遍歷XML,根據需要實例化一個新Button。 連接Click事件的事件處理程序,然后通過調用FlowLayoutPanel上Controls屬性的Add()方法將該按鈕添加到FlowLayoutPanel。

while (reader.Reader())
{
    // Parse XML here

    // Instantiate a new button that will be added to your FlowLayoutPanel
    Button btn = new Button();

    // Set button properties, as necessary
    btn.Text = "Foo";
    btn.Click += new EventHandler(SomeButton_Click);

    // Add the button to the FlowLayoutPanel
    flowLayoutPanel.Controls.Add(btn);
}

雖然FlowLayoutPanel可以輕松地為按鈕進行布局,但它可能對您不起作用。 如果是這種情況,則在循環遍歷XML時,必須計算按鈕的X和Y坐標。

您將遇到的上述方法的一個問題是它始終調用完全相同的事件處理程序。 因此,您必須想出一種方法來確定單擊了哪個按鈕。 一種方法可能是擴展Button控件以提供可用於確認時間段的其他屬性。

像所有GUI元素一樣,按鈕就像其他任何對象一樣(也可能是可顯示的)。 所以,是的,您可以擁有數組,列表,詞典 - 無論您想要什么包含按鈕。 Taylor的回復有一些示例代碼。

是的,這是可能的,正如泰勒L所證明的那樣。 唯一的問題是,通過復制和粘貼控件創建的VB6樣式的控件數組不能再在表單編輯器中完成。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM