Hi !!!

I have two Windows Forms. First Windows Form contain one Panel Control. This Panel Control contain other 5 child Label Controls. I wish from Second Windows Form get the number of child controls in Panel Control in First Windows Form and there Location?

// First Windows Form

public partial class FirstWForm : Form
{
public FirstWForm
{
    InitializeComponent();
}

private void AddLabelControlsToPanel()
{
    int position_X = 0;
    int position_Y = 0;

    for(int i = 0; i < 5; i++)
    {
       Label labelControl = new Label();
       labelControl.Point = 
            new Point(position_X, position_Y);

       this.panelControl.Controls.Add(labelControl);

       // put Label Controls in one under another ???
       position_Y += labelControl.Height
    }
}

public int CountChildInPanelControl()
{
    Control.ControlCollection ctrlCollection = this.panelControl.Controls;
    int countChild = ctrlCollection.Count;

    return countChild;
}
}


// Second Windows Form

public class SecondWForm
{
  private void Method()
  {
     FirstWForm firstWForm = new FirstWForm();
     int childInPanelControl = firstWForm.CountChildInPanelControl();

     // Some code here ...
     // Some code here ...
     // Some code here ...
     // Some code here ...
  }
}

In Second Windows Form when I call CountChildInPanelControl() method the returnet result is always zero '0'. Why ??? I don't understand !!! Sorry for my bad English !!!

Dani AI

Generated

As hinted, the primary cause is that new FirstWForm() in the second form creates a fresh instance that is not the same as the already-displayed FirstWForm. Runtime-added controls belong to the instance they were added to, so calling a count method on a different instance will return 0. Also verify that AddLabelControlsToPanel() actually runs on the instance being inspected (for example, in the form's Load handler) and that label positions use the Location property (there is no Point property on Control).

Example patterns that work (keep the panel private and expose minimal access):

// In FirstForm
public int GetPanelChildCount() =>
    panel1.Controls.Count;

public List<Point> GetPanelChildLocations() =>
    panel1.Controls.Cast<Control>().Select(c => c.Location).ToList();

// In SecondForm (receive reference to the open FirstForm)
private readonly FirstForm _owner;
public SecondForm(FirstForm owner) { _owner = owner; }
private void Demo() {
    int count = _owner.GetPanelChildCount();
    var locs = _owner.GetPanelChildLocations();
}

Alternate: locate the running form instance via Application.OpenForms:

var first = Application.OpenForms.OfType<FirstForm>().FirstOrDefault();
if (first != null) { /* call its public methods above */ }

Troubleshooting notes: ensure code queries the panel after labels are added (or raise an event from FirstForm when ready); use OfType<Label>().Count() to count only labels; access controls on the UI thread (use Invoke if needed). This keeps the design safe and avoids inspecting a different form instance.

Recommended Answers

All 2 Replies

Where are you calling

AddLabelControlsToPanel()

method from?
I do not see it being called here, or am I missing something?

Is not important where i call AddLabelControlsToPanel() method. I wish to know how to get count of child controls in the Panel Control.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.