Usama
Usama

Reputation: 77

How to handle click event of two FlowLayoutPanel with some dynamically created controls?

I have two FlowLayoutPanel controls on the same form with some controls on both of them. What O want is that if FlowLayoutPanel1 controls are clicked, I want to change label1.Text and if FlowLayoutPanel2 controls are clicked, I want to change label2.Text.

Here is my code to add controls in both FlowLayoutPanel.

public void Load_DFlavours(FlowLayoutPanel FLP)
{
    try
    {
        FLP.Controls.Clear();
        using (SQLiteConnection con = new SQLiteConnection(AppSettings.ConnectionString()))
        {
            con.Open();
            using (SQLiteDataAdapter sda = new SQLiteDataAdapter("Select distinct(Flavour_Name) From Flavours Where Category_Name = 'Flavours' Order By Flavour_Name", con))
            {
                DataTable dt = new DataTable();
                sda.Fill(dt);
                foreach (DataRow dr in dt.Rows)
                {
                    RadioButton rb2 = new RadioButton();
                    rb2.AutoSize = true;
                    rb2.Font = new Font("Segoe UI Semilight", 10F);
                    rb2.Margin = new Padding(2);
                    rb2.Text = dr["Flavour_Name"].ToString();
                    rb2.UseVisualStyleBackColor = true;
                    rb2.Tag = dr["Flavour_Name"].ToString();

                    FLP.Controls.Add(rb2);

                    rb2.CheckedChanged += Rb2_CheckedChanged;
                }
            }
            con.Close();
        }
    }
    catch (SQLiteException se)
    {
        MessageBox.Show(se.Message);
    }
}

Clickevent Code:

private void Rb2_CheckedChanged(object sender, EventArgs e)
{
    RadioButton rb2 = (RadioButton)sender;
    string flavour = rb2.Tag.ToString();

    //I want to do something here if flowlayoutPanel 1 control is 
    clicked change the label1.Text and if flowlayoutPanel 2 control is 
    clicked change the label2.text

    //I have tried this
    if(rb2.Checked)
    {
        label1.text = flavour;
    }
}

How to know which FlowLayoutPanel Controls are clicked?

I can do this by creating multiple methods but I want to do this work on the same method.

For more clarification, see this image:

See the image

Upvotes: 1

Views: 1189

Answers (3)

user3153340
user3153340

Reputation: 189

Don't have a computer to check right now, but I assume this should work

if (rb2.Checked)    
{
    if (rb2.Parent.Name == "flowlayoutPanel1")
    {
        label1.Text = flavour;
    }
    else if (rb2.Parent.Name == "flowlayoutPanel2")
    {
        label2.Text= flavour;
    }
}

Upvotes: 1

41686d6564
41686d6564

Reputation: 19641

You can use the ControlAdded and ControlRemoved events of the FlowLayoutPanel to subscribe/unsubscribe to an event which changes the text of the label based on the clicked control:

private void flowLayoutPanel1_ControlAdded(object sender, ControlEventArgs e)
{
    e.Control.Click += flowLayoutPanel1_ControlClicked;
}

private void flowLayoutPanel1_ControlRemoved(object sender, ControlEventArgs e)
{
    e.Control.Click -= flowLayoutPanel1_ControlClicked;
}

private void flowLayoutPanel1_ControlClicked(object sender, EventArgs e)
{
    var control = (Control)sender;
    label1.Text = control.Text;
}

private void flowLayoutPanel2_ControlAdded(object sender, ControlEventArgs e)
{
    e.Control.Click += flowLayoutPanel2_ControlClicked;
}

private void flowLayoutPanel2_ControlRemoved(object sender, ControlEventArgs e)
{
    e.Control.Click -= flowLayoutPanel2_ControlClicked;
}

private void flowLayoutPanel2_ControlClicked(object sender, EventArgs e)
{
    var control = (Control)sender;
    label2.Text = control.Text;
}

And of course, you need to subscribe to these events first, either by selecting the event from the properties window, or by calling the following:

flowLayoutPanel1.ControlAdded += flowLayoutPanel1_ControlAdded;
flowLayoutPanel1.ControlRemoved += flowLayoutPanel1_ControlRemoved;
flowLayoutPanel2.ControlAdded += flowLayoutPanel2_ControlAdded;
flowLayoutPanel2.ControlRemoved += flowLayoutPanel2_ControlRemoved;

Upvotes: 0

Fahmi Noor Fiqri
Fahmi Noor Fiqri

Reputation: 646

You can add more information to Tag property of the RadioButton. For example, you can:

rb2.Tag = $"{dr["Flavour_Name"]}|{FLP.Name}";

This way, you can use string.Split() to take the Flavour_name and FlowLayoutPanel name. But since the Tag property accept object, you can create new class to hold the information.

Using this approach, you can use:

RadioButton rb2 = (RadioButton)sender;
string[] splits = rb2.Tag.ToString().Split('|');
string flavour = splits[0];
string flowPanelName = splits[1];

Upvotes: 0

Related Questions