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

时间:2018-09-16 06:02:06

标签: c# winforms flowlayoutpanel

我在同一表单上有两个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);
    }
}

Clickevent代码:

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控件?

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

有关更多说明,请参见以下图片:

See the image

3 个答案:

答案 0 :(得分:1)

现在没有计算机可以检查,但是我认为这应该可以工作

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

答案 1 :(得分:0)

您可以使用FlowLayoutPanel的ControlAddedControlRemoved事件来订阅/取消订阅一个事件,该事件根据单击的控件来更改标签的文本:

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

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

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

答案 2 :(得分:0)

您可以将更多信息添加到Tag的{​​{1}}属性中。例如,您可以:

RadioButton

这样,您可以使用rb2.Tag = $"{dr["Flavour_Name"]}|{FLP.Name}"; 来命名string.Split()Flavour_name。但是由于FlowLayoutPanel属性接受对象,因此您可以创建新类来保存信息。

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

Tag