简体   繁体   English

如何获取动态添加到Windows窗体c#中的控件的值?

[英]How to obtain the value of a control added dynamically into Windows Form c#?

I read some articles and don't managed solved my problem, My problem is in the moment when I try obtain the value of the controls (CheckBox and ComboBox) added dynamically into Windows Form, I need know when the CheckBox is checked (or unchecked) and if the ComboBox is empty (or not) when I press a button, this button call a method in which I validate if all components are empty, I add the controls the following way: 我读了一些文章并且没有管理解决了我的问题,我的问题是在我尝试获取动态添加到Windows窗体中的控件(CheckBox和ComboBox)的值时,我需要知道何时选中CheckBox(或取消选中) )当按下按钮时,如果ComboBox为空(或不是),则此按钮调用一种方法,在该方法中我验证所有组件是否为空,我按以下方式添加控件:

 CheckBox box;
 ComboBox cmBox;
 for (int i = 1; i <= sumOfRegisters; i++)
 {
    box = new CheckBox();
    box.Name = "CheckBox" + i;
    box.Text = "Some text";
    box.AutoSize = true;
    box.Location = new Point(10, i * 25); //vertical

    cmBox = new ComboBox();
    cmBox.Name = "ComboBox" + i;
    cmBox.Size = new System.Drawing.Size(302, 21);
    cmBox.TabIndex = i;
    cmBox.Text = "Some Text";
    cmBox.Location = new Point(270, i * 25);

    this.groupBox.Controls.Add(cmBox);
    this.groupBox.Controls.Add(box);
}

"I add the values from database in the case of the ComboBox, I omitted this part." “我在ComboBox的情况下添加数据库中的值,我省略了这一部分。”

I try obtain the value with a foreach: 我尝试用foreach获取值:

foreach (Control ctrl in groupBox.Controls)

The problem is I don't have idea how to know if the Control (CheckBox and ComboBox) is checked or empty (as the case). 问题是我不知道如何检查Control(CheckBox和ComboBox)是否被检查或为空(视情况而定)。

Really thanks for any help, I appreciate your time. 非常感谢您的帮助,感谢您的时间。

You are throwing away your reference to each dynamically allocated control after adding it to grbMateias. 将它添加到grbMateias后,您将丢弃对每个动态分配的控件的引用。

One option is certainly to store those references, for example in a List<Control> . 一种选择当然是存储这些引用,例如在List<Control>

List<ComboBox> comboBoxes = new List<ComboBox>();

for (int i = 1; i <= sumOfRegisters; i++)
{
    box = new CheckBox();
    comboBoxes.Add(box);
    // etc.
}

You can however just iterate the controls in grbMateias 但是,您可以只迭代grbMateias中的控件

 foreach(Control ctl in grbMateias.Controls)
 {
     if (ctl is CheckBox)
     {
         // Use the checkbox
     }    
     else if (ctl is ComboBox)
     {
         // Use the ComboBox
     }    
 }

or using Linq 或使用Linq

var comboBoxes = grbMateias.Controls.OfType<ComboBox>();
var checkBoxes = grbMateias.Controls.OfType<CheckBox>();

You could use the as operator, like so: 您可以使用as运算符,如下所示:

foreach (Control ctrl in groupBox.Controls)
{
    CheckBox checkBox = ctrl as CheckBox;
    ComboBox comboBox = ctrl as ComboBox;

    if (checkBox != null)   // Control is a CheckBox
    {
        if (checkBox.Checked)
        {
            // CheckBox is checked
        }
        else
        {
            // CheckBox is not checked
        }
    } 
    else if (comboBox != null)  // Control is a ComboBox
    {
        if (comboBox.Items.Count == 0)
        {
            // ComboBox is empty
        } 
        else
        {
            // ComboBox isn't empty
        }
    }
}

Since you're giving each control a name within the loop, it's easy to use it to recognize each individual control, as long as names are unique. 由于您在循环中为每个控件赋予一个名称,因此只要名称是唯一的,就可以很容易地使用它来识别每个控件。 For example, to obtain the third checkbox you can use something like this: 例如,要获取第三个复选框,您可以使用以下内容:

Control control = groupBox.Controls.Single(x => x.Name == "CheckBox3");
Checkbox thirdCheckBox = (CheckBox)control;
bool checkBoxCheched = thirdCheckBox.Checked;

First you need to know what control you're currently working with when iterating, then you can cast it and use that specific control's methods. 首先,你需要知道迭代时你正在使用什么控件,然后你可以转换它并使用特定控件的方法。

foreach(Control c in groupBox.Controls)
{
    if(c is ComboBox)
    {
         if((ComboBox) c).SelectedItem == null)
            //handle nothing entered
    }
    else if(c is CheckBox)
    {
        if((CheckBox) c).Checked)
            //handle checked
    }
}

In the above snippit we check using the is keyword what type of Control it is. 在上面的snippit中,我们使用is关键字检查它是什么类型的Control。 Then if it's of a control that we know how to handle we cast it and check if empty or checked. 然后,如果它是我们知道如何处理的控件,我们将其转换并检查是否为空或已检查。

I need know when the CheckBox is checked (or unchecked) and if the ComboBox is empty (or not). 我需要知道何时检查(或取消选中)CheckBox以及ComboBox是否为空(或不是)。

To get the states of your checkboxes and comboboxes, including the index you used to create them, you can do the following: 要获取复选框和组合框的状态,包括用于创建它们的索引,可以执行以下操作:

var checkBoxStates = groupBox.Controls
    .OfType<CheckBox>
    .Where(x => x.Name.StartsWith("CheckBox"))
    .Select(x => new {
        Index = Int.Parse(x.Name.Substring("CheckBox".Length)), 
        IsChecked = x.Checked
    })
    .ToList();

var comboBoxStates = groupBox.Controls
    .OfType<ComboBox>
    .Where(x => x.Name.StartsWith("ComboBox"))
    .Select(x => new {
        Index = Int.Parse(x.Name.Substring("ComboBox".Length)), 
        IsEmpty = x.Items.Count == 0
    })
    .ToList();

暂无
暂无

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

相关问题 如何在C#中保存动态添加的控件 - How to Save Dynamically Added Control in C# C#Windows窗体:如何在没有Controls.Add的情况下将控件添加到InitializeComponent()下的窗体 - C# Windows Forms: How the Control is added to the Form under InitializeComponent() without Controls.Add 如何使用C#中预先存在的Windows窗体在选项卡控件中动态创建选项卡页 - How to dynamically create Tab Pages in a Tab Control with a pre-existing Windows Form in C# 如何在C#Windows Form应用程序中控制ListView控件的ScrollBar值? - How to control ScrollBar value of a listview control in C# Windows Form Application? 如何创建/设置c#Windows表单用户控件属性获取简单消息弹窗windows - How to create/ set c # Windows form user control properties to obtain simple message pop-up windows C#Windows表单图表控件将相同值元素分组 - C# Windows Form chart control grouping same value elements 如何从使用JavaScript动态添加的文本框中获取价值-C# - How to get value from textbox dynamically added with javascript - c# 如何从wpf c#中动态添加的组合框获取价值? - how to get value from dynamically added combobox in wpf c#? C#链接两个动态添加的控件。 (Windows窗体应用程序) - c# link two dynamically added controls. (Windows form application) C#ASP.NET Wrap通过动态添加的Label控件动态添加了CheckBox控件 - C# ASP.NET Wrap dynamically added CheckBox Control with a dynamically added Label Control
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM