Jyina
Jyina

Reputation: 2892

how to access a control's value that is placed in a panel?

I have a DropDownList control in a panel and this panel is in turn placed in a SplitContainer's panel1. I changed the modifiers property to 'Public' for the DropDownList but I can't access this control from another class.

//created instance of the form
Payment pForm = new Payment();

I am able to access other controls which are placed outside of the split container as below.

string amount = pForm.tbAmount.Text;

But I can't access the dropdownlist control.

Upvotes: 1

Views: 1167

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038710

A split container has 2 panels, and each panel has a collection of controls, so:

ComboBox dropdown = pForm
    .SplitContainer1       // get the splitcontainer control of pForm
    .Panel1                // get the first panel of this container
    .Controls              // get the controls collection
    .OfType<ComboBox>()    // find all controls that are of type ComboBox
    .FirstOrDefault();     // get the first or null if none

Obviously in order to be able to access pForm.SplitContainer1 from outside of the Payment form class you will have to provide a public getter to it.

And if you wanted to further constrain by the name of the dropdown (assuming you had multiple dropdowns in this panel):\

ComboBox dropdown = pForm.
    .SplitContainer1
    .Panel1
    .Controls
    .OfType<ComboBox>()
    .FirstOrDefault(x => x.Name == "comboBox1");

Upvotes: 4

Related Questions