Amin .D
Amin .D

Reputation: 3

Buttons in Flowlayoutpanel

i add a button for each data row in a flowlayoutpanel with like

foreach (var Item in query.OrderBy(x=> x.menu_sort))
            {
                var btn = new Button
                {
                    Name = Item.menu_name,
                    Text = Item.menu_description,
                    Tag = Item.menu_name,
                    Size = new Size(107, 50),
                    Font = new Font("B Nazanin",10)
                };

                MainPanel.Controls.Add(btn);

-----> i want to click on buttons and open a form. <-----

Upvotes: 0

Views: 1350

Answers (2)

Idle_Mind
Idle_Mind

Reputation: 39122

I want to click on buttons and open a form.

Wire up the Click() event for those buttons when you create them:

    private void Foo()
    {
        foreach (var Item in query.OrderBy(x=> x.menu_sort))
        {
            var btn = new Button
            {
                Name = Item.menu_name,
                Text = Item.menu_description,
                Tag = Item.menu_name,
                Size = new Size(107, 50),
                Font = new Font("B Nazanin",10)
            };

            btn.Click += Btn_Click;

            MainPanel.Controls.Add(btn);
        }
    }

    private void Btn_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        // now do something with "btn", maybe based on btn.Tag?
    }

Upvotes: 0

Alicia
Alicia

Reputation: 864

I think you want to add an event handler at runtime to a control, right? You can do it this way:

btn.Click += new EventHandler(button_Click);

Then, you have to implement the method button_Click somewhere, with the correct signature:

private void button_Click(object sender, System.EventArgs e)
{
   // Do stuff
   MyForm form = new MyForm();
   form.Show();
}

Upvotes: 1

Related Questions