Search code examples
c#winformsflowlayoutpanel

Open form on label double click inside flowlayout panel


Im trying to Double Click on a Label in a FlowLayoutPanel, The labels are created dynamically. Im trying to open the form using this

foreach(Label label in myFlp )
{
    var Id = label.Name.ToString();
    int personID;
    if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out personID))
    {
        FrmAddress frmAddress = new FrmAddress(_controller, personID);
        frmAddress.ShowDialog();
        frmAddress.Dispose();
    }
}

Getting this error;

foreach statement cannot operate on variables of type 'System.Windows.Forms.FlowLayoutPanel' because 'System.Windows.Forms.FlowLayoutPanel' does not contain a public definition for 'GetEnumerator'    

Solution

  • Try something like this :

    foreach(var control in myFLp.Controls)
    {
        if(control is Label)
        var Id = (Label)control.Name.ToString();
        int personID;
    
        if (!String.IsNullOrWhiteSpace(Id) && int.TryParse(Id, out personID))
        {
            FrmAddress frmAddress = new FrmAddress(_controller, personID);
            frmAddress.ShowDialog();
            frmAddress.Dispose();
        }
    }