Reputation: 1580
NOTE: All controls described in this question are DEVEXPRESS controls.
I am attempting to programatically add both LayoutControlItems
and EmptySpaceItems
to a LayoutControlGroup
on a DevExpress LayoutControl
.
The requirements for my project require that I have a LayoutControlGroup
panel that is dependent on a set of filter items chosen from another control on the layout control. If there are no filters chosen, than none of the `LayoutControlItems are shown. If one or more of the filters are chosen that I add one or more of the controls to the group based on the selection.
The way that I attempt to do this is the following:
1) in the designer for the LayoutControl
I have already created the LayoutControlGroup
with the LayoutControlItem
s. There are 6 total, and each LayoutControlItem
contains a PanelControl
that contains a ListBoxControl
2) When the form is initalized I hide each of the LayoutControlItems
from the LayoutControlGroup
using the LayoutControlItem.HideFromCustomization()
method.
3) After the user selects a filter or set of filters I run the following code attempting to restore the control items to the group from left to right.
this.layoutGroupScenarioPortfolios.BeginUpdate();
LayoutControlItem layoutControlToAdd;
LayoutControlItem lastLayoutControlItem = null;
for (int loop = 0; loop < selectedFilters.Length; loop++)
{
layoutControlToAdd = LayoutControlItemFactoryUtil(selectedFilters[loop]);
if (layoutControlToAdd == null)
{
continue;
}
if (loop < 1)
{
layoutControlToAdd.RestoreFromCustomization(this.layoutControlGroupSelectedFilters);
}
else
{
layoutControlToAdd.RestoreFromCustomization(lastLayoutControlItem, DevExpress.XtraLayout.Utils.InsertType.Right);
}
lastLayoutControlItem = layoutControlToAdd;
}
for (int loop = 0; loop < numOfEmptySpaceItemsNeeded; loop++)
{
layoutControlToAdd = new EmptySpaceItem(this.layoutControlGroupSelectedFilters)
{
Owner = this.layoutControlGroupSelectedFilters.Owner
};
layoutControlToAdd.RestoreFromCustomization(lastLayoutControlItem, DevExpress.XtraLayout.Utils.InsertType.Right);
lastLayoutControlItem = layoutControlToAdd;
}
this.layoutControlGroupSelectedFilters.TextVisible = true;
this.layoutGroupScenarioPortfolios.EndUpdate();
As you can see from the code one loop adds the appropriate ListControlBox
to the group. The second loop tries to add empty space items to make sure that the list box control does not take up the entire group. By the end of this code there should be 6 items spanning the group control, each with the same width in the control.
The problem is that the first control added takes up half of the group box's space while the other 5 items are equally fitted into the remaining half of the group box.
In the first loop, is the RestoreFromCustomization()
method with one parameter the correct method to use?
Upvotes: 1
Views: 5887
Reputation: 22597
I would suggest to place the controls at runtime. LayoutControl will manage the LayoutControlGroups and EmptySpaceItems.
Here is the code I had written to place user controls into the LayoutControl at runtime:
LayoutControlItem lastItem = null;
int RowWidth = 0;
public void AddMyControl()
{
MyControl myControl = new MyControl("");
myControl.Name = Guid.NewGuid().ToString();
LayoutControlItem item = new LayoutControlItem();
item.Name = Guid.NewGuid().ToString();
item.Text = "";
MyLayoutControl.BeginUpdate();
//We need to determine where to insert the new item. Right or Below. If there is
//space on the right we insert at Right else we just add the item.
if(lastItem == null || lastItem != null && (MyLayoutControl.Width - UserControlWidth) < RowWidth)
{
MyLayoutControl.AddItem(item);
RowWidth = item.MinSize.Width;
}
else
{
MyLayoutControl.AddItem(item, lastItem, DevExpress.XtraLayout.Utils.InsertType.Right);
}
item.Control = myControl;
RowWidth += item.MinSize.Width;
lastItem = item;
item.Name = " ";
MyLayoutControl.BestFit();
MyLayoutControl.EndUpdate();
}
If you just need left to right controls, flowlayoutpanel will suit better. Sometimes LayoutControl is tough to work with. I had eventually go with flowlayoutpanel as it is much simpler to work with.
Upvotes: 1