none
Dynamic Button Click event RRS feed

  • Question

  • hey! Im trying to make so if you click on a dynamic button then it gets the text of a dynamic Label. but dont know how to make it.. 10 Buttons and 10 Labels.
    for (int i = 0; i < RollerBeteckning.Count(); i++)
                {
    
    
                    //service.LoadPropertiesForItems(appointments.Items[i], PropertySet.FirstClassProperties);
                    rollerBeteckningLbl = new Label();
                    rollerBeteckningLbl.Name = "TaBortLblDynamisk " + i;
                    rollerBeteckningLbl.Font = new Font(rollerBeteckningLbl.Font.FontFamily, 10);
                    rollerBeteckningLbl.Text = RollerBeteckning[i];
                    rollerBeteckningLbl.AutoSize = true;
                    //test.Add(appointments.Items[i].TextBody.Text);
                    rollerBeteckningLbl.BorderStyle = BorderStyle.FixedSingle;
                    rollerBeteckningLbl.Location = new Point(BefintligaRollerLbl.Location.X, BefintligaRollerLbl.Location.Y + BefintligaRollerLbl.Height + 15 + (i * 25));
    
                    //lstChckBox.Add(box);
                    //box.CheckedChanged += new EventHandler(chck_CheckedChanged);
    
                    rollerTaBortBtn = new Button();
                    rollerTaBortBtn.Name = "TaBortBtnDynamisk " + i;
                    rollerTaBortBtn.Text = "Ta bort";
                    rollerTaBortBtn.Size = new Size(75, rollerBeteckningLbl.Height);
                    rollerTaBortBtn.Location = new Point(rollerBeteckningLbl.Location.X + rollerBeteckningLbl.Width + 20, BefintligaRollerLbl.Location.Y + BefintligaRollerLbl.Height + 15 + (i * 25));
                    rollerTaBortBtn.FlatStyle = FlatStyle.Flat;
                    rollerTaBortBtn.FlatAppearance.BorderSize = 0;
                    rollerTaBortBtn.BackColor = Color.FromArgb(254, 66, 2);
                    rollerTaBortBtn.ForeColor = Color.White;
                    rollerTaBortBtn.Click += new EventHandler(Btn_click);
                    TaBortRollBtnLista.Add(rollerTaBortBtn);
                    Controls.Add(rollerBeteckningLbl);
                    Controls.Add(rollerTaBortBtn);
                }
            void Btn_click(object sender, EventArgs e)
            {
    
                for (int i = 0; i < TaBortRollBtnLista.Count(); i++)
                {
    
                    try
                    {
    
                        queryString = "exec Ta_Bort_Användare_Roller '" + AnvändarId + "', '" + RollTypIDLista[i] + "' ";
    
                        conn = new SqlConnection(connetionString);
                        SqlCommand commandProcessID = new SqlCommand(queryString, conn);
    
                        conn.Open();
                        reader = commandProcessID.ExecuteReader();
    
                        reader.Close();
                        conn.Close();
    
    
    
                    }
                    catch (SqlException sql)
                    {
    
                        MessageBox.Show("Följande fel: \n\n" + sql);
                    }
    
                }
                MessageBox.Show("Nu är Rollen borttagen!");
    
            }
    I get all the buttons here.. I just want to do this for the clicked one..

    Wednesday, March 4, 2020 12:58 PM

Answers

  • Hi Carlo,

    Thank you for posting here.

    I noticed that you added explicit index when naming buttons and labels.

    So, you can achieve the function you want based on this index.

    Here is a code example.

      private void Btn_click(object sender, EventArgs e)
            {
                Button button = (Button)sender;
                string index = button.Name.Split(new char[] { ' ' })[1];
    
                Control controls = this.Controls.Find("TaBortLblDynamisk " + index, true).FirstOrDefault();
                MessageBox.Show(controls.Text);
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Carlo Goretti Thursday, March 5, 2020 7:21 AM
    Thursday, March 5, 2020 6:39 AM

All replies

  • Try something like this:

       int i = TaBortRollBtnLista.IndexOf((Button)sender);

    If this works, i represents the index of the clicked button. You can use it instead of for loop.

    Wednesday, March 4, 2020 3:00 PM
  • Hi Carlo,

    Thank you for posting here.

    I noticed that you added explicit index when naming buttons and labels.

    So, you can achieve the function you want based on this index.

    Here is a code example.

      private void Btn_click(object sender, EventArgs e)
            {
                Button button = (Button)sender;
                string index = button.Name.Split(new char[] { ' ' })[1];
    
                Control controls = this.Controls.Find("TaBortLblDynamisk " + index, true).FirstOrDefault();
                MessageBox.Show(controls.Text);
            }

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Carlo Goretti Thursday, March 5, 2020 7:21 AM
    Thursday, March 5, 2020 6:39 AM
  • Thanks! That helped me a lot!
    Thursday, March 5, 2020 7:21 AM