ToolStripDropDown - Columns and Sorting

J

Jeff Gaines

I am using a ToolStrip with several ToolStripSplitButtons that have
attached ToolStripDropDowns.

Because of the number of items that have to be shown I am using:

tsDropDown.LayoutStyle = ToolStripLayoutStyle.Table;
((TableLayoutSettings)tsDropDown.LayoutSettings).ColumnCount =
(int)(numMenuItems / 15);

Which splits the menus into columns of 15 items.

It all works as intended but the sort order is driving me mad. It sorts
across the first row then the second row etc. This is columnar data and I
want it sorted down the first column then down the second etc. I've
noticed that Win7 uses this sort order in its own programs which may
explain why I cannot find anything in Control Panel.

Is there a layout style that will sort down then across?
If not I'll have to brush up my mods and divs and sort the list of items
differently!
 
J

Jeff Gaines

On 29/12/2012 in message <[email protected]> Jeff
Gaines wrote:

Sorry to reply to my own post. I wrote a function to change the order of a
sorted list of ToolStripMenuItems and now get the 'down and across' order
I want.

If anybody knows a better way I'd be pleased to hear it!

internal List<ToolStripMenuItem>
SortMenuItemsByColumns(List<ToolStripMenuItem> listItems, float
fmaxItemsPerColumn, out int columnCount)
{
// Windows sorts across then down but this is columnar data and should
be down and across
// SortMenuItemsByColumns changes the order so it shows up correctly
// Each column must be full though (or Windows will shuffle items up
randomnly) so we need to add dummies to top it up

float flistItemsCount = (float)listItems.Count;
float fcolumnCount = (float)(flistItemsCount / fmaxItemsPerColumn);
columnCount = (int)(Math.Ceiling(fcolumnCount));

int totalCapacity = columnCount * (int)fmaxItemsPerColumn;

if (listItems.Count < totalCapacity)
{
do
{
listItems.Add(new ToolStripMenuItem(""));
}
while (listItems.Count < totalCapacity);
}

List<ToolStripMenuItem> listSortedItems = new
List<ToolStripMenuItem>(listItems.Count);

flistItemsCount = (float)listItems.Count;

fcolumnCount = (float)(flistItemsCount / fmaxItemsPerColumn);
columnCount = (int)(Math.Ceiling(fcolumnCount));

float fitemsPerCol = (float)(flistItemsCount / fcolumnCount);
int itemsPerCol = (int)(Math.Ceiling(fitemsPerCol));

int itemIndex;

for (int itemCount = 0; itemCount < itemsPerCol; itemCount++)
{
for (int colCount = 0; colCount < columnCount; colCount++)
{
itemIndex = itemCount + (colCount * itemsPerCol);
if (itemIndex < listItems.Count)
listSortedItems.Add(listItems[itemIndex]);
}
}

return listSortedItems;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top