Hi

creating dynamic elements in winforms and click events with child elements and remove elements

 Panel panel = new Panel

{

    Size = new Size(pnlparent.Width, 100), // Set a proper size for visibility

    Location = new Point(10, y), // Position panels dynamically

    BorderStyle = BorderStyle.FixedSingle // Optional: To visually distinguish panels

};


var labelName = new Label

{

    AutoSize = true,

    Location = new Point(10, 10), // Set position relative to panel

    Font = new Font("Arial", 10, FontStyle.Bold),

    Text = item.openingpageurl

};

int labelWidth = TextRenderer.MeasureText(item.openingpageurl, new Font("Arial", 10, FontStyle.Bold)).Width + 10;

var textBox = new System.Windows.Forms.TextBox

{

    Size = new Size(200, 25),

    Location = new Point(labelWidth + 10, 12) // Adjust position

};

int sass = labelName.Width;


// Create a Button

var button = new System.Windows.Forms.Button

{

    Size = new Size(80, 25),

    Location = new Point(labelWidth + 210, 10),

    Text = "Add New",

    Tag = textBox // Store the textbox in the button's Tag

};

button.Click += Button_Click; // Attach Click Event

labelName.Click += Label_Click;


// Attach Click Event to Panel (optional)

panel.Click += Panel_Click;


panel.Controls.Add(labelName);

panel.Controls.Add(textBox);

panel.Controls.Add(button);

pnlparent.Controls.Add(panel);


y += panel.Height + 10; // Increase y to prevent overlap




    private void Button_Click(object sender, EventArgs e)

    {

        System.Windows.Forms.Button btn = sender as System.Windows.Forms.Button;

        if (btn != null)

        {

            //TextBox txtBox = btn.Tag as TextBox;

            //if (txtBox != null)

            //{

            //    MessageBox.Show("Entered Text: " + txtBox.Text);

            //}

            Panel parentPanel = btn.Parent as Panel;

            if (parentPanel != null)

            {

                // Find last added control position

                int newY = 10;

                foreach (Control ctrl in parentPanel.Controls)

                {

                    if (ctrl is System.Windows.Forms.TextBox)

                    {

                        newY = Math.Max(newY, ctrl.Bottom + 5); // Position below last TextBox

                    }

                }


                // Create a new TextBox dynamically

                var newTextBox = new System.Windows.Forms.TextBox

                {

                    Size = new Size(200, 25),

                    Location = new Point(10, newY)

                };

                System.Windows.Forms.Button button = new System.Windows.Forms.Button

                {

                    Size = new Size(80, 25),

                    Location = new Point(210, newY),

                    Text = "close",

                    Tag = newTextBox // Store the textbox in the button's Tag

                };

                button.Click += ButtonClose_Click; // Attach Click Event

                parentPanel.Controls.Add(newTextBox); // Add new TextBox to panel

                parentPanel.Controls.Add(button);

                newTextBox.Focus(); // Focus on the new TextBox

            }

        }

    }

    private void ButtonClose_Click(object sender, EventArgs e)

    {

        System.Windows.Forms.Button btn = sender as System.Windows.Forms.Button;

        System.Windows.Forms.TextBox txtBox = btn.Tag as System.Windows.Forms.TextBox;

        Panel parentPanel = btn.Parent as Panel;

        if (txtBox != null)

        {

            parentPanel.Controls.Remove(txtBox);

            parentPanel.Controls.Remove(btn);

        }

    }

    private void Label_Click(object sender, EventArgs e)

    {

        Label lbl = sender as Label;

        if (lbl != null)

        {

            string url = lbl.Text.ToString();

            MessageBox.Show("Label clicked: " + url);

            // Example: Open URL in default browser

            System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo

            {

                FileName = url,

                UseShellExecute = true

            });

        }

    }


    // Click Event for Panel

    private void Panel_Click(object sender, EventArgs e)

    {

        //Panel pnl = sender as Panel;

        //if (pnl != null)

        //{

        //    string url = pnl.Tag.ToString();

        //    MessageBox.Show("Panel clicked: " + url);

        //}

    }

Previous
Next Post »