RoomDiagram rd = new RoomDiagram();
maincDockPanel.Children.Remove(rd);
when i wright this nothing happens.
how i can remove child and then add new one? thanks
If you need to remove then you have to use dockPanel1.Children.RemoveAt() because Children is UIElement Collection you may write a small code to ietrate over the collection and see if it is your required control to remove then remove it similarly dockPanel1.Children.Add() to add UIElement same should be the case if you are using UserControl
some sample code to Iterate over collection and will show you the name of the controls
foreach (Control x in dockPanel1.Children)
{
MessageBox.Show(x.Name);
}
Related
I have a panel in my Windows form that I would like to add other controls like text boxes into it and also iterate over all elements in the panel to retrieve all the data in it.
Is this possible to do with a panel control?
I experimented with a foreach loop such as
foreach(textbox tb in panel1)
{
}
but I get an error saying panel does not have a public definition for GetEnumerator.
What would be a better control/container to use, where I can add more controls to it and eventually access all the controls within it an their data?
Update
Just a heads up - I am having some problems with adding multiple text boxes in code.
I create a textbox object and then add it to the panel, but only one shows up.
I have read elsewhere on this site and others that adding a textbox with the same name might be causing the problem.
To solve this, I replaced the panel with a flow layout panel which works great.
Hopefully this helps others.
It's the correct control to use. Try iterating the Controls property of the panel object.
foreach(Control control in panel.Controls)
{
if(control is TextBox)
{
TextBox textBox = control as TextBox;
//etc.
}
}
The following example clears all textbox in any control
void ClearTextBoxes(Control parent)
{
foreach (Control child in parent.Controls)
{
TextBox textBox = child as TextBox;
if (textBox == null)
ClearTextBoxes(child);
else
textBox.Text = string.Empty;
}
}
Then whenever you want clear. you call
ClearTextBoxes(panel1);
You need to access Controls collection of the Panel, better if you do:
foreach(Textbox tb in panel1.Controls.OfType<TextBox>)
But the above would give you TextBoxes inside the panel, not inside other controls inside the panel, if you want to get get textboxes recursively then see this question
You're missing one little thing. Try:
foreach (Control c in panel1.Controls)
And then check the control type if you have more than one type of control in it.
The reason for the error is that panel1 is an object, not a collection of objects, so you have to refer specifically to the collection of objects that panel1 contains.
Something like this
foreach (Control c in panel1.Controls)
{
if(c.GetType() == typeof(TextBox))
{
//do stuff
}
}
I am using flowLayoutPanel and adding my custom user controls to it, I can add mu usercontrol to it but i don't know how can it use it after adding.
in this part i add my user controls:
...
ExtensionUserControl extension = new ExtensionUserControl(this, AMI_ClientInstance);
//Add Obj Name (Extension Number)
extension.ExtensionNumber = Obj.ObjName;
flowLayoutPanel1.Controls.Add(extension as ExtensionUserControl);
...
and another place i want to have the properties of my added user control, i try to user this code but it get error, It says that can not convert windows control to ExtensionUserControl
ExtensionUserControl extension = flowLayoutPanel1.Controls[1];
please totally tell me how can i have my user control properties after adding it to panel?
thanks
Are you sure Controls[1] is ExtensionUserControl?
I think that:
foreach(Control ctl in flowLayoutPanel1.Controls)
{
if(ctl is ExtensionUserControl)
{
(ExtensionUserControl)ctl......//do something u want
}
}
I'm not test it yet, just thinking, sorry if it doesn't work
I'm adding a couple of controls as a child controls of another custom control I've developed. Here's where I add the child controls (a custom label and a generic span control):
public static void AddLabel(this IExtendedControl control, string inheritableCssClass = "")
{
TestCLabel contentLabel = new TestCLabel();
contentLabel.Text = control.LabelText;
control.Controls.Add(contentLabel);
if (control.Required)
{
HtmlGenericControl requiredFieldIndicator = new HtmlGenericControl("span");
requiredFieldIndicator.Attributes["class"] = "requiredFieldIndicator";
requiredFieldIndicator.InnerText = " *";
control.Controls.Add(requiredFieldIndicator);
}
and I then do the following in the render method of the parent control:
protected override void Render(HtmlTextWriter w)
{
base.Render(w);
foreach (Control c in this.Controls)
{
c.RenderControl(w);
}
if (Required)
{
rfv.RenderControl(w);
}
}
but I get the error 'An entry with the same key already exists'. This is being caused by the attempt to manually render the child controls. I don't think I should need to do the manual rendering, but before I coded this in the controls weren't appearing (nothing appears in the HTML markup). Any ideas what's going on?
Looks like the manual rendering of the child controls wasn't the issue. I'd reworked the code
as I was originally assigning the child controls to a property of the parent control but changed this to add the child controls to the controls collection of the parent control (so that the child controls now go through their correct life cycle). I'd previously been manually rendering the child controls when they were properties of the parent control and was still doing this. When manually rendering all the children, the controls were being added a second time. Still doesn't answer:
Why is the manual adding necessary, as this should be automatic?
Im using WinForm C#
Have MainForm there is one panel where. my Inventory and Sell user controls are opening in panel. panel1.Controls.Add(inventory);
How to check if userControls are open?
When i check it i want to add tabControl. But i dont know how to add in tabPage controls without closing user control. Thanks
I mean if user control is already added in panel1.Controls. If its added gave name of user control
– Acid
How could the user control possibly be added to panel1.Controls without you knowing it? And if you added it yourself, you should already know the name of the user control.
Thus, all you have to do is loop through the controls in panel1.Controls and see if you find your user control. For example:
foreach (Control ctrl in panel1.Controls)
{
if (ctrl.Name == myUserControl)
{
// Found the control!
// (do something here...)
}
}
Alternatively, if you for whatever reason don't know the name of the control, you could still find all the controls of type UserControl that have been added to the panel's Controls collection. Like so:
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is UserControl)
{
// Found a UserControl!
// (do something here...)
}
}
Remember that the Tag property provided on every control gives you a way to uniquely identify it. You can check that property for matches, too, if you don't know the name.
Not sure what you mean by open, but you can handle the ControlAdded event on the Panel class to capture when a control is added...
panel1.ControlAdded += new ControlEventHandler(p_ControlAdded);
For example,I click button and then on the form appears GroupBox with some controls inside.One more click-one more GroupBox.This may lasts to infinity.Please,tell me how to do that???
You can create a new control and add it to the group you want to:
Your_Control ctl = new Your_Control();
Your_Parent_Control.Controls.Add(ctl);
Link to Control Class:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.aspx
Link to the controls property:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx
You can create controls in code just like any other object. Looks something like this.
GroupBox box = new GroupBox();
this.Controls.Add(box); // assuming "this" is your parent form
You can add controls to your group box in the same way. All Control objects have a Controls collection for child controls. Although it doesn't really make sense for some of them.