我有一个名为AddButton的函数,我希望它接受一个面板参数,以便它知道应该在哪个面板上添加按钮。
不知道如何将其作为C#和Windows窗体的新功能
private void AddButton(string Type)
{
if (Type == "But")
{
//string Name, string Text, decimal Price, string ItemName
ButAmount++;
string NameCode = "Item"+ButAmount.ToString();
string Text = Interaction.InputBox("Item naam?", "Geef de item naam", "Nieuw item");
string PriceStr = Interaction.InputBox("Wat is de prijs van dit item?", "Geef de prijs van het item", "1.3");
decimal Price = Convert.ToDecimal(PriceStr);
LocButx = LocButx + 120;
if (LocButx >= 460) { LocButx = 0; LocButy = LocButy + 50; }
// Create a Button object
Button NewButton = new Button();
// Set Button properties
NewButton.Height = 50;
NewButton.Width = 120;
NewButton.BackColor = Color.Gainsboro;
NewButton.ForeColor = Color.Black;
NewButton.Location = new Point(LocButx, LocButy);
NewButton.Text = Text;
NewButton.Name = NameCode;
NewButton.Font = new Font("Microsoft Sans Serif", 12);
//add data to tag
NewButton.Tag = $"{Type}:{Price}:{Text}";
//NewButton.Tag = AddItem(Price, ItemName);
// Add a Button Click Event handler
NewButton.Click += new EventHandler(NewButton_Click);
//Add buttons to the correct panel
}
...
}
private void NewButton_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
Button cat = sender as Button;
//MessageBox.Show(btn.Name + " Clicked");
//Retreive data from new button tag
string DataStr = btn.Tag as string;
string[] Data = DataStr.Split(':');
string Type = Data[0];
//Add code for each dynamic button press
if (Type == "But")
{
decimal Price = Convert.ToDecimal(Data[1]);
string ItemName = Data[2];
AddItem(Price, ItemName);
}
else if (Type == "Cat")
{
string Text = Data[1];
string NameCode = Data[2];
int CatAmount = Convert.ToInt32(Data[3]);
switch (CatAmount)
{
case 1:
HidePanels();
panelCat1.Visible = true;
//AddButton to panel here
break;
...
}
}
}
我希望在情况1中我可以添加AddButton(Type,panelName),这会将我的动态按钮添加到我输入的面板名称中。
谢谢!
答案 0 :(得分:-1)
您可以使用此:
yourPanel.Controls.Add(NewButton);