[RESOLVED] Get child controls in a control
Hi I'm trying to iterate through controls from main form. I'm getting 2 controls while I'm expecting 4. Here's my code so far
C# Code:
public Object GetOption()
{
Object o = new Object();
Control con = new Control();
Size size = new Size
{
Width = 90,
Height = 12
};
CheckBox checkBox = new CheckBox
{
Name = "SomeCheckBox",
Checked = true,
Location = new Point(p.X, p.Y),
Size = size
};
ComboBox comboBox = new ComboBox
{
Name = "SomeComboBox",
Text = "Some text in CB",
Location = new Point(Convert.ToInt32(p.X + 25), p.Y),
Size = size
};
TextBox count = new TextBox
{
Name = "CountTextBox",
Text = "20",
Size = size,
Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
};
TextBox date = new TextBox
{
Name = "DateNow",
Text = System.DateTime.Now.ToString("dd.MM.yyyy"),
Size = size,
Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
};
con.Controls.AddRange(new Control[] { checkBox, comboBox, count, date });
// Next position
p.X = 25;
p.Y = p.Y + 14;
o = con;
return o;
}
EDIT: I forgot to give the loop
C# Code:
private void Button1_Click(object sender, EventArgs e)
{
Control control = new Control();
control = (Control)GetOption();
foreach (Control c in control.Controls)
{
MessageBox.Show("Type of control: " + c.GetType().ToString());
Options_Pnl.Controls.Add(c);
}
}
Re: Get child controls in a control
You're setting the Location of three of the controls to the same value, so you'll only see one of them. You should probably be using a TableLayoutPanel or FlowLayoutPanel to contain those controls so there'd be no need to set the Location.
Re: Get child controls in a control
Hi, I edited my post. The problem isn't location, I get 2 dialogs in my loop instead of 4.
Re: Get child controls in a control
Firstly, your code is ridiculously convoluted. Why would you make it so crazy?
Secondly, I think I see the reason for the issue but I'm actually surprised that it doesn't throw an exception. To test my theory, comment out this line:
csharp Code:
Options_Pnl.Controls.Add(c);
and run the code again and I think you'll see four messages.
Re: Get child controls in a control
A control can only have one parent at a time so the line I suggested commenting out is actually removing that control from the Controls collection it's already in. What I think is happening is that, after two iterations of your loop, you have removed two controls from the original Controls collection and that leaves two. There's two controls and you've done two iterations so the loop thinks it's finished. Normally, modifying a collection while enumerating it causes an exception to be thrown but maybe it's not in this case because of the indirect way you're making that modification.
Like I said, your code is rather crazy anyway. If the aim is to create four controls in a method and add them to Options_Pnl then you can simplify that a lot:
csharp Code:
public Control[] GetOption()
{
Size size = new Size
{
Width = 90,
Height = 12
};
var controls = new Control[]
{
new CheckBox
{
Name = "SomeCheckBox",
Checked = true,
Location = new Point(p.X, p.Y),
Size = size
};
new ComboBox
{
Name = "SomeComboBox",
Text = "Some text in CB",
Location = new Point(Convert.ToInt32(p.X + 25), p.Y),
Size = size
},
new TextBox
{
Name = "CountTextBox",
Text = "20",
Size = size,
Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
},
new TextBox
{
Name = "DateNow",
Text = System.DateTime.Now.ToString("dd.MM.yyyy"),
Size = size,
Location = new Point(Convert.ToInt32(p.X + 25), p.Y)
}
};
// Next position
p.X = 25;
p.Y = p.Y + 14;
return controls;
}
csharp Code:
Options_Pnl.Controls.AddRange(GetOption());
That said, you really ought to be designing a user control that contains those four children and simply adding instances of that to a TableLayoutPanel or FlowLayoutPanel.
Re: Get child controls in a control
Hi thanks for helping. I did it like this
C# Code:
public Object GetOption()
{
Object o = new Object();
/*
TextBox1 = new TextBox
{
Location = p,
Size = new Size(110, 40),
Text = "Static"
};
*/
Size size = new Size
{
Width = 30,
Height = 12
};
CheckBox checkBox = new CheckBox
{
Name = "SomeCheckBox",
Checked = true,
Location = new Point(p.X, p.Y),
Size = size
};
ComboBox comboBox = new ComboBox
{
Name = "SomeComboBox",
Location = new Point(Convert.ToInt32(p.X + 25), p.Y),
Size = size
};
TextBox count = new TextBox
{
Name = "CountTextBox",
Text = "20",
Size = size,
Location = new Point(Convert.ToInt32(p.X + 45), p.Y)
};
TextBox date = new TextBox
{
Name = "DateNow",
Text = System.DateTime.Now.ToString("dd.MM.yyyy"),
Size = size,
Location = new Point(Convert.ToInt32(p.X + 75), p.Y)
};
comboBox.Items.Add("test 1");
comboBox.Items.Add("test 2");
o = new Control[] { checkBox, comboBox, count, date };
// Next position
p.X = 25;
p.Y = p.Y + 24;
return o;
}
private void AddOption_Btn_Click(object sender, EventArgs e)
{
Control[] controls = (Control[])GetOption();
for (int i = 0; i <= controls.Count() - 1; i++)
{
controls[i].Parent = Options_Pnl;
Options_Pnl.Controls.Add(controls[i]);
}
}
I don't want to simplify it there's to much things to do I just hope I'm not doing it wrong.
Re: [RESOLVED] Get child controls in a control
That will produce the desired result but you are doing some rather silly things.
1.
csharp Code:
public Object GetOption()
Why on Earth would you declare the return type of a method Object when you know for a fact that it will be returning a Control array and you are then going to cast that return value as a Control array when you call the method? Why would you not declare it as the type it is and then use it as that type without a cast?
2.
What is the point of that Object array? You create it, assign it to that variable and never use it, then finally assign a different object to the same variable here:
csharp Code:
o = new Control[] { checkBox, comboBox, count, date };
Why would you create an object of any type that you never use?
3.
csharp Code:
for (int i = 0; i <= controls.Count() - 1; i++)
Why use a 'for' loop and only ever use the loop counter as an index into a list when you could use a 'foreach' loop over that list?
4.
csharp Code:
controls[i].Parent = Options_Pnl;
Options_Pnl.Controls.Add(controls[i]);
Those two lines do effectively the exact same thing. Why use them both?
5. Why use a loop and either of the lines you've got in it when you could just make one call to AddRange, as I've already demonstrated.
6.
Quote:
I don't want to simplify it
Why would you ever not want to simplify code? It's part of the process of learning if nothing else, and it makes maintenance of that code much easier.
Re: [RESOLVED] Get child controls in a control
I've already fixed them except for 3 and 4 :wave: