简体   繁体   中英

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.

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");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM