Add UserControls on LayoutControl via Code - c#

i want to add some UserControls on a LayoutControl via Code. They should appear among each other. And this is the problem. I just become one UserControl to be visible.
I do following at the moment:
foreach (myClass tempMyClass in allObjectsFromMyClass)
{
// I create UserControl
ctrlProgramm programm = new ctrlProgramm();
// I set some label values and so on for Control
programm.BxProgrammInitialiseren(tempMyClass);
// I Add UserControl to the LayoutControl Controls Collection
LayoutControl.Controls.Add(programm);
}
I just can see the Control which is created in the first loop of the foreach.
Maybe they are recline one over the other? Maybe i need to add a Panel first?
Information: The LayoutControl is in Namespace DevExpress.XtraLayout and we are using DevExpress v2010 vol.2
regards

When adding controls into LayoutControl you should wrap these controls with LayoutControlItems as follows:
//Create a layout item and add it to the root group.
LayoutControlItem item1 = layoutControl.Root.AddItem();
item1.Name = "item1";
ctrlProgramm programm = new ctrlProgramm();
// Set the item's Control and caption.
item1.Control = programm;
item1.Text = "Program:";
Related help articles:
How to: Create layout items via code
How to: Create layout groups and items via code
To learn more about the LayoutControl concepts please refer the following articles:
Introducing the XtraLayoutControl
Tutorial: Creating a Simple Layout

Related

How to add children to a specific to several grids

I have a WPF Project, Main window contains several grids.
After creating Textboxes and -blocks in a loop I add them to a grid using:
grid1.Children.Add(textbox1);
grid2.Children.Add(textbox2);
...
grid20.Children.Add(textbox20);
Is it somehow possible to replace this piece of code with a loop too?
If you have many of these grid/textboxes it might be wiser to wrap them in another control as Yaho Cho suggested in his comment..
I might be stating the obvious, but if you place the grids and textboxes in a list (i.e. create them in code and add them to the container in code) you can loop them.
List<TextBox> tl = new List<TextBox>(){ ... };
List<Grid> gl = new List<Grid>(){ ... }
for (i=0; i<20; i++)
{
gl[i].Children.Add(tl[i]);
container.Children.Add(gl[i]);
}

How to add link Labels at run time in Windows form

I have been making a Windows Form Application in C# using the Visual C# 2008 IDE.
There are basically two forms in my application. One is created at Runtime and it's layout is undefined and the second one's predefined.
Now, I have been adding form elements using the toolbox provided and don't have any idea how to add them using written code(not using toolbox). I want to add n number of Labels to the second form which is undefined. n can be anything(decided at runtime, depending on the user's input). Can anybody tell me what is the efficient way to do this?
Just a quick example of a "dynamic control" being created at run-time and added to the form:
Label lbl = new Label();
lbl.Text = "Hello World!";
lbl.Location = new Point(100, 25);
this.Controls.Add(lbl);
You can replace "this" with the container to add it to, like "panel1" for instance. For containers that have their own layout engine, like a FlowLayoutPanel, then you wouldn't need to specify a Location().
Create a new LinkLabel(), set its properties (in particular, text and position), then add it to the Controls collection of your form or any panel.
You may also want to add event handlers, and store them somewhere (probably in a List<T>) so you can change or remove them later.
Create one in the designer, configure it's properties as you wish. Then go to the designer file, which name is like Form1.Desiner.cs, copy the code related to your LinkLabel (find everything with text search) and paste it where you wish :)

Load form's controls to panel in C#

I want to load Form's controls to a panel in C# so the panel will show the same components as the form. I have tried this code:
foreach (Control control in (new Form2()).Controls)
{
panels[panelsCounter].Controls.Add(control);
}
But the problem is that when I'm running the program it loads only the type of control that I've added last (For example if I've been added a label and than I've added a button to the form it shows only a button, but if I add another label, it shows both of the labels, but not the button).
Please help me.
This is a classic bug, you are modifying the collection while you are iterating it. The side-effect is that only ever other control will be moved to the panel. You'll need to do this carefully, iterate the collection backwards to avoid the problem:
var formObj = new Form2(); //???
for (int ix = formObj.Controls.Count-1; ix >= 0; --ix) {
panels[panelsCounter].Controls.Add(formObj.Controls[ix]);
}
Controls are not designed to be displayed multiple times. You cannot add controls to multiple forms, or add the same control to a form multiple times. They simply weren't designed to support it.
You could go through each control and create a new control of the same type, and even copy over the values of their properties (or at least what's publicly accessible to you), effectively cloning them, but it's important that it be a different control that you add to the new panel.

Indexing into Controls Collection on Tab Control

I have a C# Forms tab application. Each TabPage has a menu on the left (Outlook style navigation panel), and a Panel on the right for content.
If I want the content panel for tab page 0, how would I go about fetching it? I'm a bit stumped because I don't know how to index into the controls collection on a tab page. The following is underlined in red, so I believe its wrong.
Panel panel = tabControl.TabPages[0].Controls["Panel"];
EDIT: remove Window in Panel sub question. It will be moved to a separate question.
Sorry about the beginner questions. I'm a C/C++ guy with lots of MFC time, and C# UI is a bit frustrating at the moment.
foreach (Control control in tabControl1.TabPages[0].Controls)
{
// if (control.Name == "panel1")
}
You can always call this recursively on control.Controls to find a control in any hierarchy. control.Name can be used to find your specific control.
You can't show a Form, inside a Panel. You could create Custom Control where you can add your functionality and add that control to a Panel.
in order to create a new form for example you need to create a variable of what ever form that it is you want to create.
example
Form2 frm2 = new Form2();
frm2.Show();
if you want to show that form in the panel then the panel would be the Owner keep in mind the difference between Owner and Parent
please paste what ever code you have so far and we can suggest the necessary changes
Finally, how does one display a Window in a Panel? - you don't want to do that. If you want a window and a panel to share a piece of UI functionality, create a user control with all the the functionality and then you can place it in a form or in a panel.
A possibility to encapsulate complex UI content is to create a UserControl. This way you can create a reusable piece of complex UI you can basically add as a "blob" inside a form.
The reason why
Panel panel = tabControl.TabPages[0].Controls["Panel"];
is underlined red is because the Controls collection returns a Control which might be a Panel but also might be something else. So you need to cast it:
Panel panel = tabControl.TabPages[0].Controls["Panel"] as Panel;
if (panel != null)
{
// got a panel here so do something
}
Also: MSDN has some good resources - you should make use of it.

Sizing issues while adding a .Net UserControl to a TabPage

I have a complex Windows Forms GUI program that has a lot of automated control generation and manipulation. One thing that I need to be able to do is add a custom UserControl to a newly instatiated TabPage. However, when my code does this I get automatic resizing events that cause the formatting to get ugly. Without detailing all of the different Containers that could possibly be involved, the basic issue is this:
At a certain point in the code I create a new tab page:
TabPage tempTabPage = new TabPage("A New Tab Page");
Then I set it to a certain size that I want it to maintain:
tempTabPage.Width = 1008;
tempTabPage.Height = 621;
Then I add it to a TabControl:
tabControl.TabPages.Add(tempTabPage);
Then I create a user control that I want to appear in the newly added TabPage:
CustomView customView = new CustomView("A new custom control");
Here is where the problem comes in. At this point both the tempTabPage and the customView are the same size with no padding or margin and they are the size I want them to be. I now try to add this new custom UserControl to the tab page like this:
tempTabPage.Controls.Add(customView);
When making this call the customView and it's children controls get resized to be larger and so parts of the customView are hidden.
Can anyone give me any direction on what to look for or what could be causing this kind of issue?
Thanks ahead of time.
The UserControl's "AutoScaleMode" property should be set to "None".
If you want the customView to fill the TabPage.
Use Dock like this:
tempTabPage.Controls.Add(customView);
customView.Dock = DockStyle.Fill;
Then the customView will fill out the space in the TabPage, but you have to handle resizing of the customView so child controls will be shown properly.
I had the same issue.
The UserControl's "AutoScaleMode" set to "None" works for me.

Categories

Resources