在foreach循环中自动替换groupbox [英] Replacing groupbox automatically in foreach loop
问题描述
大家好
我有6个名为gBox1,gBox2,gBox3,gBox4,gBox5,gBox6的GroupBox,每个gBox包含21个名为slot1,slot2的ComboBox, slot3,slot4,slot5 ....所以我想只选择7个组合框进行操作。
如果用户在文本框中输入2,那么从gBox1-> cb15,slot1, slot2,slot3,slot4,slot5,slot6,slot7和来自gBox2-> slot8,slot9,slot10,slot11,slot12,slot13文本的所有comboBox都应该存储在一个字符串数组中。
现在问题是我能够像这样迭代ComboBox(帮助我从代码项目获得)
Hi All
I have 6 GroupBox named as gBox1,gBox2,gBox3,gBox4,gBox5,gBox6 AND each gBox contain 21 ComboBox named as slot1,slot2,slot3,slot4,slot5.... so on out of which I want to select only 7 comboBoxes for operation.
For eaxmple if user enter 2 in textbox then from gBox1->cb15,slot1,slot2,slot3,slot4,slot5,slot6,slot7 and from gBox2->slot8,slot9,slot10,slot11,slot12,slot13 text of all comboBox should be stored in a string array.
Now problem is I am able to Iterate on ComboBox like this (help I got from code project for this)
if (no_of_period == 2)
{
if (duration == 1)
{
foreach (var comboBox in gBox2.Controls.OfType<ComboBox>().OrderBy(m => m.Name))
{
if (comboBox.Name.Substring(0, 4).Equals("slot"))
{
comboBox.Text = i.ToString("HH:mm");
}
}
i = i.AddMinutes(60);
foreach (var comboBox in gBox3.Controls.OfType<ComboBox>().OrderBy(m => m.Name))
{
if (comboBox.Name.Substring(0, 4).Equals("slot"))
{
comboBox.Text = i.ToString("HH:mm");
}
}
}
现在我必须为所有6个gBox写这个代码有没有办法自动gBox应该改变......像这样的东西 - 不正确的代码
Now I have to write this code for all 6 gBox is there any way to automatically gBox should change... Something like this- NOT CORRECT CODE
foreach (var sgrpBox in batch_creation.ActiveForm.Controls.OfType<GroupBox>().OrderBy(g => g.Name))
{
string gname = null;
if (sgrpBox.Name.Substring(0, 4).Equals("gBox"))
{
gname = sgrpBox.Name.Substring(0, 4) + G;
foreach (var comboBox in ganme.Controls.OfType<ComboBox>().OrderBy(m => m.Name))
{.......
需要指导
关注所有
Need guidance
Regards to all
推荐答案
尝试这样一个简单的循环 -
Try a simple loop like this -
foreach (Control c in this.Controls)
{
if(c is GroupBox)
{
// Do stuff here ;
}
}
这篇关于在foreach循环中自动替换groupbox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!