简体   繁体   English

如何在控件上使用foreach()来操纵另一个控件?

[英]How do I use foreach() on a control to manipulate another control?

I'm making a UI in winform, I'm using VS2015 to create the winform. 我正在用Winform制作UI,正在使用VS2015创建Winform。 The UI is subdivided into multiple pages by a tab control. 用户界面通过标签控件细分为多个页面。 In one of the tab page along with other things (picture box, text box, buttons etc.) I have 8 check box named cb1 ~ cb8, and 8 horizontal scroll bar named scrjoint1 ~ scrjoint8. 在选项卡页面之一以及其他内容(图片框,文本框,按钮等)中,我有8个名为cb1〜cb8的复选框,以及8个水平滚动条,分别名为scrjoint1〜scrjoint8。

The scroll bar is used to set the angle value in RotateTransform() for objects drawn in the picture box. 滚动条用于在RotateTransform()中为在图片框中绘制的对象设置角度值。 What I wanted to do is: 我想做的是:

1.If cb1 is check, scrjoint1 is 0. 1.如果检查cb1,则scrjoint1为0。

  • Else scrl1 is whatever the user set it to be. 其他的scrl1都是用户设置的。

2.If cb2 is check, scrljoint2 is 0. 2.如果检查cb2,则scrljoint2为0。

  • Else scrljoint2 is whaterver the user set it to be. 无论用户将其设置为scrljoint2。

3.And so on for the other checkbox and horizontal scroll. 3.依次类推其他复选框和水平滚动。

Instead of checking each check box individually and then assigning the value of scrljint respectively, what I had in mind is to achieve by using foreach() function as in the following format: 我想到的不是通过单独检查每个复选框,然后分别分配scrljint的值,而是要通过使用foreach()函数来实现,格式如下:

private void scrJoint_Scroll(object sender, ScrollEventArgs e)
{
    foreach (var cbControl in this.Controls)
    {
        if (cbControl is CheckBox)
        {
            if (((CheckBox)cbControl).Checked)
            {
                //set scrolJoint1~8 = 0 according to checkbox that is checked
            }
            else
            {
                //Do nothing
            }
        }
    }
}

But the current issue I'm having now is,when there is one or more check box that is checked (let say cb1 and cb2 is check) how do I set the respective horizontal scroll bar ( scrJoint1 and scrJoint2 ) to 0 in: 但是我现在遇到的问题是,当选中一个或多个复选框时(假设cb1和cb2被选中),如何在以下位置将各个水平滚动条( scrJoint1scrJoint2 )设置为0:

if (((CheckBox)cbControl).Checked)
{
    //set scrolJoint1~8 = 0 according to checkbox that 
}
else
{
    //Do nothing
}

Couple more questions I liked to ask in respect to what I'm trying to do: 关于我想做的事情,我想问几个问题:

1.Is using foreach() a good idea in what I'm trying to achieve? 1.在我要实现的目标中使用foreach()是个好主意吗?

2.Would it be better if I just check it one by one using If-Else (although I don't want to use this method, because I might need to include more check boxes and scroll bars later, and the code will get ridiculously long very fast). 2.如果我只使用If-Else逐个检查它会更好(尽管我不想使用此方法,因为以后可能需要包括更多的复选框和滚动条,并且代码会变得可笑长非常快)。

3.Is there another more cunning ways (clean and short method is preferred) of doing what I wanted to achieve that you guys can suggest (if there is, can you provide an example). 3.是否还有其他更狡猾的方法(首选简洁和简短的方法)来完成你们可以建议的目标(如果有的话,您能提供一个例子)吗?

I suppose you are doing right approach. 我想您正在采取正确的方法。 If possible use @Ofir Winegarten suggestion. 如果可能,请使用@Ofir Winegarten建议。 else wise you can little bit modify your method.and please place proper exception handling as well. 否则,您可以稍微修改您的方法。也请放置适当的异常处理。

this.Controls.Cast<Control>().Where(row => row.GetType().IsAssignableFrom(typeof(CheckBox)) && ((CheckBox)row).Checked).All(row => SetScroll(((CheckBox)row)));

where setScroll is a method or 其中setScroll是方法或

foreach (var cbControl in this.Controls.Cast<Control>().Where(row => row.GetType().IsAssignableFrom(typeof(CheckBox)) && ((CheckBox)row).Checked))

A Simple solution might be: 一个简单的解决方案可能是:

Subscribe to the CheckedChanged event of each of the individual checkboxes. 订阅每个复选框的CheckedChanged事件。
Inside these methods, set the related scrollbar.Value to 0 and disable it ( scrollBar.Enabled = false ). 在这些方法中,将相关的scrollbar.Value设置为0并禁用它( scrollBar.Enabled = false )。

private void CbOnCheckedChanged_1(object sender, EventArgs eventArgs)
{
    SetScrollBarValue(this.cb1, this.scrjoint1)
}

private void CbOnCheckedChanged_2(object sender, EventArgs eventArgs)
{
    SetScrollBarValue(this.cb2, this.scrjoint2)
}

....

private static void SetScrollBarValue(CheckBox cb, ScrollBar sb)
{
    if (cb.Checked)
    {
        sb.Value = 0;
        sb.Enabled = false;
    }
    else sb.Enabled = true;
}

You will have the same method of each checkbox. 每个复选框的方法相同。

A more generalized way would be to subscribe to the different checkboxes events with the same method and there, using one of the techniques suggested above, get the related scrollbar. 更通用的方法是使用相同的方法订阅不同的复选框事件,然后使用上面建议的一种技术获取相关的滚动条。 In the following example I chose the shortest one - search by name: 在以下示例中,我选择了最短的一个-按名称搜索:

private void CbOnCheckedChanged(object sender, EventArgs eventArgs)
{
    CheckBox cb = (CheckBox) sender;
    ScrollBar sb = (ScrollBar) this.Controls.Find("scrJoint" + cb.Name.Last());
    SetScrollBarValue(cb, sb);
}

Another quite short way is to store the id in the Tag property of each control ( Checkbox and ScrollBar ) and then using linq get the scrollbar: 另一个很短的方法是将id存储在每个控件的Tag属性( CheckboxScrollBar )中,然后使用linq获取滚动条:

private void CbOnCheckedChanged(object sender, EventArgs eventArgs)
{
    CheckBox cb = (CheckBox) sender;
    ScrollBar sb = 
        this.Controls.OfType<ScrollBar>().Single(bar => (int)bar.Tag ==(int)cb.Tag);    
    SetScrollBarValue(cb, sb);
}

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

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