Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象列表的控件_C#_Visual Studio 2010 - Fatal编程技术网

C# 对象列表的控件

C# 对象列表的控件,c#,visual-studio-2010,C#,Visual Studio 2010,我正在尝试为多个对象创建某种类型的控件列表。该对象用于屏幕上显示的图案,并具有几个不同的属性,如持续时间、偏移量、下一个图案、图案索引。我希望能够创建一个可编辑的控件列表,以便在GUI中轻松设置每个模式的不同属性。这是我正在创建的表单的草稿。这是我正在创建的表单的链接 每一行都是一个模式的属性集合。我希望能够以某种方式迭代代码中的每一行,并根据用户输入设置每个模式的属性。从我到目前为止所读到的内容来看,我想使用ItemsControl工具并以某种方式将其绑定到GUI,但我不确定如何做到这一点,

我正在尝试为多个对象创建某种类型的控件列表。该对象用于屏幕上显示的图案,并具有几个不同的属性,如持续时间、偏移量、下一个图案、图案索引。我希望能够创建一个可编辑的控件列表,以便在GUI中轻松设置每个模式的不同属性。这是我正在创建的表单的草稿。这是我正在创建的表单的链接


每一行都是一个模式的属性集合。我希望能够以某种方式迭代代码中的每一行,并根据用户输入设置每个模式的属性。从我到目前为止所读到的内容来看,我想使用ItemsControl工具并以某种方式将其绑定到GUI,但我不确定如何做到这一点,或者我是否应该这样做。我现在使用的是一个包含多个面板的TableLayoutPanel,但这些工具似乎没有太多控制。我应该如何将每一行的控件组合在一起,然后高效地遍历每一行

好吧,我想我已经实现了你所追求的运动目标,我比WinForms更了解WPF,但这是我的解决方案,我只是自学成才

我假设在您的解决方案中这样做不会有任何问题

首先创建一个用户控件。我右键单击了WinForm项目>添加>用户控件。这就是要添加组成行的内容的地方。所以它应该是这样的,我将我的用户控件命名为RowContent:

确保在控件中有自己的名称。因此,对于复选框,我将其命名为stprIndex\u chkBox、enable\u chkBox等等

现在,您需要使用为每个控件实现所需的功能。因此,您需要更改
stprIndex\u chkBox.Text的值,以及
enable\u chkBox.Checked
,以供初学者使用。您还需要访问numericalUpDowns的
值。因此,在RowContent.cs中,我根据表单中需要的数据添加了getter和setter。下面是访问器的一个片段(请记住,您需要添加更多):

现在您可以看到,这些将允许您访问RowContent类之外的变量。让我们转到TablePanelLayout控件

我以创建RowContent的相同方式创建了一个额外的用户控件,但这次我将其命名为ContentCollection。我将用户控件的
AutoSize
设置为true,并将TableLayoutPanel(名为tableLayoutPanel1)放到它上面

为了节省时间,我将所有控件动态添加到行中,如下所示:

public partial class ContentCollection : UserControl
{
    public ContentCollection()
    {
        InitializeComponent();
        RowContent one = new RowContent();
        RowContent two = new RowContent();
        RowContent three = new RowContent();
        RowContent four = new RowContent();
        RowContent five = new RowContent();
        RowContent six = new RowContent();
        tableLayoutPanel1.Controls.Add(one);
        tableLayoutPanel1.Controls.Add(two);
        tableLayoutPanel1.Controls.Add(three);
        tableLayoutPanel1.Controls.Add(four);
        tableLayoutPanel1.Controls.Add(five);
        tableLayoutPanel1.Controls.Add(six);
        tableLayoutPanel1.SetRow(one, 0);
        tableLayoutPanel1.SetRow(two, 1);
        tableLayoutPanel1.SetRow(three, 2);
        tableLayoutPanel1.SetRow(four, 3);
        tableLayoutPanel1.SetRow(five, 4);
        tableLayoutPanel1.SetRow(six, 5);
    }
}
这给了我:

现在您可以看到,我们正在动态添加这些内容。我希望您能够想象在WinForm中使用此用户控件时如何“自定义”此用户控件。在同一个文件中,您需要添加更多的getter/setter/函数,这取决于您想做什么,就像其他用户控件一样;因此,
AddAdditionalRow(RowContext rc)
等等。实际上,我作弊了,并将
tableLayoutPanel
更改为
public
,以最终加快这一过程

最后,您有了将保存自定义对象的ContentCollection,现在需要将其添加到表单中

转到您的表单,打开工具箱并滚动到顶部以查看您的表单!将其拖放到主窗体和vio'la上。现在,要遍历行内容,它相当容易。因为我将我的用户控件拖放到表单上,所以我可以将其命名为(userControl12)并立即开始访问这些控件。查看我是如何每隔一个复选框添加复选标记并动态更改步进器索引的:

public partial class Form1 : Form
{
    static int i = 0;
    public Form1()
    {
        InitializeComponent();
        foreach (RowContent ctrl in userControl11.tableLayoutPanel1.Controls)
        {
            ctrl.SetChkBox1Text = i.ToString();
            if (!ctrl.IsEnabledChecked && i % 2 == 0)
                ctrl.IsEnabledChecked = true;
            i++;
        }
        foreach (RowContent ctrl in userControl12.tableLayoutPanel1.Controls)
        {
            ctrl.SetChkBox1Text = i.ToString();
            i++;
        }
    }
}


我不是说这是最好的设计。。。因为它不是,但它说明了如何做这样的事情。如果您有任何问题,请告诉我。

这是个好主意。我做了类似的事情。也许我会做一些改变,比如你必须让它更有效率。谢谢
public partial class Form1 : Form
{
    static int i = 0;
    public Form1()
    {
        InitializeComponent();
        foreach (RowContent ctrl in userControl11.tableLayoutPanel1.Controls)
        {
            ctrl.SetChkBox1Text = i.ToString();
            if (!ctrl.IsEnabledChecked && i % 2 == 0)
                ctrl.IsEnabledChecked = true;
            i++;
        }
        foreach (RowContent ctrl in userControl12.tableLayoutPanel1.Controls)
        {
            ctrl.SetChkBox1Text = i.ToString();
            i++;
        }
    }
}