Lucas Mue
Lucas Mue

Reputation: 9

How can i trigger a click event at a Button which has no name

In my code the buttons are made automatically and I need to save the information from the button in the click event. I am coding a ShopSystem in WindowsForms and when I click a button (should also work like clicking 3 times) it should stand in a text box at the next form but I just need help at coding the clickevent.

while (id < artikelAnzahl)
{
    Button ArtikelID = new Button
    {
        Location = new Point(posX, posY),
        Size = new Size(100, 75),
    };
    posX += 120;
    double s = double.Parse(id.ToString()) / 5;

    if (int.TryParse(s.ToString(), out int i))
    {
        posY += 100;
        posX = 70;
    }
    this.Controls.Add(ArtikelID);

    foreach (var p in xmlArtikelliste.Descendants("Artikel"))
    {
        if (int.Parse(p.Attribute("ID").Value) == id)
        {
            ArtikelID.Text = p.Element("Name").Value + " " +
                p.Element("Preis").Value + "€ " +
                p.Element("Anzahl").Value + "Stk. ";
        }
    }
    id++; 
}

Upvotes: 0

Views: 290

Answers (1)

draganndi
draganndi

Reputation: 155

Edit: Adding Click event delegate will not do the trick since the buttons are generated in a while loop and the event handler's action will always have the values from the last loop iteration.

So, you can achieve what you need with the following:

  • Button's Tag property for storing the data

You can store Article ID from xmlArtikelliste.Descendants("Artikel"), or the whole article if you will:

ArtikelID.Tag = p.Attribute("ID") 
  • Event handler which will be added to the Button's Click event.

Sample code:

ArtikelID.Click += (sender, e) =>
{
    if(sender is Button button)
    {
        // Pass button.Tag.ToString() as parameter when navigating to the other form;
    }
};

You can read more about it on the official docs.

Complete code:

while (id < artikelAnzahl)
{
    Button ArtikelID = new Button
    {
        Location = new Point(posX, posY),
        Size = new Size(100, 75),
    };
    posX += 120;
    double s = double.Parse(id.ToString()) / 5;

    if (int.TryParse(s.ToString(), out int i))
    {
        posY += 100;
        posX = 70;
    }
    this.Controls.Add(ArtikelID);

    foreach (var p in xmlArtikelliste.Descendants("Artikel"))
    {
        if (int.Parse(p.Attribute("ID").Value) == id)
        {
            ArtikelID.Text = p.Element("Name").Value + " " +
                p.Element("Preis").Value + "€ " +
                p.Element("Anzahl").Value + "Stk. ";

            ArtikelID.Tag = p.Attribute("ID");
        }
    }

    ArtikelID.Click += (sender, e) =>
    {
        if (sender is Button button && button.Tag != null)
        {
            // Pass button.Tag.ToString() as parameter when navigating to the other form;
        }
    };

    id++;
}

Edit 1: (sender, e) is a standard delegate signature for event handlers which executes an Action.

Ideally, what you could do is create a list of buttons, add button template and bind the data received from xmlArtikell to it. You can have a look at the following introductory tutorial on data binding in Win Forms.

Edit 2: Added solution to store the data in Button's Tag property, so it can be utilized later in the Click event handler. Otherwise, the action will always have the last while loop iteration variables to work with.

Upvotes: 1

Related Questions