简体   繁体   English

如何访问放置在面板中的控件值?

[英]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 . 我在面板中有一个DropDownList控件,而这个面板又放在SplitContainerpanel1 I changed the modifiers property to 'Public' for the DropDownList but I can't access this control from another class. 我将修改器属性更改为DropDownList 'Public',但我无法从另一个类访问此控件。

//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: 拆分容器有2个面板,每个面板都有一组控件,因此:

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. 显然,为了能够从Payment表单类的外部访问pForm.SplitContainer1 ,您必须为它提供一个公共getter。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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