У меня есть функция AddButton, я хотел бы, чтобы она принимала аргумент панели, чтобы она знала, к какой панели она должна добавить кнопку.
Понятия не имею, как это сделать, поскольку я новичок в C # и окнах
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), который добавит мою динамическую кнопку к вводимому имени панели.
Спасибо!