-1
我正在使用C#創建一個應用程序,它可以通過串行與Arduino進行通信。我有按鈕通過串行發送某些字母來告訴Arduino要做什麼。 Arduino將控制RGB LED燈帶。但我希望用戶能夠添加按鈕並在用戶按下按鈕時發送RGB值。我已經想出瞭如何添加按鈕和所有,但我想知道如何爲已添加的每個按鈕創建單個事件處理程序。允許用戶控制會發生什麼
int h = 5;
int l = 5;
private void addButton_Click(object sender, EventArgs e)
{
Point newLoc = new Point(h, l);
List<Button> buttons = new List<Button>();
Button newButton = new Button();
newButton.Text = buttonNameTextBox.Text;
newButton.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(253)))), ((int)(((byte)(199)))), ((int)(((byte)(13)))));
newButton.Size = new Size(150, 50);
newButton.Font = new System.Drawing.Font("Calibri", 15F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
newButton.Location = newLoc;
newLoc.Offset(0, newButton.Height + 5);
newButton.
buttons.Add(newButton);
newButton.Name = componentNameTextBox.Text;
this.Controls.Add(newButton);
l += 65;
newButton.Click += new EventHandler(newButton_Click);
}
所以這就是我必須創建按鈕和放置按鈕。我的最終目標是能夠讓用戶輸入他們想要的LED的RGB值,以便當按下按鈕時它將發送它們。
您可以將一個事件處理程序到多個公司然後使用'sender'參數來確定哪個按鈕被點擊。 –