如何使用一些动态创建的控件处理两个 FlowLayoutPanel 的点击事件?

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

我在同一个窗体上有两个 FlowLayoutPanel 控件,两个控件上都有一些控件。 O想要的是,如果单击FlowLayoutPanel1控件,我想更改label1.Text,如果单击FlowLayoutPanel2控件,我想更改label2.Text

这是我在 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);
    }
}

点击事件代码:

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;
    }
}

如何知道单击了哪些 FlowLayoutPanel 控件?

我可以通过创建多个方法来做到这一点,但我想在同一个方法上完成这项工作。

有关更多说明,请参阅此图片:

您可以使用 FlowLayoutPanel 的 ControlAdded and ControlRemoved 事件来 subscribe/unsubscribe 一个根据点击的控件更改标签文本的事件:

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;
}

当然,您需要先订阅这些事件,方法是从属性 window 中选择事件,或者调用以下命令:

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

现在没有电脑可以检查,但我想这应该可以

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

您可以向 RadioButtonTag 属性 添加更多信息。例如,您可以:

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

这样,您可以使用string.Split()Flavour_nameFlowLayoutPanel名字。但是由于 Tag 属性 接受对象,您可以创建新的 class 来保存信息。

使用这种方法,您可以使用:

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